50 分征求 字符串加密解密程序(50分)

  • 主题发起人 主题发起人 Temp
  • 开始时间 开始时间
T

Temp

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; 原串不带回车或者换行字符,要求加密后的字符也没有回车和换行字符,要求加密解密功能<br>放在一个函数里面,以word类型作为加密和解密的参数
 
//---------密碼加密---------------------------<br>function PPbin(s:integer):Char;<br>begin<br>if s&lt;0 then<br>s:=s+256<br>else if s&gt;255 then<br>s:=s-256;<br>Result:=Chr(S); //IntToStr(integer(chr(s)));<br>end;<br><br>function pw2bin(str:string;l:boolean):string;<br>var<br>a1,a2,a3,a4,a5,a6:string;<br>begin<br>if l then begin<br>a1:=PPbin(ord(Str[1])+31);<br>a2:=ppbin(Ord(Str[2])-ord(Str[1])-2);<br>a3:=ppbin(ord(Str[3])-ord(Str[2])-3);<br>a4:=ppbin(ord(Str[4])-ord(str[3])-4);<br>a5:=ppbin(ord(Str[5])-ord(Str[4])-5);<br>a6:=ppbin(ord(Str[6])-ord(Str[5])-6);<br>end<br>else<br>begin<br>a1:=ppbin(ord(Str[1])-31);<br>a2:=ppbin(ord(Str[2])+ord(a1[1])+2);<br>a3:=ppbin(ord(Str[3])+ord(a2[1])+3);<br>a4:=ppbin(ord(Str[4])+ord(a3[1])+4);<br>a5:=ppbin(ord(Str[5])+ord(a4[1])+5);<br>a6:=ppbin(ord(Str[6])+ord(a5[1])+6);<br>end;<br>Result:=a1+a2+a3+a4+a5+a6;<br>end;<br>//-----------------------------------------<br>function Encrypt(S: string): string;<br>var<br>i,j,k1,k2: integer;<br>cSum,nSum: integer;<br>sTmp: string[8];<br>begin<br>Result := Space(8)+'??';<br><br>//檢查密碼是否合法.<br>S := TrimRight(S);<br>if Length(S) &lt; 5 then<br>begin Showmessage('密碼長度最少必?5位'); Exit; end;<br>if Length(S) &gt; 8 then<br>begin Showmessage('密碼長度超過8位'); Exit; end;<br>cSum:= 0;<br>nSum:= 0;<br>k1:=0;<br>k2:=0;<br>for i := 1 to Length(S) do begin<br>if not (S in ['A'..'Z','a'..'z','0'..'9']) then<br>begin Showmessage('密碼含有不合法字元'); Exit; end;<br>if S in ['A'..'Z','a'..'z'] then Inc(cSum);<br>if S in ['0'..'9'] then Inc(nSum);<br>end;<br>if cSum &lt; 3 then<br>begin Showmessage('密碼最少必?含3位字母'); Exit; end;<br>if nSum &lt; 2 then<br>begin Showmessage('密碼最少必?含2位數字'); Exit; end;<br><br>//加密.<br>Randomize;<br>while True do begin<br>k1 := Random(30);<br>k2 := Random(30);<br>sTmp := PadR(S,8);<br>Result := '';<br>for i := 1 to 8 do begin<br>j := k1 Mod 30 + i;<br>j := (k2+j) Mod 30 + 1;<br>Result := Result + Chr(Ord(sTmp) xor j)<br>end;<br>if (Pos('"',Result)&gt;0) or (Pos('''',Result)&gt;0) then<br>Else Break;<br>end;<br>Result := Result + Chr(k1+40) + Chr(k2+60);<br>end;<br><br>//--------密碼解密----------------------------<br>function Decrypt(S: string): string;<br>var<br>i,j: integer;<br>Key1,Key2: integer;<br>begin<br>Result := '??'+Space(6);<br><br>//檢查密碼是否正確.<br>if Length(S) &lt;&gt; 10 then Exit;<br><br>//解密<br>Key1 := Ord(S[9])-40;<br>Key2 := Ord(S[10])-60;<br>Result := '';<br>for i := 1 to 8 do begin<br>j := Key1 Mod 30 + i;<br>j := (Key2+j) Mod 30 + 1;<br>Result := Result + Chr(Ord(S) xor j)<br>end;<br>Result := Trim(Result);<br>end;<br>//--------字元加密----------------------------<br>function EncryStr(S: String;n:Integer): String;<br>var<br>i,j,k1,k2: integer;<br>sTmp: String;<br>begin<br>//加密.<br>Randomize;<br>while True do begin<br>k1 := Random(30);<br>k2 := Random(30);<br>sTmp := PadR(S,n);<br>Result := '';<br>for i := 1 to n do begin<br>j := k1 Mod 30 + i;<br>j := (k2+j) Mod 30 + 1;<br>Result := Result + Chr(Ord(sTmp) xor j)<br>end;<br>if (Pos('"',Result)&gt;0) or (Pos('''',Result)&gt;0) then<br>Else Break;<br>end;<br>Result := Result + Chr(k1+40) + Chr(k2+60);<br>end;<br>//--------字元解密----------------------------<br>function DecryStr(S: String;n:Integer): String;<br>var<br>i,j: integer;<br>Key1,Key2: integer;<br>begin<br>//檢查密碼是否正確.<br>if Length(S) &lt;&gt; n+2 then Exit;<br><br>//解密<br>Key1 := Ord(S[n+1])-40;<br>Key2 := Ord(S[n+2])-60;<br>Result := '';<br>for i := 1 to n do begin<br>j := Key1 Mod 30 + i;<br>j := (Key2+j) Mod 30 + 1;<br>Result := Result + Chr(Ord(S) xor j)<br>end;<br>Result := Trim(Result);<br>end;<br>
 
