Block Modes and Padding
```
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", { mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.AnsiX923 });
```
CryptoJS supports the following modes:
- CBC (the default)
- CFB
- CTR
- OFB
- ECB
And CryptoJS supports the following padding schemes:
- Pkcs7 (the default)
- Iso97971
- AnsiX923
- Iso10126
- ZeroPadding
- NoPadding
The Cipher Input
For the plaintext message, the cipher algorithms accept either strings or instances of CryptoJS.lib.WordArray.
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.
For the ciphertext, the cipher algorithms accept either strings or instances of CryptoJS.lib.CipherParams. A CipherParams object represents a collection of parameters such as the IV, a salt, and the raw ciphertext itself. When you pass a string, it's automatically converted to a CipherParams object according to a configurable format strategy.
The Cipher Output
The plaintext you get back after decryption is a WordArray object. See Hashers' Output for more detail.
The ciphertext you get back after encryption isn't a string yet. It's a CipherParams object. A CipherParams object gives you access to all the parameters used during encryption. When you use a CipherParams object in a string context, it's automatically converted to a string according to a format strategy. The default is an OpenSSL-compatible format.
```
var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); alert(encrypted.key); // 74eb593087a982e2a6f5dded54ecd96d1fd0f3d44a58728cdcd40c55227522223 alert(encrypted.iv); // 7781157e2629b094f0e3dd48c4d786115 alert(encrypted.salt); // 7a25f9132ec6a8b34 alert(encrypted.ciphertext); // 73e54154a15d1beeb509d9e12f1e462a0 alert(encrypted); // U2FsdGVkX1+iX5Ey7GqLND5UFUoV0b7rUJ2eEvHkYqA=
```
You can define your own formats in order to be compatible with other crypto implementations. A format is an object with two methods—stringify and parse—that converts between CipherParams objects and ciphertext strings.
Here's how you might write a JSON formatter:
```
var JsonFormatter = { stringify: function (cipherParams) { // create json object with ciphertext var jsonObj = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) }; // optionally add iv and salt if (cipherParams.iv) { jsonObj.iv = cipherParams.iv.toString(); } if (cipherParams.salt) { jsonObj.s = cipherParams.salt.toString(); } // stringify json object return JSON.stringify(jsonObj); }, parse: function (jsonStr) { // parse json string var jsonObj = JSON.parse(jsonStr); // extract ciphertext from json object, and create cipher params object var cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct) }); // optionally extract iv and salt if (jsonObj.iv) { cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv) } if (jsonObj.s) { cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s) } return cipherParams; } }; var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", { format: JsonFormatter }); alert(encrypted); // {"ct":"tZ4MsEnfbcDOwqau68aOrQ==","iv":"8a8c8fd8fe33743d3638737ea4a00698","s":"ba06373c8f57179c"} var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase", { format: JsonFormatter }); alert(decrypted.toString(CryptoJS.enc.Utf8)); // Message
```
Progressive Ciphering
```
var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'); var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f'); var aesEncryptor = CryptoJS.algo.AES.createEncryptor(key, { iv: iv }); var ciphertextPart1 = aesEncryptor.process("Message Part 1"); var ciphertextPart2 = aesEncryptor.process("Message Part 2"); var ciphertextPart3 = aesEncryptor.process("Message Part 3"); var ciphertextPart4 = aesEncryptor.finalize(); var aesDecryptor = CryptoJS.algo.AES.createDecryptor(key, { iv: iv }); var plaintextPart1 = aesDecryptor.process(ciphertextPart1); var plaintextPart2 = aesDecryptor.process(ciphertextPart2); var plaintextPart3 = aesDecryptor.process(ciphertextPart3); var plaintextPart4 = aesDecryptor.process(ciphertextPart4); var plaintextPart5 = aesDecryptor.finalize();
```
Interoperability
With OpenSSL
Encrypt with OpenSSL:
openssl enc -aes-256-cbc -in infile -out outfile -pass pass:"Secret Passphrase" -e -base64
Decrypt with CryptoJS:
```
var decrypted = CryptoJS.AES.decrypt(openSSLEncrypted, "Secret Passphrase");
```
Encoders
CryptoJS can convert from encoding formats such as Base64, Latin1 or Hex to WordArray objects and vica versa.
```
var words = CryptoJS.enc.Base64.parse('SGVsbG8sIFdvcmxkIQ=='); var base64 = CryptoJS.enc.Base64.stringify(words); var words = CryptoJS.enc.Latin1.parse('Hello, World!'); var latin1 = CryptoJS.enc.Latin1.stringify(words); var words = CryptoJS.enc.Hex.parse('48656c6c6f2c20576f726c6421'); var hex = CryptoJS.enc.Hex.stringify(words); var words = CryptoJS.enc.Utf8.parse('?'); var utf8 = CryptoJS.enc.Utf8.stringify(words); var words = CryptoJS.enc.Utf16.parse('Hello, World!'); var utf16 = CryptoJS.enc.Utf16.stringify(words); var words = CryptoJS.enc.Utf16LE.parse('Hello, World!'); var utf16 = CryptoJS.enc.Utf16LE.stringify(words);
版權聲明:本文為CSDN部落客「weixin_33770878」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/weixin_33770878/article/details/92434768