光影岁月 發表於 2019-5-11 18:57:00

Delphi加密解密算法

<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">// 加密方法一(通过密钥加密解密)<br>function EncryptString(Source, Key: string): string;<br>function UnEncryptString(Source, Key: string): string;<br>//加密方法二(通过移位加密解密)<br>function Encode(Str: string): string;<br>function Decode(Str: string): string;<br>//加密方法三(异或加密解密)<br>function Enc(str: string): string;<br>function Dec(str: string): string;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">---------------</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.EncryptString(Source, Key: string): string;<br>// 对字符串加密(Source:源 Key:密匙)<br>var<br>KeyLen: integer;<br>KeyPos: integer;<br>Offset: integer;<br>Dest: string;<br>SrcPos: integer;<br>SrcAsc: integer;<br>Range: integer;<br>begin<br>KeyLen := Length(Key);<br>if KeyLen = 0 then<br>Key := 'delphi';<br>KeyPos := 0;<br>Range := 256;<br>randomize;<br>Offset := random(Range);<br>Dest := format('%1.2x', );<br>for SrcPos := 1 to Length(Source) do<br>begin<br>SrcAsc := (Ord(Source) + Offset) mod 255;<br>if KeyPos &lt; KeyLen then<br>KeyPos := KeyPos + 1<br>else<br>KeyPos := 1;<br>SrcAsc := SrcAsc xor Ord(Key);<br>Dest := Dest + format('%1.2x', );<br>Offset := SrcAsc;<br>end;<br>result := Dest;<br>end;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">---------------</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.UnEncryptString(Source, Key: string): string;<br>// 对字符串解密(Src:源 Key:密匙)<br>var<br>KeyLen: integer;<br>KeyPos: integer;<br>Offset: integer;<br>Dest: string;<br>SrcPos: integer;<br>SrcAsc: integer;<br>TmpSrcAsc: integer;<br>begin<br>KeyLen := Length(Key);<br>if KeyLen = 0 then<br>Key := 'delphi';<br>KeyPos := 0;<br>Offset := strtoint('$' + copy(Source, 1, 2));<br>SrcPos := 3;<br>repeat<br>SrcAsc := strtoint('$' + copy(Source, SrcPos, 2));<br>if KeyPos &lt; KeyLen then<br>KeyPos := KeyPos + 1<br>else<br>KeyPos := 1;<br>TmpSrcAsc := SrcAsc xor Ord(Key);<br>if TmpSrcAsc &lt;= Offset then<br>TmpSrcAsc := 255 + TmpSrcAsc - Offset<br>else<br>TmpSrcAsc := TmpSrcAsc - Offset;<br>Dest := Dest + chr(TmpSrcAsc);<br>Offset := SrcAsc;<br>SrcPos := SrcPos + 2;<br>until SrcPos &gt;= Length(Source);<br>result := Dest;<br>end;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">----------------</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.Decode(Str: string): string;<br>var<br>TmpChr: AnsiChar;<br>i, Len: Integer;<br>begin<br>Result := Str;<br>len := Length(Result);<br>TmpChr := result;<br>for i := Len downto 2 do<br>begin<br>Result := result;<br>Result := TmpChr;<br>end;<br>end;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.Encode(Str: string): string;<br>var<br>TmpChr: AnsiChar;<br>i, Len: Integer;<br>begin<br>Result := Str;<br>len := Length(Result);<br>TmpChr := result;<br>for i := 1 to len - 1 do<br>begin<br>Result := result;<br>Result := TmpChr;<br>end;<br>end;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">---------</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp; XorKey: array of Byte = ($B2, $09, $AA, $55, $93, $6D, $84, $47);</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.Enc(str: string): string;<br>var<br>i, j: Integer;<br>begin<br>Result := '';<br>j := 0;<br>for i := 1 to Length(str) do<br>begin<br>Result := Result + IntToHex(Byte(str) xor XorKey, 2);<br>j := (j + 1) mod 8;<br>end;<br>end;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">&nbsp;</p>
<p style="margin: 10px auto; padding: 0; text-align: left; color: rgba(57, 57, 57, 1); text-transform: none; text-indent: 0; letter-spacing: normal; font-family: verdana, &quot;ms song&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; word-spacing: 0; white-space: normal; orphans: 2; widows: 2; background-color: rgba(250, 247, 239, 1); font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial">function TFrmRegister.Dec(str: string): string;<br>var<br>i, j: Integer;<br>begin<br>Result := '';<br>j := 0;<br>for i := 1 to Length(str) div 2 do<br>begin<br>Result := Result + Char(StrToInt('$' + Copy(str, i * 2 - 1, 2)) xor<br>XorKey);<br>j := (j + 1) mod 8;<br>end;<br>end;</p>

</div>
<div id="MySignature" role="contentinfo">
    好的代码像粥一样,都是用时间熬出来的<br><br>
来源:https://www.cnblogs.com/jijm123/p/10849622.html
頁: [1]
查看完整版本: Delphi加密解密算法