求一个base64算法(100分)

  • 主题发起人 主题发起人 zhuangqr
  • 开始时间 开始时间
Z

zhuangqr

Unregistered / Unconfirmed
GUEST, unregistred user!
求一个base64算法  急~~!
 
//----------------------------------------------------------------------------<br>//base64字符串加密函数<br>function Base64Encode(const s: string): string;<br>var<br> &nbsp;i,c1,c2,c3: Integer;<br> &nbsp;m,n: Integer;<br>const<br> &nbsp;Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';<br>begin<br> &nbsp;Result := '';<br> &nbsp;m:=1;<br> &nbsp;n:=0;<br> &nbsp;for i := 1 to (Length(s) div 3) do<br> &nbsp;begin<br> &nbsp; &nbsp;c1 := Ord(s[m]);<br> &nbsp; &nbsp;c2 := Ord(s[m+1]);<br> &nbsp; &nbsp;c3 := Ord(s[m+2]);<br> &nbsp; &nbsp;m:=m+3;<br> &nbsp; &nbsp;Result := Result+base64[(c1 shr 2)and $3F+1];<br> &nbsp; &nbsp;Result := Result+base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];<br> &nbsp; &nbsp;Result := Result+base64[((c2 shl 2)and $3C) or ((c3 shr 6)and $03)+1];<br> &nbsp; &nbsp;Result := Result+base64[c3 and $3F+1];<br> &nbsp; &nbsp;n:=n+4;<br> &nbsp; &nbsp;if(n = 76)then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; n:=0;<br> &nbsp; &nbsp; &nbsp; Result := Result+#13#10;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;if (Length(s) mod 3)=1 then<br> &nbsp;begin<br> &nbsp; &nbsp;c1 := Ord(s[m]);<br> &nbsp; &nbsp;Result := Result+base64[(c1 shr 2)and $3F+1];<br> &nbsp; &nbsp;Result := Result+base64[(c1 shl 4)and $30+1];<br> &nbsp; &nbsp;Result := Result+'=';<br> &nbsp; &nbsp;Result := Result+'=';<br> &nbsp;end;<br> &nbsp;if (Length(s) mod 3)=2 then<br> &nbsp;begin<br> &nbsp; &nbsp;c1 := Ord(s[m]);<br> &nbsp; &nbsp;c2 := Ord(s[m+1]);<br> &nbsp; &nbsp;Result := Result+ base64[(c1 shr 2)and $3F+1];<br> &nbsp; &nbsp;Result := Result+ base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];<br> &nbsp; &nbsp;Result := Result+base64[(c2 shl 2)and $3C+1];<br> &nbsp; &nbsp;Result := Result+ '=';<br> &nbsp;end;<br>end;<br>//----------------------------------------------------------------------------<br>//base64字符串解密函数<br>function Base64Decode(const s: string): string;<br>var<br> &nbsp;i,m,n: Integer;<br> &nbsp;c1,c2,c3,c4: Integer;<br>const<br> &nbsp;Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';<br>begin<br> &nbsp;Result := '';<br> &nbsp;n:=1;<br> &nbsp;m:=Length(s);<br> &nbsp;if s[m]='='then m:=m-1;<br> &nbsp;if s[m]='='then m:=m-1;<br> &nbsp;for i:=1 to m div 4 do<br> &nbsp;begin<br> &nbsp; &nbsp;c1:=Pos(s[n],Base64)-1;<br> &nbsp; &nbsp;c2:=Pos(s[n+1],Base64)-1;<br> &nbsp; &nbsp;c3:=Pos(s[n+2],Base64)-1;<br> &nbsp; &nbsp;c4:=Pos(s[n+3],Base64)-1;<br> &nbsp; &nbsp;n:=n+4;<br> &nbsp; &nbsp;Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));<br> &nbsp; &nbsp;Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));<br> &nbsp; &nbsp;Result:=Result+Chr(((c3 shl 6)and $C0)or c4);<br> &nbsp;end;<br> &nbsp;if m mod 4=2 then<br> &nbsp;begin<br> &nbsp; &nbsp;c1:=Pos(s[n],Base64)-1;<br> &nbsp; &nbsp;c2:=Pos(s[n+1],Base64)-1;<br> &nbsp; &nbsp;Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));<br> &nbsp;end;<br><br> &nbsp;if m mod 4=3 then<br> &nbsp;begin<br> &nbsp; &nbsp;c1:=Pos(s[n],Base64)-1;<br> &nbsp; &nbsp;c2:=Pos(s[n+1],Base64)-1;<br> &nbsp; &nbsp;c3:=Pos(s[n+2],Base64)-1;<br> &nbsp; &nbsp;Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));<br> &nbsp; &nbsp;Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));<br> &nbsp;end;<br>end;<br><br><br>分全部给我吧,我没分了.
 
符合要求
 
后退
顶部