2015-03-18 01:13:43
直接复制我整理好的
引用原文:https://gist.github.com/ufologist/5581486 但是做了一些修改。(CryptoJS.pad.ZeroPadding)
//cryptoJS
<script src="./tripledes.js"></script>
<script src="./mode-ecb-min.js"></script>
<script src="./pad-zeropadding-min.js"></script>
function encryptByDES(message, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding
});
return encrypted.toString();
}
function decryptByDES(ciphertext, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.ZeroPadding
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
var message = 't';
var key = '5e8487e6';
var ciphertext = encryptByDES(message, key);
// ciphertext: 8dKft9vkZ4I=
console.info('ciphertext:', ciphertext);
var plaintext = decryptByDES(ciphertext, key);
// plaintext : Message
console.info('plaintext :', plaintext);
引用原文:https://gist.github.com/cuixin/10612934 des.go 文件没做修改
与js一起使用的例子:
key := []byte("5e8487e6")
//解密,reply是从js中收到的加密字符串
fmt.Println("Received back from client: " + reply)
ddd, _ := base64.StdEncoding.DecodeString(reply)//js加密后的结果是base64的,要转成byte的。
destext, _ := DesDecrypt(ddd, key)
fmt.Println("获取解密结果:", string(destext))//拿到结果
//贴心tip: string->[]byte看这里 []byte("XXX")
//加密,接上面
outs, _ := DesEncrypt(destext, key)
dist := make([]byte, 2048) //开辟存储空间
base64.StdEncoding.Encode(dist, outs)
fmt.Println("加密送出:", string(dist))