本文目錄一覽:
- 1、為什麼 CryptoJS DES 加密的結果和 Java DES 不一樣
- 2、怎麼把mysql資料庫中現有的數據進行des加密,加密後的結果和java中des加密的結果一樣。
- 3、微信小程序怎樣加密
- 4、關於騰訊雲簡訊介面的sig欄位,sha256加密問題,nodejs
為什麼 CryptoJS DES 加密的結果和 Java DES 不一樣
最近需要對數據進行加密/解密, 因此選用了CryptoJS庫, 對數據做DES演算法的加密/解密
首選查看官方示例, 將密文進行Base64編碼, 掉進一個大坑
script src=”htt p:/ /crypto-js.googlecod e.c om/svn/tags/3.1.2/build/rollups/tripledes.js”/script
script
var encrypted = CryptoJS.DES.encrypt(“Message”, “Secret Passphrase”);
// ciphertext changed every time you run it
// 加密的結果不應該每次都是一樣的嗎?
console.log(encrypted.toString(), encrypted.ciphertext.toString(CryptoJS.enc.Base64));
var decrypted = CryptoJS.DES.decrypt(encrypted, “Secret Passphrase”);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
/script
對這些加密演算法不了解, 只能求助Google
des encrypion: js encrypted value does not match the java encrypted value
In cryptoJS you have to convert the key to hex and useit as word just like above (otherwise it will be considered as passphrase)
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.
原來是我指定key的方式不對, 直接將字元串做為參數, 想當然的以為這就是key, 其實不然, CryptoJS會根據這個字元串算出真正的key和IV(各種新鮮名詞不解釋, 問我也沒用, 我也不懂 -_-“)
那麼我們只需要將key和iv對應的字元串轉成CryptoJS的WordArray類型, 在DES加密時做為參數傳入即可, 這樣對Message這個字元串加密, 每次得到的密文都是YOa3le0I+dI=
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
var ivHex = CryptoJS.enc.Utf8.parse(‘inputvec’);
var encrypted = CryptoJS.DES.encrypt(‘Message’, keyHex, { iv: ivHex });
這樣是不是就萬事OK了? 哪有, 誰知道這坑是一個接一個啊.
我們再試試Java這邊的DES加密是不是和這個結果一樣, 具體實現請參考Simple Java Class to DES Encrypt Strings
果真掉坑裡了, Java通過DES加密Message這個字元串得到的結果是8dKft9vkZ4I=和CryptoJS算出來的不一樣啊…親
繼續求助Google
C# and Java DES Encryption value are not identical
SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES.(JCA Doc)
This means that in the case of the SunJCE provider,
Cipher c1 = Cipher.getInstance(“DES/ECB/PKCS5Padding”);
and
Cipher c1 = Cipher.getInstance(“DES”);
are equivalent statements.
原來是CryptoJS進行DES加密時, 默認的模式和padding方式和Java默認的不一樣造成的, 必須使用ECB mode和PKCS5Padding, 但是CryptoJS中只有Pkcs7, 不管了, 試試看…
script src=”htt p:/ /crypto-js.googleco de.c om/svn/tags/3.1.2/build/rollups/tripledes.js”/script
script src=”ht tp:/ /crypto-js.googleco de.c om/svn/tags/3.1.2/build/components/mode-ecb.js”/script
script
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
var encrypted = CryptoJS.DES.encrypt(‘Message’, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log(encrypted.toString(), encrypted.ciphertext.toString(CryptoJS.enc.Base64));
/script
咦…使用Pkcs7能得到和Java DES一樣的結果了, 哇塞…好神奇
那我們試試統一Java也改成Cipher.getInstance(“DES/ECB/PKCS7Padding”)試試, 結果得到一個大大的錯誤
Error:java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES/ECB/PKCS7Padding
沒辦法, 繼續Google
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/ECB/PKCS7PADDING
I will point out that PKCS#5 and PKCS#7 actually specify exactly the same type of padding (they are the same!), but it’s called #5 when used in this context. :)
這位大俠給出的解釋是: PKCS#5和PKCS#7是一樣的padding方式, 對加密演算法一知半解, 我也只能暫且認可這個解釋了.
忙完了DES的加密, 接下來就是使用CryptoJS來解密了. 我們需要直接解密DES加密後的base64密文字元串. CryptoJS好像沒有提供直接解密DES密文字元串的方法啊, 他的整個加密/解密過程都是內部自己在玩, 解密時需要用到加密的結果對象, 這不是坑我嗎?
只好研究下CryptoJS DES加密後返回的對象, 發現有一個屬性ciphertext, 就是密文的WordArray, 那麼解密的時候, 我們是不是只要提供這個就行了呢?
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(‘8dKft9vkZ4I=’)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
果不其然, 到此為止, 問題全部解決, 豁然開朗…
完整代碼請參考CryptoJS-DES.html
Use CryptoJS encrypt message by DES and direct decrypt ciphertext, compatible with Java Cipher.getInstance(“DES”)
怎麼把mysql資料庫中現有的數據進行des加密,加密後的結果和java中des加密的結果一樣。
最近需要對數據進行加密/解密, 因此選用了CryptoJS庫, 對數據做DES演算法的加密/解密
首選查看官方示例, 將密文進行Base64編碼, 掉進一個大坑
script src=”htt p:/ /crypto-js.googlecod e.c om/svn/tags/3.1.2/build/rollups/tripledes.js”/script
script
var encrypted = CryptoJS.DES.encrypt(“Message”, “Secret Passphrase”);
// ciphertext changed every time you run it
// 加密的結果不應該每次都是一樣的嗎?
console.log(encrypted.toString(), encrypted.ciphertext.toString(CryptoJS.enc.Base64));
var decrypted = CryptoJS.DES.decrypt(encrypted, “Secret Passphrase”);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
/script
對這些加密演算法不了解, 只能求助Google
des encrypion: js encrypted value does not match the java encrypted value
In cryptoJS you have to convert the key to hex and useit as word just like above (otherwise it will be considered as passphrase)
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.
原來是我指定key的方式不對, 直接將字元串做為參數, 想當然的以為這就是key, 其實不然, CryptoJS會根據這個字元串算出真正的key和IV(各種新鮮名詞不解釋, 問我也沒用, 我也不懂 -_-“)
那麼我們只需要將key和iv對應的字元串轉成CryptoJS的WordArray類型, 在DES加密時做為參數傳入即可, 這樣對Message這個字元串加密, 每次得到的密文都是YOa3le0I+dI=
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
var ivHex = CryptoJS.enc.Utf8.parse(‘inputvec’);
var encrypted = CryptoJS.DES.encrypt(‘Message’, keyHex, { iv: ivHex });
這樣是不是就萬事OK了? 哪有, 誰知道這坑是一個接一個啊.
我們再試試Java這邊的DES加密是不是和這個結果一樣, 具體實現請參考Simple Java Class to DES Encrypt Strings
果真掉坑裡了, Java通過DES加密Message這個字元串得到的結果是8dKft9vkZ4I=和CryptoJS算出來的不一樣啊…親
繼續求助Google
C# and Java DES Encryption value are not identical
SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES.(JCA Doc)
This means that in the case of the SunJCE provider,
Cipher c1 = Cipher.getInstance(“DES/ECB/PKCS5Padding”);
and
Cipher c1 = Cipher.getInstance(“DES”);
are equivalent statements.
原來是CryptoJS進行DES加密時, 默認的模式和padding方式和Java默認的不一樣造成的, 必須使用ECB mode和PKCS5Padding, 但是CryptoJS中只有Pkcs7, 不管了, 試試看…
script src=”htt p:/ /crypto-js.googleco de.c om/svn/tags/3.1.2/build/rollups/tripledes.js”/script
script src=”ht tp:/ /crypto-js.googleco de.c om/svn/tags/3.1.2/build/components/mode-ecb.js”/script
script
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
var encrypted = CryptoJS.DES.encrypt(‘Message’, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log(encrypted.toString(), encrypted.ciphertext.toString(CryptoJS.enc.Base64));
/script
咦…使用Pkcs7能得到和Java DES一樣的結果了, 哇塞…好神奇
那我們試試統一Java也改成Cipher.getInstance(“DES/ECB/PKCS7Padding”)試試, 結果得到一個大大的錯誤
Error:java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES/ECB/PKCS7Padding
沒辦法, 繼續Google
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/ECB/PKCS7PADDING
I will point out that PKCS#5 and PKCS#7 actually specify exactly the same type of padding (they are the same!), but it’s called #5 when used in this context. :)
這位大俠給出的解釋是: PKCS#5和PKCS#7是一樣的padding方式, 對加密演算法一知半解, 我也只能暫且認可這個解釋了.
忙完了DES的加密, 接下來就是使用CryptoJS來解密了. 我們需要直接解密DES加密後的base64密文字元串. CryptoJS好像沒有提供直接解密DES密文字元串的方法啊, 他的整個加密/解密過程都是內部自己在玩, 解密時需要用到加密的結果對象, 這不是坑我嗎?
只好研究下CryptoJS DES加密後返回的對象, 發現有一個屬性ciphertext, 就是密文的WordArray, 那麼解密的時候, 我們是不是只要提供這個就行了呢?
var keyHex = CryptoJS.enc.Utf8.parse(‘abcd1234’);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(‘8dKft9vkZ4I=’)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
果不其然, 到此為止, 問題全部解決, 豁然開朗…
完整代碼請參考CryptoJS-DES.html
Use CryptoJS encrypt message by DES and direct decrypt ciphertext, compatible with Java Cipher.getInstance(“DES”)
微信小程序怎樣加密
1.下載一份Js版的aesUtil.js源碼。【注:文章末尾會貼出所有的相關類文件】
2.下載一份Js版的md
5.js源碼。
3.在pulic.js中進行加解密操作代碼如下,其中秘鑰和秘鑰偏移量要與後台的一致。var CryptoJS = require(‘aesUtil.js’); //引用…
4.在網路請求幫助類中進行參數的加密和返回數據的解密操作。var aes = require…
關於騰訊雲簡訊介面的sig欄位,sha256加密問題,nodejs
按照我下面的寫法哈:
const crypto = require(‘crypto’);
function sig()
{
var strMobile = “這裡是我的手機”; //tel的mobile欄位的內容
var strAppKey = “這裡是我的key”; //sdkappid對應的appkey,需要業務方高度保密
var strRand = “7226249334”; //url中的random欄位的值
var strTime = “1457336869”; //unix時間戳
var buf = “appkey=”+strAppKey+”random=”+strRand+”time=”
+strTime+”mobile=”+strMobile;
var sig = crypto.createHash(‘sha256’).update(buf, ‘utf-8’).digest(‘hex’);
return sig;
}
console.log(sig());
輸出結果如下:
7b1e97051886abdbd66c684530db2ba01644f828f31e06d624d88aff5469faa0
原因就是文檔裡面強調了編碼要使用 utf-8,而 js 默認編碼不是這個,所以需要強制指定下。
另外這個 sig 的計算結果是和你的輸出參數有關的,並不是不變的。
還有就是你的 mobile 和 appkey 怎麼傳遞的都是中文,官方給的貌似都是數字和字母呃。
原創文章,作者:0P7BB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129395.html