本地引入
CryptoJS.min.js(网盘Tool下) <!--或者CDN-->
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>
// **************************** // JS 对称加密算法 let pwd = CryptoJS.enc.Utf8.parse("666666"); // 解析明文 let key = CryptoJS.enc.Utf8.parse("abcd1234"); // 解析秘钥 let iv = CryptoJS.enc.Utf8.parse("666666"); // 解析偏移向量, CBC模式下用到, ECB模式不用 // JS AES CBC模式 加密 let ciphertext = CryptoJS.AES.encrypt(pwd, key, { mode: CryptoJS.mode.CBC, // 加密模式 padding: CryptoJS.pad.Pkcs7, // 填充方式 iv: iv // 偏移向量 }).toString(); console.log(ciphertext) // ciphertext结果为:p0h2lUuOAh4tmEN7FTLq8w==, base64的形式 // JS AES CBC模式 解密, 必须为base64格式才能解密,如果为16进制,需要先转为base64 let key = CryptoJS.enc.Utf8.parse("abcd1234"); // 解析秘钥 let iv = CryptoJS.enc.Utf8.parse("666666"); // 解析偏移向量, CBC模式下用到, ECB模式不用 let ciphertexts = CryptoJS.AES.decrypt("p0h2lUuOAh4tmEN7FTLq8w==", key, { mode: CryptoJS.mode.CBC, // 加密模式 padding: CryptoJS.pad.Pkcs7, // 填充方式 iv: iv // 偏移向量 }).toString(CryptoJS.enc.Utf8); console.log(ciphertexts) // ****************************
// **************************** // JS DES ECB模式 加密 function encryptByDES(message, key){ let keyHex = CryptoJS.enc.Utf8.parse(key); let encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); } // JS DES ECB模式 解密 function decryptByDES(ciphertext, key){ let keyHex = CryptoJS.enc.Utf8.parse(key); let decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Hex.parse(ciphertext) }, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let result_value = decrypted.toString(CryptoJS.enc.Utf8); return result_value; } let message = \'18616563858\';//需要加密的数据 let key = \'pptv\';//加密key // JS DES 加密 desMessage = encryptByDES(message, key); console.log(desMessage); // JS DES 解密 message = decryptByDES(desMessage,key) console.log(message); // ****************************
// **************************** // JS ECB模式 加密 var key = "abcd1234" let ckey = CryptoJS.enc.Utf8.parse(key) let encrypted = CryptoJS.AES.encrypt("TFT2020@ASX", ckey, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let hexstr = encrypted.ciphertext.toString() console.log(hexstr); // 返回hex格式的密文 // PHP 解密 $mes = hex2bin("4b5bc2f487e7b081d1188beb4f0deb38"); $res = openssl_decrypt($mes, \'AES-128-ECB\', "1234567890123456", OPENSSL_RAW_DATA); echo $res; //输出admin // ****************************
// **************************** var key = "abcd1234" let string = "TFT2020SAC%&#" let ivstr = "123456" let KeyHex = CryptoJS.enc.Utf8.parse(key) let encrypted = CryptoJS.DES.encrypt(string, KeyHex, { mode: CryptoJS.mode.CBC, // ecb模式不需要偏移量 padding: CryptoJS.pad.Pkcs7, iv: CryptoJS.enc.Utf8.parse(ivstr) }); let hexstr = encrypted.ciphertext.toString() // 返回hex格式的密文 console.log(hexstr) // PHP解密 $mes = hex2bin(hexstr); $res = openssl_decrypt($mes, \'DES-CBC\', "abcd1234", OPENSSL_RAW_DATA, "123456"); echo $res; //输出TFT2020SAC%&# // ****************************
// **************************** // PHP AES加密 $key = \'1234567890123456\'; //密钥,前后端双方事先约定好 $iv = \'12345678\'; //偏移量,前后端双方事先约定好,ecb模式不需要此参数 $message = "TFT202@S$%A"; $aes = openssl_encrypt($message, \'AES-128-ECB\', $key, OPENSSL_RAW_DATA); $rs = bin2hex($aes); var_dump($rs); // JS AES解密 let ckey = CryptoJS.enc.Utf8.parse("1234567890123456"); let ciphertext = CryptoJS.enc.Hex.parse("b9e1cab6e983ca5f0cc2f85e3df8de3b"); let srcs = CryptoJS.enc.Base64.stringify(ciphertext); let decrypt = CryptoJS.AES.decrypt(srcs, ckey, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let decrypted = decrypt.toString(CryptoJS.enc.Utf8); console.log(decrypted.toString(CryptoJS.enc.Utf8)); // ****************************
// **************************** // PHP DES加密 $des = openssl_encrypt($message, \'DES-CBC\', $key, OPENSSL_RAW_DATA, $iv); $rs = bin2hex($des); var_dump($rs); // JS DES解密 let keyHex = CryptoJS.enc.Utf8.parse("1234567890123456") let decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Hex.parse("7e001ef45efe287715a7f826fd9c9d46") }, keyHex, { iv: CryptoJS.enc.Utf8.parse("12345678"), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); console.log(decrypted.toString(CryptoJS.enc.Utf8)) // ****************************
JS和Go通信加解密 DES_ECB
// **************************** // JS DES ECB模式 加密 function encryptByDES(message, key){ let keyHex = CryptoJS.enc.Utf8.parse(key); let encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(); } // JS DES ECB模式 解密 function decryptByDES(ciphertext, key){ let keyHex = CryptoJS.enc.Utf8.parse(key); let decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Hex.parse(ciphertext) }, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let result_value = decrypted.toString(CryptoJS.enc.Utf8); return result_value; } let message = \'18616563858\';//需要加密的数据 let key = \'TSuiSHio\';//加密key // JS DES 加密 desMessage = encryptByDES(message, key); console.log(desMessage); // JS DES 解密 message = decryptByDES(desMessage,key) message = decryptByDES("99DB561A3678B43352ECC40DC7519270", "TSuiSHio") console.log(message); // **************************** // Go DES ECB加密 func EncryptDES_ECB(src, key string) string { data := []byte(src) keyByte := []byte(key) block, err := des.NewCipher(keyByte) if err != nil { panic(err) } bs := block.BlockSize() //对明文数据进行补码 data = PKCS5Padding(data, bs) if len(data)%bs != 0 { panic("Need a multiple of the blocksize") } out := make([]byte, len(data)) dst := out for len(data) > 0 { //对明文按照blocksize进行分块加密 //必要时可以使用go关键字进行并行加密 block.Encrypt(dst, data[:bs]) data = data[bs:] dst = dst[bs:] } return fmt.Sprintf("%X", out) } // Go DES ECB解密 func DecryptDES_ECB(src, key string) string { data, err := hex.DecodeString(src) if err != nil { panic(err) } keyByte := []byte(key) block, err := des.NewCipher(keyByte) if err != nil { panic(err) } bs := block.BlockSize() if len(data)%bs != 0 { panic("crypto/cipher: input not full blocks") } out := make([]byte, len(data)) dst := out for len(data) > 0 { block.Decrypt(dst, data[:bs]) data = data[bs:] dst = dst[bs:] } out = PKCS5UnPadding(out) return string(out) } // ********************************************* //明文补码算法 func PKCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } //明文减码算法 func PKCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] } //=====调用===== //key的长度必须都是8位 key := "TSuiSHio" iv := "XadiousY" info := "TiTAouA@loRoOiu" Enc_str = EncryptDES_ECB(info, key) fmt.Println(Enc_str) Dec_str = DecryptDES_ECB(Enc_str, key) fmt.Println(Dec_str) Dec_str = DecryptDES_ECB("953c7eb7eb632a3f0e85f7f7cd3187a6", key) fmt.Println(Dec_str) //****************
Go AES DES加密
package main import ( "crypto/aes" "bytes" "encoding/base64" "crypto/cipher" "fmt" "crypto/des" "encoding/hex" ) func main() { var aeskey = []byte("321423u9y8d2fwfl") pass := []byte("vdncloud123456") xpass, err := AesEncrypt(pass, aeskey) if err != nil { fmt.Println(err) return } pass64 := base64.StdEncoding.EncodeToString(xpass) fmt.Printf("加密后:%v\n",pass64) bytesPass, err := base64.StdEncoding.DecodeString(pass64) if err != nil { fmt.Println(err) return } tpass, err := AesDecrypt(bytesPass, aeskey) if err != nil { fmt.Println(err) return } fmt.Printf("解密后:%s\n", tpass) //key的长度必须都是8位 key := "TSuiSHio" iv := "XadiousY" info := "TiTAouA@loRoOiu" Enc_str := EncryptDES_CBC(info, key, iv) fmt.Println(Enc_str) Dec_str := DecryptDES_CBC(Enc_str, key, iv) fmt.Println(Dec_str) Enc_str = EncryptDES_ECB(info, key) fmt.Println(Enc_str) Dec_str = DecryptDES_ECB(Enc_str, key) fmt.Println(Dec_str) Dec_str = DecryptDES_ECB("953c7eb7eb632a3f0e85f7f7cd3187a6", key) fmt.Println(Dec_str) } // DES CBC加密 func EncryptDES_CBC(src, key, iv string) string { data := []byte(src) keyByte := []byte(key) block, err := des.NewCipher(keyByte) if err != nil { panic(err) } data = PKCS5Padding(data, block.BlockSize()) //获取CBC加密模式 ivk := []byte(iv) mode := cipher.NewCBCEncrypter(block, ivk) out := make([]byte, len(data)) mode.CryptBlocks(out, data) return fmt.Sprintf("%X", out) } // DES CBC解密 func DecryptDES_CBC(src, key, iv string) string { keyByte := []byte(key) data, err := hex.DecodeString(src) if err != nil { panic(err) } block, err := des.NewCipher(keyByte) if err != nil { panic(err) } ivk := []byte(iv) mode := cipher.NewCBCDecrypter(block, ivk) plaintext := make([]byte, len(data)) mode.CryptBlocks(plaintext, data) plaintext = PKCS5UnPadding(plaintext) return string(plaintext) } // DES ECB加密 func EncryptDES_ECB(src, key string) string { data := []byte(src) keyByte := []byte(key) block, err := des.NewCipher(keyByte) if err != nil { panic(err) } bs := block.BlockSize() //对明文数据进行补码 data = PKCS5Padding(data, bs) if len(data)%bs != 0 { panic("Need a multiple of the blocksize") } out := make([]byte, len(data)) dst := out for len(data) > 0 { //对明文按照blocksize进行分块加密 //必要时可以使用go关键字进行并行加密 block.Encrypt(dst, data[:bs]) data = data[bs:] dst = dst[bs:] } return fmt.Sprintf("%X", out) } // DES ECB解密 func DecryptDES_ECB(src, key string) string { data, err := hex.DecodeString(src) if err != nil { panic(err) } keyByte := []byte(key) block, err := des.NewCipher(keyByte) if err != nil { panic(err) } bs := block.BlockSize() if len(data)%bs != 0 { panic("crypto/cipher: input not full blocks") } out := make([]byte, len(data)) dst := out for len(data) > 0 { block.Decrypt(dst, data[:bs]) data = data[bs:] dst = dst[bs:] } out = PKCS5UnPadding(out) return string(out) } // ********************************************* //明文补码算法 func PKCS5Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) } //明文减码算法 func PKCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] } // ********************************************* // AES 加密 func AesEncrypt(origData, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } blockSize := block.BlockSize() origData = PKCS5Padding(origData, blockSize) blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) crypted := make([]byte, len(origData)) blockMode.CryptBlocks(crypted, origData) return crypted, nil } // AES 解密 func AesDecrypt(crypted, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } blockSize := block.BlockSize() blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) origData := make([]byte, len(crypted)) blockMode.CryptBlocks(origData, crypted) origData = PKCS5UnPadding(origData) return origData, nil }
sha256(sha256.min.js网盘Tool下)
需要引入<script src=\'sha256.min.js\'></script>
// ********************* let param = { "acount": 100, "type": "1", "name": "linyifan", "bankCardNo": "6432323232323236563", } let paramStr = ksort(param) paramStr = JSON.stringify(paramStr) console.log(typeof paramStr) let secret = sha256(paramStr) param.sign = secret console.log(param) function ksort(o) { let sorted = {}, keys = Object.keys(o); keys.sort(); keys.forEach((key)=>{ sorted[key] = o[key]; }) return sorted; } // ********************* // ********************* // PHP解密 $data = [ "acount"=> 100, "type"=> "1", "name"=> "linyifan", "bankCardNo"=> "6432323232323236563", "sign"=> "", ]; $this->sha256_verify($data,$sign); public function sha256_verify($data,$sign){ unset($data[\'sign\']); ksort($data); $data = json_encode($data,JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES); print_r($data); print_r(hash("sha256", $data));die; if(hash("sha256",json_encode($data,JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES)) !== $sign){ $this->wrtlog(\'data\',json_encode($data,JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES)); $this->wrtlog(\'sha256\',hash("sha256",json_encode($data,JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES))); return false; } return true; } // *********************