faint,楼上的大侠能否按照要求给出愿程序呢?就是:原串不带回车或者换行字符,要求加密后的字符也没有回车和换行字符,要求加密解密功能<br>放在一个函数里面,以word类型作为加密和解密的参数<br><br>可能的函数声明: function CryptStr(const S:String; sType: DWord):String;
 
&nbsp; &nbsp;最重要是保证加密后的字符不包含回车或者换行字符,因为相应的字符要保存到<br>文本文件里面,如果加上了回车换行符号,读文件就会产生问题
 
Function EncrypKey (Src:String; Key:String):string;<br>var<br>&nbsp; KeyLen :Integer;<br>&nbsp; KeyPos :Integer;<br>&nbsp; offset :Integer;<br>&nbsp; dest :string;<br>&nbsp; SrcPos :Integer;<br>&nbsp; SrcAsc :Integer;<br>&nbsp; Range :Integer;<br>begin<br>&nbsp; KeyLen:=Length(Key);<br>&nbsp; if KeyLen = 0 then<br>&nbsp; &nbsp; key:='Think Space';<br>&nbsp; KeyPos:=0;<br>&nbsp; Range:=256;<br><br>&nbsp; Randomize;<br>&nbsp; offset:=Random(Range);<br>&nbsp; dest:=format('%1.2x',[offset]);<br>&nbsp; for SrcPos := 1 to Length(Src) do<br>&nbsp; begin<br>&nbsp; &nbsp; SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;<br>&nbsp; &nbsp; if KeyPos &lt; KeyLen then<br>&nbsp; &nbsp; &nbsp; KeyPos:= KeyPos + 1 else KeyPos:=1;<br>&nbsp; &nbsp; SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);<br>&nbsp; &nbsp; dest:=dest + format('%1.2x',[SrcAsc]);<br>&nbsp; &nbsp; offset:=SrcAsc;<br>&nbsp; end;<br>&nbsp; Result:=Dest;<br>end;<br><br>Function UncrypKey (Src:String; Key:String):string;<br>var<br>&nbsp; KeyLen :Integer;<br>&nbsp; KeyPos :Integer;<br>&nbsp; offset :Integer;<br>&nbsp; dest :string;<br>&nbsp; SrcPos :Integer;<br>&nbsp; SrcAsc:Integer;<br>&nbsp; TmpSrcAsc :Integer;<br>begin<br>&nbsp; KeyLen:=Length(Key);<br>&nbsp; if KeyLen = 0 then<br>&nbsp; &nbsp; key:='Think Space';<br>&nbsp; KeyPos:=0;<br>&nbsp; offset:=StrToInt('$'+ copy(src,1,2));<br>&nbsp; SrcPos:=3;<br>&nbsp; repeat<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));<br>&nbsp; &nbsp; except<br><br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if KeyPos &lt; KeyLen Then<br>&nbsp; &nbsp; &nbsp; KeyPos := KeyPos + 1<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; KeyPos := 1;<br>&nbsp; &nbsp; TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);<br>&nbsp; &nbsp; if TmpSrcAsc &lt;= offset then<br>&nbsp; &nbsp; &nbsp; TmpSrcAsc := 255 + TmpSrcAsc - offset<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; TmpSrcAsc := TmpSrcAsc - offset;<br>&nbsp; &nbsp; dest := dest + chr(TmpSrcAsc);<br>&nbsp; &nbsp; offset:=srcAsc;<br>&nbsp; &nbsp; SrcPos:=SrcPos + 2;<br>&nbsp; until SrcPos &gt;= Length(Src);<br>&nbsp; Result:=Dest;<br>end;<br>
 
