清年 發表於 2022-6-7 15:25:00

DELPHI国密SM4加密解密-可与JAVA互通

<p>--代码源自开源项目cnvcl,引用单元请从cnpack官网下载</p>
<p>--cnvcl的git项目上边已经可以跟java互通了,官网的代码没有更新,很多朋友可能没发现,百度搜sm4的加解密也发现跟java的对不上,我在这里贴一下,想要源代码的可以在git搜cnvcl的项目</p>
<p>uses&nbsp;</p>
<p>&nbsp;&nbsp;CnSM4,CnBase64</p>
<p>//PKCS7填充</p>
<p>{* 给字符串末尾加上 PKCS7 规定的填充“几个几”的填充数据}<br>function StrAddPKCS7Padding(const Str: AnsiString; BlockSize: Byte): AnsiString;<br>var<br>&nbsp; R: Byte;<br>begin<br>&nbsp; R := Length(Str) mod BlockSize;<br>&nbsp; R := BlockSize - R;<br>&nbsp; if R = 0 then<br>&nbsp; R := R + BlockSize;</p>
<p>&nbsp; Result := Str + AnsiString(StringOfChar(Chr(R), R));<br>end;<br>{* 去除 PKCS7 规定的字符串末尾填充“几个几”的填充数据}<br>function StrRemovePKCS7Padding(const Str: AnsiString): AnsiString;<br>var<br>&nbsp; L: Integer;<br>&nbsp; V: Byte;<br>begin<br>&nbsp; Result := Str;<br>&nbsp; if Result = '' then<br>&nbsp; Exit;<br>&nbsp; L := Length(Result);<br>&nbsp; V := Ord(Result);// 末是几表示加了几</p>
<p>&nbsp; if V &lt;= L then<br>&nbsp; Delete(Result, L - V + 1, V);<br>end;</p>
<p>&nbsp;</p>
<p>//加密</p>
<p>function Sm4Encode(Value,key:Ansistring):AnsiString;</p>
<p><em id="__mceDel">var<br>&nbsp; Output: AnsiString;<br>&nbsp; Len: Integer;<br>&nbsp; s: string;<br>&nbsp; BaseByte:Byte;<br>&nbsp; begin<br>&nbsp; Result := '';<br>&nbsp; BaseByte := 200;<br>&nbsp; try<br>&nbsp; &nbsp; if Value = '' then Exit;<br>&nbsp; &nbsp; Len := Length(UTF8Encode(Value));<br>&nbsp; &nbsp; if Len &lt; 16 then<br>&nbsp; &nbsp; &nbsp; Len := 16<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Len := (((Len - 1) div 16) + 1) * 16;<br>&nbsp; &nbsp; SetLength(Output, Len);<br>&nbsp; &nbsp; ZeroMemory(@(Output), Len);<br>&nbsp; &nbsp; SM4EncryptEcbStr(UTF8Encode(key), StrAddPKCS7Padding(UTF8Encode(Value),SM4_BLOCKSIZE), @(Output));<br>&nbsp; &nbsp; BaseByte := Base64Encode(Output,s);<br>&nbsp; &nbsp; if BaseByte &lt;&gt; 0 then Exit;<br>&nbsp; &nbsp; Result := s;<br>&nbsp; finally<br>&nbsp; &nbsp; if Result = '' then<br>&nbsp; &nbsp; &nbsp; raise Exception.Create('SM4Encode Error!Base64:'+inttostr(BaseByte));<br>&nbsp; end;<br>end;</em></p>
<p>//解密</p>
<p>function Sm4Decode(Value,key:Ansistring):AnsiString;<br>var<br>&nbsp; S: AnsiString;<br>&nbsp; Output: AnsiString;<br>&nbsp; Len: Integer;<br>&nbsp; BaseByte :Byte;<br>begin<br>&nbsp; Result := '';<br>&nbsp; BaseByte := 200;<br>&nbsp; try<br>&nbsp; &nbsp; if Value = '' then Exit;<br>&nbsp; &nbsp; BaseByte := Base64Decode(AnsiString(Value),s);<br>&nbsp; &nbsp; if BaseByte &lt;&gt; 0then Exit;<br>&nbsp; &nbsp; Len := Length(S);<br>&nbsp; &nbsp; if Len &lt; 16 then<br>&nbsp; &nbsp; &nbsp; Len := 16<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Len := (((Len - 1) div 16) + 1) * 16;<br>&nbsp; &nbsp; SetLength(Output, Len);<br>&nbsp; &nbsp; ZeroMemory(@(Output), Len);<br>&nbsp; &nbsp; SM4DecryptEcbStr(UTF8Encode(key), S, @(Output));<br>&nbsp; &nbsp; Output := StrRemovePKCS7Padding(Output);<br>&nbsp; &nbsp; Result := UTF8Decode(Output);<br>&nbsp; finally<br>&nbsp; &nbsp; if Result = '' then<br>&nbsp; &nbsp; &nbsp; raise Exception.Create('SM4Decode Error!Base64:'+inttostr(BaseByte));<br>&nbsp; end;<br>end;</p><br><br>
来源:https://www.cnblogs.com/Yang-YaChao/p/16351961.html
頁: [1]
查看完整版本: DELPHI国密SM4加密解密-可与JAVA互通