在 ECB 模式和 PKCS#5 填充中使用 Triple DES 使用 CryptoJS 进行加密
Encrypting with CryptoJS using Triple DES in ECB mode and PKCS#5 padding
我在发送给第三方 API 之前加密字符串,这需要在 ECB 模式下使用 PKCS#5 填充进行三重 DES 加密。他们给了我一个示例字符串来加密
www.society.com|1084470043000|memberaccess
和
的示例密钥
92f2f81930b5afb056630afb02f2f8b1
生成的加密字符串应该匹配
uQlYYov+XlVEHL0JBpvt15FeJxt+fXgjs9KLJHH5XsUd80emx5Nn5kSaCXxhQId/
但是我收到的字符串是
U2FsdGVkX19/IR4A9cm+duXf3vhnmnuyJtXFVWJi/Ptqib7g5gOANppYXh+mTQ8tcenO58a5FxnFVxRq1PUETA==
我正在使用这些库
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/crypto-js.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/tripledes.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/mode-ecb.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/pad-pkcs7.js"></script>
这是我的代码
var encrypted = CryptoJS.TripleDES.encrypt("www.society.com|1084470043000|memberaccess" , "92f2f81930b5afb056630afb02f2f8b1", {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
我意识到我指定的是 PKCS#7 而不是 PKCS#5,但我还没有找到 PKCS#5 的库。
我的问题是是否可以使用 CryptoJS 满足他们的要求,如果不能,是否还有其他可用的 javascript 解决方案?
我的fiddle
https://jsfiddle.net/dwc6zb89/2/
我没有花足够的时间在密码学上给你一个关于它是否是 base64 的好答案。我相信是这样,因为我有一段我编写的 c# 代码可以产生几乎正确的结果。少了几个字符。我将继续比较两者,看看我的 C# 代码在做什么,而 javascript 不是。
public static string Encrypt(string toEncrypt, bool useHashing)
{
var key = "92f2f81930b5afb056630afb02f2f8b1";
var iso8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
var keyArray = Convert.FromBase64String(iso8859_1.GetString(iso8859_1.GetBytes(key)));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//Always release the resources and flush data
// of the Cryptographic service provide. Best Practice
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
更新:
我觉得我快疯了。通过使用以下
转换我的密钥
var encryptedKey = CryptoJS.enc.Base64.parse(key);
我能够生成一个加密值,它与供应商给我的样本只有几个字符。这是我现在的 fiddle.
https://jsfiddle.net/uv8z2mrc/1/
更新:
我有两个问题。
我需要用 base 64 加密我的密钥,然后才能像这样使用它。
var encryptedKey = CryptoJS.enc.Base64.parse(key);
供应商文档有误,要加密的值应该使用 .org 地址而不是 .com。 GRRR!
来自 CryptoJS 文档:
For the key, when you pass a string, it's treated as a passphrase and used to derive an actual key and IV. Or you can pass a WordArray that represents the actual key. If you pass the actual key, you must also pass the actual IV.
您应该将密钥十六进制解码为字数组,并将 16 字节的 IV 设置为全零字节。 ECB 不使用 IV,但请指定它 none-the-less。否则密钥将从字符串派生,这显然会导致不正确的结果。
我在发送给第三方 API 之前加密字符串,这需要在 ECB 模式下使用 PKCS#5 填充进行三重 DES 加密。他们给了我一个示例字符串来加密
www.society.com|1084470043000|memberaccess
和
的示例密钥92f2f81930b5afb056630afb02f2f8b1
生成的加密字符串应该匹配
uQlYYov+XlVEHL0JBpvt15FeJxt+fXgjs9KLJHH5XsUd80emx5Nn5kSaCXxhQId/
但是我收到的字符串是
U2FsdGVkX19/IR4A9cm+duXf3vhnmnuyJtXFVWJi/Ptqib7g5gOANppYXh+mTQ8tcenO58a5FxnFVxRq1PUETA==
我正在使用这些库
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/crypto-js.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/tripledes.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/mode-ecb.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9/pad-pkcs7.js"></script>
这是我的代码
var encrypted = CryptoJS.TripleDES.encrypt("www.society.com|1084470043000|memberaccess" , "92f2f81930b5afb056630afb02f2f8b1", {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
我意识到我指定的是 PKCS#7 而不是 PKCS#5,但我还没有找到 PKCS#5 的库。
我的问题是是否可以使用 CryptoJS 满足他们的要求,如果不能,是否还有其他可用的 javascript 解决方案?
我的fiddle
https://jsfiddle.net/dwc6zb89/2/
我没有花足够的时间在密码学上给你一个关于它是否是 base64 的好答案。我相信是这样,因为我有一段我编写的 c# 代码可以产生几乎正确的结果。少了几个字符。我将继续比较两者,看看我的 C# 代码在做什么,而 javascript 不是。
public static string Encrypt(string toEncrypt, bool useHashing)
{
var key = "92f2f81930b5afb056630afb02f2f8b1";
var iso8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
var keyArray = Convert.FromBase64String(iso8859_1.GetString(iso8859_1.GetBytes(key)));
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
//Always release the resources and flush data
// of the Cryptographic service provide. Best Practice
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
//set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
//mode of operation. there are other 4 modes.
//We choose ECB(Electronic code Book)
tdes.Mode = CipherMode.ECB;
//padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
//transform the specified region of bytes array to resultArray
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
//Release resources held by TripleDes Encryptor
tdes.Clear();
//Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
更新:
我觉得我快疯了。通过使用以下
转换我的密钥var encryptedKey = CryptoJS.enc.Base64.parse(key);
我能够生成一个加密值,它与供应商给我的样本只有几个字符。这是我现在的 fiddle.
https://jsfiddle.net/uv8z2mrc/1/
更新:
我有两个问题。
我需要用 base 64 加密我的密钥,然后才能像这样使用它。
var encryptedKey = CryptoJS.enc.Base64.parse(key);
供应商文档有误,要加密的值应该使用 .org 地址而不是 .com。 GRRR!
来自 CryptoJS 文档:
For the key, when you pass a string, it's treated as a passphrase and used to derive an actual key and IV. Or you can pass a WordArray that represents the actual key. If you pass the actual key, you must also pass the actual IV.
您应该将密钥十六进制解码为字数组,并将 16 字节的 IV 设置为全零字节。 ECB 不使用 IV,但请指定它 none-the-less。否则密钥将从字符串派生,这显然会导致不正确的结果。