1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import CryptoJS from 'crypto-js';
console.log("md5 加密:", CryptoJS.MD5("Message").toString(CryptoJS.enc.Hex)); console.log("md5 加密转base64:", CryptoJS.MD5("Message").toString(CryptoJS.enc.Base64));
console.log("SHA1 加密:", CryptoJS.SHA1("Message").toString(CryptoJS.enc.Hex));
console.log("SHA256 加密:", CryptoJS.SHA256("Message").toString(CryptoJS.enc.Hex)); console.log("SHA224 加密:", CryptoJS.SHA224("Message").toString(CryptoJS.enc.Hex)); console.log("SHA384 加密:", CryptoJS.SHA384("Message").toString(CryptoJS.enc.Hex));
console.log("SHA512 加密:", CryptoJS.SHA512("Message").toString(CryptoJS.enc.Hex));
console.log("SHA3 加密:", CryptoJS.SHA3("Message").toString(CryptoJS.enc.Hex));
console.log("SHA3 512加密:", CryptoJS.SHA3("Message", { outputLength: 512 }).toString(CryptoJS.enc.Hex)); console.log("SHA3 384加密:", CryptoJS.SHA3("Message", { outputLength: 384 }).toString(CryptoJS.enc.Hex)); console.log("SHA3 256加密:", CryptoJS.SHA3("Message", { outputLength: 256 }).toString(CryptoJS.enc.Hex)); console.log("SHA3 224加密:", CryptoJS.SHA3("Message", { outputLength: 224 }).toString(CryptoJS.enc.Hex));
console.log("HmacMD5 加密:", CryptoJS.HmacMD5("Message","Secret Passphrase").toString(CryptoJS.enc.Hex)); console.log("HmacSHA1 加密:", CryptoJS.HmacSHA1("Message","Secret Passphrase").toString(CryptoJS.enc.Hex)); console.log("HmacSHA256 加密:", CryptoJS.HmacSHA256("Message","Secret Passphrase").toString(CryptoJS.enc.Hex)); console.log("HmacSHA512 加密:", CryptoJS.HmacSHA512("Message","Secret Passphrase").toString(CryptoJS.enc.Hex));
var salt = CryptoJS.lib.WordArray.random(128 / 8); var key128Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 128 / 32 }); var key256Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 256 / 32 }); var key512Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 512 / 32 }); var key512Bits1000Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 512 / 32, iterations: 1000 }); console.log("PBKDF2 key128Bits加密:", key128Bits.toString(CryptoJS.enc.Hex)); console.log("PBKDF2 key256Bits 加密:", key256Bits.toString(CryptoJS.enc.Hex)); console.log("PBKDF2 key512Bits 加密:", key512Bits.toString(CryptoJS.enc.Hex)); console.log("PBKDF2 key512Bits1000Iterations 加密:", key512Bits1000Iterations.toString(CryptoJS.enc.Hex));
var encrypted = CryptoJS.AES.encrypt("Message", "111"); var decrypted = CryptoJS.AES.decrypt(encrypted.toString(), "111"); console.log("encrypted AES加密:", encrypted.toString()); console.log("encrypted AES解密:", decrypted.toString(CryptoJS.enc.Utf8));
var key = CryptoJS.enc.Utf8.parse("1234567891012345");
var iv = CryptoJS.enc.Utf8.parse("1234567891012345"); var encryptedIv = CryptoJS.AES.encrypt("Message", key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); var decryptedIv = CryptoJS.AES.decrypt(encryptedIv.toString(), key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); let aesEncodeBase64Str = encryptedIv.toString(); let aesEncodeHexStr = encryptedIv.ciphertext.toString(); console.log("encrypted AES参数BASE64加密:", aesEncodeBase64Str); console.log("encrypted AES参数Hex加密:", aesEncodeHexStr);
var decryptedIv2 = CryptoJS.AES.decrypt(CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(aesEncodeHexStr)), key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); console.log("encrypted AES向量解密1:", decryptedIv.toString(CryptoJS.enc.Utf8)); console.log("encrypted AES向量解密2:", decryptedIv2.toString(CryptoJS.enc.Utf8));
var encrypted1 = CryptoJS.DES.encrypt("Message", "Secret Passphrase"); var decrypted1 = CryptoJS.DES.decrypt(encrypted1.toString(), "Secret Passphrase"); console.log("encrypted DES加密:", encrypted1.toString()); console.log("encrypted DES解密:", decrypted1.toString(CryptoJS.enc.Utf8));
var encrypted2 = CryptoJS.TripleDES.encrypt("Message", "Secret Passphrase"); var decrypted2 = CryptoJS.TripleDES.decrypt(encrypted2, "Secret Passphrase"); console.log("encrypted TripleDES加密:", encrypted2.toString()); console.log("encrypted TripleDES解密:", decrypted2.toString(CryptoJS.enc.Utf8));
var encrypted3 = CryptoJS.Rabbit.encrypt("Message", "Secret Passphrase"); var decrypted3 = CryptoJS.Rabbit.decrypt(encrypted3, "Secret Passphrase"); console.log("encrypted Rabbit加密:", encrypted3.toString()); console.log("encrypted Rabbit解密:", decrypted3.toString(CryptoJS.enc.Utf8));
var encrypted4 = CryptoJS.RC4.encrypt("Message", "Secret Passphrase"); var decrypted4 = CryptoJS.RC4.decrypt(encrypted4, "Secret Passphrase"); console.log("encrypted RC4加密:", encrypted4.toString()); console.log("encrypted RC4解密:", decrypted4.toString(CryptoJS.enc.Utf8));
var encrypted5 = CryptoJS.RC4Drop.encrypt("Message", "Secret Passphrase", { drop: 3072 / 4 }); var decrypted5 = CryptoJS.RC4Drop.decrypt(encrypted5, "Secret Passphrase", { drop: 3072 / 4 }); console.log("encrypted RC4Drop加密:", encrypted5.toString()); console.log("encrypted RC4Drop解密:", decrypted5.toString(CryptoJS.enc.Utf8));
let base64en = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse("Message")); console.log("Base64加密:", base64en); const base64De = CryptoJS.enc.Base64.parse(base64en).toString(CryptoJS.enc.Utf8); console.log("Base64 解密:",base64De);
|