to zhihuali:<br><br>&nbsp; &nbsp;好像您的源程序并没有防止回车或者换行符的语句阿?
 
再麻烦卷起千堆雪tyn大侠:<br>&nbsp; &nbsp;<br>&nbsp; &nbsp;请问您的源程序是如何防止加密后的字符出现回车或者换行符号呢?<br>(因为我用readln读写文本文件,如果出现这两个字符,读写可能会出现错误)<br>非常不好意思再麻烦大侠,请再帮一把~~~~<br>&nbsp;
 
唉~~~~自己整理吧~~~~<br><br>const<br>&nbsp; C1 = 52845;<br>&nbsp; C2 = 22719;<br><br>function Encrypt(const S: String; Key: Word): String;<br>var<br>&nbsp; I: byte;<br>begin<br>&nbsp; Result[0] := S[0];<br>&nbsp; for I := 1 to Length(S) do begin<br>&nbsp; &nbsp; Result := char(byte(S) xor (Key shr 8));<br>&nbsp; &nbsp; Key := (byte(Result) + Key) * C1 + C2;<br>&nbsp; end;<br>end;<br><br>function Decrypt(const S: String; Key: Word): String;<br>var<br>&nbsp; I: byte;<br>begin<br>&nbsp; Result[0] := S[0];<br>&nbsp; for I := 1 to Length(S) do begin<br>&nbsp; &nbsp; Result := char(byte(S) xor (Key shr 8));<br>&nbsp; &nbsp; Key := (byte(S) + Key) * C1 + C2;<br>&nbsp; end;<br>end;<br><br>var<br>&nbsp; S: string;<br>begin<br>&nbsp; Write('&gt;');<br>&nbsp; ReadLn(S);<br>&nbsp; S := Encrypt(S,12345);<br>&nbsp; WriteLn(S);<br>&nbsp; S := Decrypt(S,12345);<br>&nbsp; WriteLn(S);<br>end.<br><br>
 
这很容易啊:<br>第一步:先将待加密字符换码,比如将一个字符变成两个只含0~9,a~f的字符,<br>回车就是“0d”,换行就是“0a”,等等,换码后源字符就不含回车或换行符了;<br>第二步:对加密后的字符判断,如果加密后的字符是回车或换行,就用加密前的字符替换,<br>也就是该字符与没有加密一样。解密时遇到该字符,解密结果必定也是回车或换行,同样<br>用加密前的字符替换,相当于这个字符既未加密也未解密。<br>第三步:再将解密后的两两字符换码回来,就是你原始的(可能含有特殊字符的)文本。<br><br>我做加密解密程序就是用上述方法,效果很满意。:)<br>
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部