规则
例子1
索引 | 对应字符 | 索引 | 对应字符 | 索引 | 对应字符 | 索引 | 对应字符 |
0 | A | 17 | R | 34 | i | 51 | z |
1 | B | 18 | S | 35 | j | 52 | 0 |
2 | C | 19 | T | 36 | k | 53 | 1 |
3 | D | 20 | U | 37 | l | 54 | 2 |
4 | E | 21 | V | 38 | m | 55 | 3 |
5 | F | 22 | W | 39 | n | 56 | 4 |
6 | G | 23 | X | 40 | o | 57 | 5 |
7 | H | 24 | Y | 41 | p | 58 | 6 |
8 | I | 25 | Z | 42 | q | 59 | 7 |
9 | J | 26 | a | 43 | r | 60 | 8 |
10 | K | 27 | b | 44 | s | 61 | 9 |
11 | L | 28 | c | 45 | t | 62 | + |
12 | M | 29 | d | 46 | u | 63 | / |
13 | N | 30 | e | 47 | v | ||
14 | O | 31 | f | 48 | w | ||
15 | P | 32 | g | 49 | x | ||
16 | Q | 33 | h | 50 | y |
例子2
原理
编辑先以“迅雷下载”为例: 很多下载类网站都提供“迅雷下载”的链接,其地址通常是加密的迅雷专用下载地址。
应用
编辑代码实现
编辑JavaScript版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | if (!Shotgun)
var Shotgun={}; if (!Shotgun.Js)
Shotgun.Js={}; Shotgun.Js.Base64={
_table:[
'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' ,
'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ,
'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' ,
'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/'
],
encode: function (bin){
var codes=[];
var un=0;
un=bin.length%3;
if (un==1)
bin.push(0,0);
else if (un==2)
bin.push(0);
for ( var i=2;i<bin.length;i+=3){
var c=bin[i-2]<<16;
c|=bin[i-1]<<8;
c|=bin[i];
codes.push( this ._table[c>>18&0x3f]);
codes.push( this ._table[c>>12&0x3f]);
codes.push( this ._table[c>>6&0x3f]);
codes.push( this ._table[c&0x3f]);
}
if (un>=1){
codes[codes.length-1]= "=" ;
bin.pop();
}
if (un==1){
codes[codes.length-2]= "=" ;
bin.pop();
}
return codes.join( "" );
},
decode: function (base64Str){
var i=0;
var bin=[];
var x=0,code=0,eq=0;
while (i<base64Str.length){
var c=base64Str.charAt(i++);
var idx= this ._table.indexOf(c);
if (idx==-1){
switch (c){
case '=' :idx=0;eq++; break ;
case '' :
case ' ' :
case " " :
case ' ' :
continue ;
default :
throw { "message" : "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au65E0u6548u7F16u7801uFF1A" +c};
}
}
if (eq>0&&idx!=0)
throw { "message" : "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u683Cu5F0Fu9519u8BEFuFF01" };
code=code<<6|idx;
if (++x!=4)
continue ;
bin.push(code>>16);
bin.push(code>>8&0xff);
bin.push(code&0xff)
code=x=0;
}
if (code!=0)
throw { "message" : "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u6570u636Eu957Fu5EA6u9519u8BEF" };
if (eq==1)
bin.pop();
else if (eq==2){
bin.pop();
bin.pop();
} else if (eq>2)
throw { "message" : "u0062u0061u0073u0065u0036u0034u002Eu0074u0068u0065u002Du0078u002Eu0063u006Eu0020u0045u0072u0072u006Fu0072u003Au7F16u7801u683Cu5F0Fu9519u8BEFuFF01" };
return bin;
} }; |
BASH版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | base64Table=(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu v wxyz0123456789+/); function str2binary(){
idx=0;
for ((i=0;i<${ #str};i++));do
dividend=$( printf "%d" "'${str:i:1}" );
for ((j=0;j<8;j++)); do
let idx=8*i+7-j;
let bin[$idx]=$dividend%2;
dividend=$dividend /2 ;
done ;
done ;
let idx=${ #str}*8;
for ((i=0;i<appendEqualCnt*2;i++)); do
let bin[$idx]=0;
let idx++;
done ; } function calcBase64(){
for ((i=0;i<${ #bin[*]}/6;i++));do
sum =0;
for ((j=0;j<6;j++)); do
let idx=i*6+j;
let n=6-1-j;
let sum = sum +${bin[$idx]}*2**n;
done ;
echo -n${base64Table[$ sum ]};
done } declare -abin function base64Encode(){
read -p "pleaseenterASCIIstring:" str;
let appendZero=${ #str}*8%6;
let bits=${ #str}*8;
appendEqualCnt=0;
if [[$appendZero- ne 0]]; then
let appendEqualCnt=(6-$appendZero) /2 ;
fi
str2binary;
calcBase64;
if [[$appendEqualCnt- eq 2]]; then
echo -n "==" ;
elif [[$appendEqualCnt- eq 1]]; then
echo -n "=" ;
fi
echo ;
} |
Java版
1 2 3 4 5 6 7 | import java.util.Base64; 对于标准的Base64: 加密为字符串使用Base64.getEncoder().encodeToString(); 加密为字节数组使用Base64.getEncoder().encode(); 解密使用Base64.getDecoder().decode(); 对于URL安全或MIME的Base64,只需将上述getEncoder()getDecoder()更换为getUrlEncoder()getUrlDecoder() 或getMimeEncoder()和getMimeDecoder()即可。 |
PHP版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | <?php $txt1 =trim( $_POST [ 'text1' ]); $txt2 =trim( $_POST [ 'text2' ]); $txt3 =trim( $_POST [ 'text3' ]); $button = $_POST [ 'button' ]; ?> <!DOCTYPEHTMLPUBLIC "-//W3C//DTDHTML4.0Transitional//EN" > <html> <head> <title>迅雷和FlashGet,QQ旋风地址地址转换工具</title> <metahttp-equiv= "Content-Type" content= "text/html;charset=gb2312" > <metacontent= "迅雷,FlashGet,地址转换," name= "keywords" > </head> <body> <formname= "form1" method= "post" action= "" > <hrsize= "1" > <h3>迅雷转换</h3> <P>转换地址: <inputname= "text1" value= "<?phpecho$txt1;?>" type= "text" style= "516px;" /></P> <P>转换后的: <inputtype= "text" value="<?php ?> "style=" 516px;"/></P> <P> <inputtype= "submit" name= "button" value= "普通地址->迅雷地址" /> <inputtype= "submit" name= "button" value= "迅雷地址->普通地址" /></P> <h3>FlashGet转换</h3> <P>FlashGet地址: <inputname= "text2" value= "<?phpecho$txt2;?>" type= "text" style= "516px;" /></P> <P>转换后地址: <inputtype= "text" value="<?php if ( $button == "FlashGet地址->普通地址" )echostr_ireplace( "[FLASHGET]" , "" , base64_decode ( str_ireplace ( "flashget://" , "" , $txt2 ))); ?> "style=" 516px;"/></P> <P> <inputtype= "submit" value= "普通地址->FlashGet地址" name= "button" /> <inputtype= "submit" value= "FlashGet地址->普通地址" name= "button" /></P> <h3>QQ旋风转换</h3> <P>QQ旋风地址: <inputname= "text3" value= "<?phpecho$txt3;?>" type= "text" style= "516px;" /></P> <P>转换后地址: <inputtype= "text" value="<?php ?> "style=" 516px;"/></P> <P> <inputtype= "submit" value= "普通地址->QQ旋风" name= "button" /> <inputtype= "submit" value= "QQ旋风->普通地址" name= "button" /></P> </form> </body> </html> |
VB版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | Option Explicit 'Base64Encoding/DecodingAlgorithm 'By:DavidMidkiff(mznull@earthlink.net) ' 'ThisalgorithmsencodesanddecodesdataintoBase64 'format.Thisformatisextremelymoreefficientthan 'Hexadecimalencoding. Private m_bytIndex(0 To 63) As Byte Private m_bytReverseIndex(0 To 255) As Byte Private Const k_bytEqualSign As Byte =61 Private Const k_bytmask1 As Byte =3 Private Const k_bytmask2 As Byte =15 Private Const k_bytmask3 As Byte =63 Private Const k_bytmask4 As Byte =192 Private Const k_bytmask5 As Byte =240 Private Const k_bytmask6 As Byte =252 Private Const k_bytShift2 As Byte =4 Private Const k_bytShift4 As Byte =16 Private Const k_bytShift6 As Byte =64 Private Const k_lMaxBytesPerLine As Long =152 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( ByVal Destination As Long , ByVal Source As Long , ByVal Length As Long ) Public Function Decode64(sInput As String ) As String If sInput= "" Then Exit Function Decode64=StrConv(DecodeArray64(sInput),vbUnicode) End Function Private Function DecodeArray64(sInput As String ) As Byte () Dim bytInput() As Byte Dim bytWorkspace() As Byte Dim bytResult() As Byte Dim lInputCounter As Long Dim lWorkspaceCounter As Long bytInput=Replace(Replace(sInput,vbCrLf, "" ), "=" , "" ) ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput)*2)) As Byte lWorkspaceCounter=LBound(bytWorkspace) For lInputCounter=LBound(bytInput) To UBound(bytInput)
bytInput(lInputCounter)=m_bytReverseIndex(bytInput(lInputCounter)) Next lInputCounter For lInputCounter=LBound(bytInput) To (UBound(bytInput)-((UBound(bytInput) Mod 8)+8)) Step 8
bytWorkspace(lWorkspaceCounter)=(bytInput(lInputCounter)*k_bytShift2)+(bytInput(lInputCounter+2)k_bytShift4)
bytWorkspace(lWorkspaceCounter+1)=((bytInput(lInputCounter+2) And k_bytmask2)*k_bytShift4)+(bytInput(lInputCounter+4)k_bytShift2)
bytWorkspace(lWorkspaceCounter+2)=((bytInput(lInputCounter+4) And k_bytmask1)*k_bytShift6)+bytInput(lInputCounter+6)
lWorkspaceCounter=lWorkspaceCounter+3 Next lInputCounter Select Case (UBound(bytInput) Mod 8):
Case 3:
bytWorkspace(lWorkspaceCounter)=(bytInput(lInputCounter)*k_bytShift2)+(bytInput(lInputCounter+2)k_bytShift4)
Case 5:
bytWorkspace(lWorkspaceCounter)=(bytInput(lInputCounter)*k_bytShift2)+(bytInput(lInputCounter+2)k_bytShift4)
bytWorkspace(lWorkspaceCounter+1)=((bytInput(lInputCounter+2) And k_bytmask2)*k_bytShift4)+(bytInput(lInputCounter+4)k_bytShift2)
lWorkspaceCounter=lWorkspaceCounter+1
Case 7:
bytWorkspace(lWorkspaceCounter)=(bytInput(lInputCounter)*k_bytShift2)+(bytInput(lInputCounter+2)k_bytShift4)
bytWorkspace(lWorkspaceCounter+1)=((bytInput(lInputCounter+2) And k_bytmask2)*k_bytShift4)+(bytInput(lInputCounter+4)k_bytShift2)
bytWorkspace(lWorkspaceCounter+2)=((bytInput(lInputCounter+4) And k_bytmask1)*k_bytShift6)+bytInput(lInputCounter+6)
lWorkspaceCounter=lWorkspaceCounter+2 End Select ReDim bytResult(LBound(bytWorkspace) To lWorkspaceCounter) As Byte If LBound(bytWorkspace)=0 Then lWorkspaceCounter=lWorkspaceCounter+1
CopyMemoryVarPtr(bytResult(LBound(bytResult))),VarPtr(bytWorkspace(LBound(bytWorkspace))),lWorkspaceCounter
DecodeArray64=bytResult End Function Public Function Encode64(ByRefsInput As String ) As String If sInput= "" Then Exit Function Dim bytTemp() As Byte bytTemp=StrConv(sInput,vbFromUnicode) Encode64=EncodeArray64(bytTemp) End Function Private Function EncodeArray64(ByRefbytInput() As Byte ) As String On Error GoTo ErrorHandler Dim bytWorkspace() As Byte ,bytResult() As Byte Dim bytCrLf(0 To 3) As Byte ,lCounter As Long Dim lWorkspaceCounter As Long ,lLineCounter As Long Dim lCompleteLines As Long ,lBytesRemaining As Long Dim lpWorkSpace As Long ,lpResult As Long Dim lpCrLf As Long If UBound(bytInput)<1024 Then
ReDim bytWorkspace(LBound(bytInput) To (LBound(bytInput)+4096)) As Byte Else
ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput)*4)) As Byte End If lWorkspaceCounter=LBound(bytWorkspace) For lCounter=LBound(bytInput) To (UBound(bytInput)-((UBound(bytInput) Mod 3)+3)) Step 3
bytWorkspace(lWorkspaceCounter)=m_bytIndex((bytInput(lCounter)k_bytShift2))
bytWorkspace(lWorkspaceCounter+2)=m_bytIndex(((bytInput(lCounter) And k_bytmask1)*k_bytShift4)+((bytInput(lCounter+1))k_bytShift4))
bytWorkspace(lWorkspaceCounter+4)=m_bytIndex(((bytInput(lCounter+1) And k_bytmask2)*k_bytShift2)+(bytInput(lCounter+2)k_bytShift6))
bytWorkspace(lWorkspaceCounter+6)=m_bytIndex(bytInput(lCounter+2) And k_bytmask3)
lWorkspaceCounter=lWorkspaceCounter+8 Next lCounter Select Case (UBound(bytInput) Mod 3):
Case 0:
bytWorkspace(lWorkspaceCounter)=m_bytIndex((bytInput(lCounter)k_bytShift2))
bytWorkspace(lWorkspaceCounter+2)=m_bytIndex((bytInput(lCounter) And k_bytmask1)*k_bytShift4)
bytWorkspace(lWorkspaceCounter+4)=k_bytEqualSign
bytWorkspace(lWorkspaceCounter+6)=k_bytEqualSign
Case 1:
bytWorkspace(lWorkspaceCounter)=m_bytIndex((bytInput(lCounter)k_bytShift2))
bytWorkspace(lWorkspaceCounter+2)=m_bytIndex(((bytInput(lCounter) And k_bytmask1)*k_bytShift4)+((bytInput(lCounter+1))k_bytShift4))
bytWorkspace(lWorkspaceCounter+4)=m_bytIndex((bytInput(lCounter+1) And k_bytmask2)*k_bytShift2)
bytWorkspace(lWorkspaceCounter+6)=k_bytEqualSign
Case 2:
bytWorkspace(lWorkspaceCounter)=m_bytIndex((bytInput(lCounter)k_bytShift2))
bytWorkspace(lWorkspaceCounter+2)=m_bytIndex(((bytInput(lCounter) And k_bytmask1)*k_bytShift4)+((bytInput(lCounter+1))k_bytShift4))
bytWorkspace(lWorkspaceCounter+4)=m_bytIndex(((bytInput(lCounter+1) And k_bytmask2)*k_bytShift2)+((bytInput(lCounter+2))k_bytShift6))
bytWorkspace(lWorkspaceCounter+6)=m_bytIndex(bytInput(lCounter+2) And k_bytmask3) End Select lWorkspaceCounter=lWorkspaceCounter+8 If lWorkspaceCounter<=k_lMaxBytesPerLine Then
EncodeArray64=Left$(bytWorkspace,InStr(1,bytWorkspace,Chr$(0))-1) Else
bytCrLf(0)=13
bytCrLf(1)=0
bytCrLf(2)=10
bytCrLf(3)=0 ReDim bytResult(LBound(bytWorkspace) To UBound(bytWorkspace)) lpWorkSpace=VarPtr(bytWorkspace(LBound(bytWorkspace))) lpResult=VarPtr(bytResult(LBound(bytResult))) lpCrLf=VarPtr(bytCrLf(LBound(bytCrLf))) lCompleteLines=Fix(lWorkspaceCounter/k_lMaxBytesPerLine) For lLineCounter=0 To lCompleteLines
CopyMemorylpResult,lpWorkSpace,k_lMaxBytesPerLine
lpWorkSpace=lpWorkSpace+k_lMaxBytesPerLine
lpResult=lpResult+k_lMaxBytesPerLine
CopyMemorylpResult,lpCrLf,4&
lpResult=lpResult+4& Next lLineCounter lBytesRemaining=lWorkspaceCounter-(lCompleteLines*k_lMaxBytesPerLine) If lBytesRemaining>0 Then CopyMemorylpResult,lpWorkSpace,lBytesRemaining
EncodeArray64=Left$(bytResult,InStr(1,bytResult,Chr$(0))-1) End If Exit Function ErrorHandler:
Er As ebytResult
EncodeArray64=bytResult End Function |
C#版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 直接使用.NET中的的库类函数 方法: ///<summary> ///Base64加密 ///</summary> ///<paramname="Message"></param> ///<returns></returns> publicstringBase64Code(stringMessage) { byte []bytes=Encoding.Default.GetBytes(Message); returnConvert.ToBase64String(bytes); } ///<summary> ///Base64解密 ///</summary> ///<paramname="Message"></param> ///<returns></returns> publicstringBase64Decode(stringMessage) { byte []bytes=Convert.FromBase64String(Message); returnEncoding.Default.GetString(bytes); } |
python版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | def base(string: str ) - > str :
oldstr = ''
newstr = []
base = ''
base64_list = [ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' ,
'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ,
'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' ,
'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' ]
#把原始字符串转换为二进制,用bin转换后是0b开头的,所以把b替换了,首位补0补齐8位
for i in string:
oldstr + = '{:08}' . format ( int ( str ( bin ( ord (i))).replace( '0b' ,'')))
#把转换好的二进制按照6位一组分好,最后一组不足6位的后面补0
for j in range ( 0 , len (oldstr), 6 ):
newstr.append( '{:<06}' . format (oldstr[j:j + 6 ]))
#在base_list中找到对应的字符,拼接
for l in range ( len (newstr)):
base + = base64_list[ int (newstr[l], 2 )]
#判断base字符结尾补几个‘=’
if len (string) % 3 = = 1 :
base + = '=='
elif len (string) % 3 = = 2 :
base + = '='
return base |