|
|
导航: |
论坛 -> DELPHI技术
斑竹:liumazi,sephil |
|
作者: |
|
2021/3/31 22:19:41 |
标题: |
delphi的AES和c#、网页加密的结果保持一致 |
浏览:3824 |
|
加入我的收藏 |
楼主: |
AES的加密模式ECB,填充 PKCS7
https://oktools.net/aes 秘钥:F23456781234567F 文本:abc 结果Base64:AdDzMAIpm5QuPvxesH9cxQ==
c# 秘钥:F23456781234567F 文本:abc 结果Base64:AdDzMAIpm5QuPvxesH9cxQ== c#代码 public static String Encrypt_AES(String str, String strAesKey) { Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(strAesKey); Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = System.Security.Cryptography.CipherMode.ECB; rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length); }
delphi的没搞定,请指教
----------------------------------------------
- |
作者: |
|
2021/3/31 23:49:04 |
1楼: |
https://stackoverflow.com/questions/63826701/aes-256-with-pkcs7-padding 不知道默认 tdes.KeySize = 256; tdes.BlockSize = 128; 给的多少? 老王的 在sf.net 。。名字太长记不住 自己搜论坛 cnvcl 去看看。 条件允许建议学php 调用 OpenSSL https://www.php.net/manual/en/function.openssl-encrypt.php
https://github.com/topics/pkcs7 居然还有这种关键词。。。 https://github.com/Delphier/TAES
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2021/4/1 8:58:37 |
2楼: |
谢谢,用这个搞定了。 https://github.com/Delphier/TAES
procedure TForm1.Button1Click(Sender: TObject); var OriginalText, Key, IV, EncryptedText: TBytes; begin OriginalText := TEncoding.ANSI.GetBytes('abc'); Key := TEncoding.ANSI.GetBytes('F23456781234567F'); // 256 bits-32 bytes IV := TEncoding.ANSI.GetBytes('1234567890123456'); // 16 bytes
EncryptedText := TAES.Encrypt(OriginalText, Key, 128, IV, cmECB, pmPKCS7); Memo1.Text := TNetEncoding.Base64.EncodeBytesToString(EncryptedText); end;
此帖子包含附件:
大小:17.9K |
----------------------------------------------
-
|
作者: |
|
2021/4/1 9:12:47 |
3楼: |
最开始打算用 FlyUtils.AES.ElAES ,加密出来的结果每次都变化,不知道哪里没设置对
----------------------------------------------
-
|
作者: |
|
2021/4/1 9:39:49 |
4楼: |
分组加密考虑的因素有很多:
1、每块的加密算法(DES/AES等) 2、数据分块的每块大小(128/192/256等) 3、块与块之间的运算模式(ECB/CBC等) 4、数据不满足整数块时如何填充(PKCS1/PKCS7等) 5、密文是否要转变成可读的(Hex/Base64等)
网上找的代码,但凡有一处和所需的不一样,加密出来的结果就对不上号。
----------------------------------------------
欢迎使用CnPack IDE Wizards http://www.cnpack.org/
|
作者: |
|
2021/4/1 10:28:18 |
5楼: |
AES 128 ECB PKCS7 Base64 是的
----------------------------------------------
-
|
作者: |
|
2021/4/1 13:11:25 |
6楼: |
较新版本的Delphi有Winapi.Security.Cryptography单元:
uses Winapi.Security.Cryptography, Winapi.WinRT, Winapi.CommonTypes, System.Win.WinRT;
procedure TForm1.Button1Click(Sender: TObject); var MsgHs: HString; AlgName: HString; //keyLength: UInt32; Encoding: BinaryStringEncoding; Key: Core_ICryptographicKey;
SrcBuff, KeyBuff: IBuffer; AES: Core_ISymmetricKeyAlgorithmProvider; KeyHs: HString; Rst: IBuffer; begin WindowsCreateString('abc', 3, MsgHs); WindowsCreateString('F23456781234567F', 16, KeyHs); AlgName := TCore_SymmetricAlgorithmNames.AesEcbPkcs7; AES := TCore_SymmetricKeyAlgorithmProvider.OpenAlgorithm(AlgName);
Encoding := BinaryStringEncoding.Utf8; KeyBuff := TCryptographicBuffer.ConvertStringToBinary(KeyHs, Encoding); SrcBuff := TCryptographicBuffer.ConvertStringToBinary(MsgHs, Encoding); Key := AES.CreateSymmetricKey(KeyBuff);
Rst := TCore_CryptographicEngine.Encrypt(Key, SrcBuff, nil); ShowMessage(TCryptographicBuffer.EncodeToBase64String(Rst).ToString); end;
输出结果为:AdDzMAIpm5QuPvxesH9cxQ==
----------------------------------------------
-
|
作者: |
glwang (glwang) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2021/4/1 15:57:00 |
7楼: |
MARK
----------------------------------------------
|
作者: |
|
2021/4/1 18:09:22 |
8楼: |
6楼的加密方便多了,利用系统自带功能。 照着写了一个解密过程
function AESDecrypt(aMsg, aKey: string): string; var MsgHs: HString; AlgName: HString; RstHs: UInt64; Encoding: BinaryStringEncoding; Key: Core_ICryptographicKey;
SrcBuff, KeyBuff: IBuffer; AES: Core_ISymmetricKeyAlgorithmProvider; KeyHs: HString; Rst: IBuffer; begin WindowsCreateString(PWideChar(aMsg), Length(aMsg), MsgHs); WindowsCreateString(PWideChar(aKey), Length(aKey), KeyHs); SrcBuff := TCryptographicBuffer.DecodeFromBase64String(MsgHs); AlgName := TCore_SymmetricAlgorithmNames.AesEcbPkcs7; AES := TCore_SymmetricKeyAlgorithmProvider.OpenAlgorithm(AlgName);
Encoding := BinaryStringEncoding.Utf8; KeyBuff := TCryptographicBuffer.ConvertStringToBinary(KeyHs, Encoding); Key := AES.CreateSymmetricKey(KeyBuff);
Rst := TCore_CryptographicEngine.Decrypt(Key, SrcBuff, nil); RstHs := TCryptographicBuffer.ConvertBinaryToString(Encoding, Rst); Result := WindowsGetStringRawBuffer(RstHs, 0); end;
----------------------------------------------
Delphi爱好者。
|
作者: |
|
2021/4/1 18:10:55 |
8楼: |
厉害 bluestorm8 (bluestorm)
----------------------------------------------
-
|
作者: |
|
2021/4/1 18:31:49 |
9楼: |
…… 被禁用帐号,帖子内容自动屏蔽! ……
----------------------------------------------
发布广告,禁用帐号!
|
作者: |
pwhll (渭水阳) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2021/4/1 21:33:22 |
10楼: |
还真不知道有这个单元:Winapi.Security.Cryptography,长经验了
----------------------------------------------
-
|
作者: |
|
2021/4/1 22:42:33 |
11楼: |
有空可以打开Winapi.Security.Cryptography.pas看一下,里面支持的加密类型还有很多很多。
----------------------------------------------
-
|
作者: |
mywyn (孤独的夜行者) |
★☆☆☆☆ |
-
|
普通会员 |
|
2021/4/2 8:58:56 |
12楼: |
6楼的用法在WIN下最方便,但不能跨平台
----------------------------------------------
-
|
作者: |
|
2021/4/2 12:11:52 |
13楼: |
限制确实比较多: Delphi好像是要求10.1+, OS要求Win8+,不能跨平台
----------------------------------------------
-
|
作者: |
|
2021/4/2 14:58:03 |
14楼: |
对操作系统要求高,Win7不支持
----------------------------------------------
-
|
作者: |
lsuper (lsuper) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2021/4/2 15:21:03 |
15楼: |
推荐一个独立实现:SecureBlackbox,能兼容 dotnet java 算法,网上有老版源码、支持所有平台
----------------------------------------------
-
|
作者: |
|
2021/4/2 15:27:58 |
16楼: |
花点工夫 openssl 可以 吃 win android linux .h 有翻译好的windows版不知道 android linux 能不能通用
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
mywyn (孤独的夜行者) |
★☆☆☆☆ |
-
|
普通会员 |
|
2021/4/3 16:38:21 |
17楼: |
找到了一个很简单的办法,用mORMot里的SynCrypto.pas,简单包含就行了,D7~10.4通吃。mORMot真是宝藏,不知道c5soft老大还在不在研究。
此帖子包含附件:
大小:17.4K |
----------------------------------------------
-
|
作者: |
|
2021/4/3 17:25:53 |
18楼: |
这个不错
----------------------------------------------
-
|
作者: |
|
2021/4/3 19:37:08 |
19楼: |
{$ifdef MSWINDOWS} PadLockLibHandle := LoadLibrary('LibPadlock'); {$else} // Linux: PadLockLibHandle := LoadLibrary('libvia_padlock.so'); if PadLockLibHandle=0 then PadLockLibHandle := LoadLibrary('libvia_padlock.so.1.0.0'); {$endif} padlock_phe_available := GetProcAddress(PadLockLibHandle,'padlock_phe_available'); padlock_phe_sha1 := GetProcAddress(PadLockLibHandle,'padlock_phe_sha1'); padlock_phe_sha256 := GetProcAddress(PadLockLibHandle,'padlock_phe_sha256'); padlock_ace_available := GetProcAddress(PadLockLibHandle,'padlock_ace_available'); padlock_aes_begin := GetProcAddress(PadLockLibHandle,'padlock_aes_begin'); padlock_aes_setkey := GetProcAddress(PadLockLibHandle,'padlock_aes_setkey'); padlock_aes_setmodeiv := GetProcAddress(PadLockLibHandle,'padlock_aes_setmodeiv'); padlock_aes_encrypt := GetProcAddress(PadLockLibHandle,'padlock_aes_encrypt'); padlock_aes_decrypt := GetProcAddress(PadLockLibHandle,'padlock_aes_decrypt'); padlock_aes_close := GetProcAddress(PadLockLibHandle,'padlock_aes_close'); https://blog.csdn.net/fengjingge/article/details/42193313 https://blog.csdn.net/XiaoXiaoLuZuo/article/details/17092519
https://www.cryptopp.com/cryptopp840.zip
http://www.logix.cz/michal/doc/article.xp/padlock-en https://www.cryptopp.com/wiki/VIA_Padlock https://github.com/ARMmbed/mbedtls/issues/28
https://openwrt.org/packages/pkgdata/libopenssl-padlock https://forum.ubuntu.org.cn/viewtopic.php?t=129112 https://opensuse.pkgs.org/15.2/opensuse-update-oss-x86_64/libopenssl1_0_0-32bit-1.0.2p-lp152.8.6.1.x86_64.rpm.html https://opensuse.pkgs.org/15.2/opensuse-update-oss-x86_64/libopenssl1_0_0-1.0.2p-lp152.8.9.1.x86_64.rpm.html https://centos.pkgs.org/7/centos-x86_64/openssl-libs-1.0.2k-19.el7.x86_64.rpm.html https://blog.csdn.net/weixin_34049032/article/details/92783384 - -真复杂
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2021/4/4 12:54:47 |
20楼: |
补cryptopp 用法介绍 https://www.cnblogs.com/liaocheng/p/4264719.html https://www.pressc.cn/144.html https://blog.csdn.net/qq_32261191/article/details/78855651 https://www.jianshu.com/p/201b2fcdc7ff
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
|