AI销管
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

QuickStartGuide.wiki 17 KiB

il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <wiki:toc/>
  2. ----
  3. = Quick-start Guide =
  4. == Hashers ==
  5. === The Hasher Algorithms ===
  6. ==== MD5 ====
  7. MD5 is a widely used hash function. It's been used in a variety of security applications and is also commonly used to check the integrity of files. Though, MD5 is not collision resistant, and it isn't suitable for applications like SSL certificates or digital signatures that rely on this property.
  8. {{{
  9. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/md5.js"></script>
  10. <script>
  11. var hash = CryptoJS.MD5("Message");
  12. </script>
  13. }}}
  14. ==== SHA-1 ====
  15. The SHA hash functions were designed by the National Security Agency (NSA). SHA-1 is the most established of the existing SHA hash functions, and it's used in a variety of security applications and protocols. Though, SHA-1's collision resistance has been weakening as new attacks are discovered or improved.
  16. {{{
  17. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha1.js"></script>
  18. <script>
  19. var hash = CryptoJS.SHA1("Message");
  20. </script>
  21. }}}
  22. ==== SHA-2 ====
  23. SHA-256 is one of the four variants in the SHA-2 set. It isn't as widely used as SHA-1, though it appears to provide much better security.
  24. {{{
  25. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha256.js"></script>
  26. <script>
  27. var hash = CryptoJS.SHA256("Message");
  28. </script>
  29. }}}
  30. SHA-512 is largely identical to SHA-256 but operates on 64-bit words rather than 32.
  31. {{{
  32. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha512.js"></script>
  33. <script>
  34. var hash = CryptoJS.SHA512("Message");
  35. </script>
  36. }}}
  37. CryptoJS also supports SHA-224 and SHA-384, which are largely identical but truncated versions of SHA-256 and SHA-512 respectively.
  38. ==== SHA-3 ====
  39. SHA-3 is the winner of a five-year competition to select a new cryptographic hash algorithm where 64 competing designs were evaluated.
  40. {{{
  41. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha3.js"></script>
  42. <script>
  43. var hash = CryptoJS.SHA3("Message");
  44. </script>
  45. }}}
  46. SHA-3 can be configured to output hash lengths of one of 224, 256, 384, or 512 bits. The default is 512 bits.
  47. {{{
  48. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha3.js"></script>
  49. <script>
  50. var hash = CryptoJS.SHA3("Message", { outputLength: 512 });
  51. var hash = CryptoJS.SHA3("Message", { outputLength: 384 });
  52. var hash = CryptoJS.SHA3("Message", { outputLength: 256 });
  53. var hash = CryptoJS.SHA3("Message", { outputLength: 224 });
  54. </script>
  55. }}}
  56. ==== RIPEMD-160 ====
  57. {{{
  58. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/ripemd160.js"></script>
  59. <script>
  60. var hash = CryptoJS.RIPEMD160("Message");
  61. </script>
  62. }}}
  63. === The Hasher Input ===
  64. The hash algorithms accept either strings or instances of CryptoJS.lib.WordArray. A WordArray object represents an array of 32-bit words. When you pass a string, it's automatically converted to a WordArray encoded as UTF-8.
  65. === The Hasher Output ===
  66. The hash you get back isn't a string yet. It's a WordArray object. When you use a WordArray object in a string context, it's automatically converted to a hex string.
  67. {{{
  68. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha256.js"></script>
  69. <script>
  70. var hash = CryptoJS.SHA256("Message");
  71. alert(typeof hash); // object
  72. alert(hash); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
  73. </script>
  74. }}}
  75. You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.
  76. {{{
  77. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha256.js"></script>
  78. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/enc-base64-min.js"></script>
  79. <script>
  80. var hash = CryptoJS.SHA256("Message");
  81. alert(hash.toString(CryptoJS.enc.Base64)); // L3dmip37+NWEi57rSnFFypTG7ZI25Kdz9tyvpRMrL5E=
  82. alert(hash.toString(CryptoJS.enc.Latin1)); // /wf��ûøÕ���ëJqEÊ�Æí�6ä§söܯ¥+/�
  83. alert(hash.toString(CryptoJS.enc.Hex)); // 2f77668a9dfbf8d5848b9eeb4a7145ca94c6ed9236e4a773f6dcafa5132b2f91
  84. </script>
  85. }}}
  86. === Progressive Hashing ===
  87. {{{
  88. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/sha256.js"></script>
  89. <script>
  90. var sha256 = CryptoJS.algo.SHA256.create();
  91. sha256.update("Message Part 1");
  92. sha256.update("Message Part 2");
  93. sha256.update("Message Part 3");
  94. var hash = sha256.finalize();
  95. </script>
  96. }}}
  97. == HMAC ==
  98. Keyed-hash message authentication codes (HMAC) is a mechanism for message authentication using cryptographic hash functions.
  99. HMAC can be used in combination with any iterated cryptographic hash function.
  100. {{{
  101. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/hmac-md5.js"></script>
  102. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/hmac-sha1.js"></script>
  103. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/hmac-sha256.js"></script>
  104. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/hmac-sha512.js"></script>
  105. <script>
  106. var hash = CryptoJS.HmacMD5("Message", "Secret Passphrase");
  107. var hash = CryptoJS.HmacSHA1("Message", "Secret Passphrase");
  108. var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");
  109. var hash = CryptoJS.HmacSHA512("Message", "Secret Passphrase");
  110. </script>
  111. }}}
  112. === Progressive HMAC Hashing ===
  113. {{{
  114. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/hmac-sha256.js"></script>
  115. <script>
  116. var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, "Secret Passphrase");
  117. hmac.update("Message Part 1");
  118. hmac.update("Message Part 2");
  119. hmac.update("Message Part 3");
  120. var hash = hmac.finalize();
  121. </script>
  122. }}}
  123. == PBKDF2 ==
  124. PBKDF2 is a password-based key derivation function. In many applications of cryptography, user security is ultimately dependent on a password, and because a password usually can't be used directly as a cryptographic key, some processing is required.
  125. A salt provides a large set of keys for any given password, and an iteration count increases the cost of producing keys from a password, thereby also increasing the difficulty of attack.
  126. {{{
  127. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/pbkdf2.js"></script>
  128. <script>
  129. var salt = CryptoJS.lib.WordArray.random(128/8);
  130. var key128Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 128/32 });
  131. var key256Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 256/32 });
  132. var key512Bits = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 512/32 });
  133. var key512Bits1000Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 512/32, iterations: 1000 });
  134. </script>
  135. }}}
  136. == Ciphers ==
  137. === The Cipher Algorithms ===
  138. ==== AES ====
  139. The Advanced Encryption Standard (AES) is a U.S. Federal Information Processing Standard (FIPS). It was selected after a 5-year process where 15 competing designs were evaluated.
  140. {{{
  141. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  142. <script>
  143. var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
  144. var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
  145. </script>
  146. }}}
  147. CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.
  148. ==== DES, Triple DES ====
  149. DES is a previously dominant algorithm for encryption, and was published as an official Federal Information Processing Standard (FIPS). DES is now considered to be insecure due to the small key size.
  150. {{{
  151. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/tripledes.js"></script>
  152. <script>
  153. var encrypted = CryptoJS.DES.encrypt("Message", "Secret Passphrase");
  154. var decrypted = CryptoJS.DES.decrypt(encrypted, "Secret Passphrase");
  155. </script>
  156. }}}
  157. Triple DES applies DES three times to each block to increase the key size. The algorithm is believed to be secure in this form.
  158. {{{
  159. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/tripledes.js"></script>
  160. <script>
  161. var encrypted = CryptoJS.TripleDES.encrypt("Message", "Secret Passphrase");
  162. var decrypted = CryptoJS.TripleDES.decrypt(encrypted, "Secret Passphrase");
  163. </script>
  164. }}}
  165. ==== Rabbit ====
  166. Rabbit is a high-performance stream cipher and a finalist in the eSTREAM Portfolio. It is one of the four designs selected after a 3 1/2-year process where 22 designs were evaluated.
  167. {{{
  168. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/rabbit.js"></script>
  169. <script>
  170. var encrypted = CryptoJS.Rabbit.encrypt("Message", "Secret Passphrase");
  171. var decrypted = CryptoJS.Rabbit.decrypt(encrypted, "Secret Passphrase");
  172. </script>
  173. }}}
  174. ==== RC4, RC4Drop ====
  175. RC4 is a widely-used stream cipher. It's used in popular protocols such as SSL and WEP. Although remarkable for its simplicity and speed, the algorithm's history doesn't inspire confidence in its security.
  176. {{{
  177. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/rc4.js"></script>
  178. <script>
  179. var encrypted = CryptoJS.RC4.encrypt("Message", "Secret Passphrase");
  180. var decrypted = CryptoJS.RC4.decrypt(encrypted, "Secret Passphrase");
  181. </script>
  182. }}}
  183. It was discovered that the first few bytes of keystream are strongly non-random and leak information about the key. We can defend against this attack by discarding the initial portion of the keystream. This modified algorithm is traditionally called RC4-drop.
  184. By default, 192 words (768 bytes) are dropped, but you can configure the algorithm to drop any number of words.
  185. {{{
  186. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/rc4.js"></script>
  187. <script>
  188. var encrypted = CryptoJS.RC4Drop.encrypt("Message", "Secret Passphrase");
  189. var encrypted = CryptoJS.RC4Drop.encrypt("Message", "Secret Passphrase", { drop: 3072/4 });
  190. var decrypted = CryptoJS.RC4Drop.decrypt(encrypted, "Secret Passphrase", { drop: 3072/4 });
  191. </script>
  192. }}}
  193. === Custom Key and IV ===
  194. {{{
  195. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  196. <script>
  197. var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
  198. var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
  199. var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv });
  200. </script>
  201. }}}
  202. === Block Modes and Padding ===
  203. {{{
  204. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  205. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/mode-cfb-min.js"></script>
  206. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-ansix923-min.js"></script>
  207. <script>
  208. var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", { mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.AnsiX923 });
  209. </script>
  210. }}}
  211. CryptoJS supports the following modes:
  212. * CBC (the default)
  213. * CFB
  214. * CTR
  215. * OFB
  216. * ECB
  217. And CryptoJS supports the following padding schemes:
  218. * Pkcs7 (the default)
  219. * Iso97971
  220. * AnsiX923
  221. * Iso10126
  222. * ZeroPadding
  223. * NoPadding
  224. === The Cipher Input ===
  225. For the plaintext message, the cipher algorithms accept either strings or instances of CryptoJS.lib.WordArray.
  226. 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.
  227. 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.
  228. === The Cipher Output ===
  229. The plaintext you get back after decryption is a WordArray object. See Hashers' Output for more detail.
  230. 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.
  231. {{{
  232. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  233. <script>
  234. var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
  235. alert(encrypted.key); // 74eb593087a982e2a6f5dded54ecd96d1fd0f3d44a58728cdcd40c55227522223
  236. alert(encrypted.iv); // 7781157e2629b094f0e3dd48c4d786115
  237. alert(encrypted.salt); // 7a25f9132ec6a8b34
  238. alert(encrypted.ciphertext); // 73e54154a15d1beeb509d9e12f1e462a0
  239. alert(encrypted); // U2FsdGVkX1+iX5Ey7GqLND5UFUoV0b7rUJ2eEvHkYqA=
  240. </script>
  241. }}}
  242. 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.
  243. Here's how you might write a JSON formatter:
  244. {{{
  245. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  246. <script>
  247. var JsonFormatter = {
  248. stringify: function (cipherParams) {
  249. // create json object with ciphertext
  250. var jsonObj = {
  251. ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64)
  252. };
  253. // optionally add iv and salt
  254. if (cipherParams.iv) {
  255. jsonObj.iv = cipherParams.iv.toString();
  256. }
  257. if (cipherParams.salt) {
  258. jsonObj.s = cipherParams.salt.toString();
  259. }
  260. // stringify json object
  261. return JSON.stringify(jsonObj);
  262. },
  263. parse: function (jsonStr) {
  264. // parse json string
  265. var jsonObj = JSON.parse(jsonStr);
  266. // extract ciphertext from json object, and create cipher params object
  267. var cipherParams = CryptoJS.lib.CipherParams.create({
  268. ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
  269. });
  270. // optionally extract iv and salt
  271. if (jsonObj.iv) {
  272. cipherParams.iv = CryptoJS.enc.Hex.parse(jsonObj.iv)
  273. }
  274. if (jsonObj.s) {
  275. cipherParams.salt = CryptoJS.enc.Hex.parse(jsonObj.s)
  276. }
  277. return cipherParams;
  278. }
  279. };
  280. var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase", { format: JsonFormatter });
  281. alert(encrypted); // {"ct":"tZ4MsEnfbcDOwqau68aOrQ==","iv":"8a8c8fd8fe33743d3638737ea4a00698","s":"ba06373c8f57179c"}
  282. var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase", { format: JsonFormatter });
  283. alert(decrypted.toString(CryptoJS.enc.Utf8)); // Message
  284. </script>
  285. }}}
  286. === Progressive Ciphering ===
  287. {{{
  288. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  289. <script>
  290. var key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
  291. var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
  292. var aesEncryptor = CryptoJS.algo.AES.createEncryptor(key, { iv: iv });
  293. var ciphertextPart1 = aesEncryptor.process("Message Part 1");
  294. var ciphertextPart2 = aesEncryptor.process("Message Part 2");
  295. var ciphertextPart3 = aesEncryptor.process("Message Part 3");
  296. var ciphertextPart4 = aesEncryptor.finalize();
  297. var aesDecryptor = CryptoJS.algo.AES.createDecryptor(key, { iv: iv });
  298. var plaintextPart1 = aesDecryptor.process(ciphertextPart1);
  299. var plaintextPart2 = aesDecryptor.process(ciphertextPart2);
  300. var plaintextPart3 = aesDecryptor.process(ciphertextPart3);
  301. var plaintextPart4 = aesDecryptor.process(ciphertextPart4);
  302. var plaintextPart5 = aesDecryptor.finalize();
  303. </script>
  304. }}}
  305. === Interoperability ===
  306. ==== With OpenSSL ====
  307. Encrypt with OpenSSL:
  308. {{{
  309. openssl enc -aes-256-cbc -in infile -out outfile -pass pass:"Secret Passphrase" -e -base64
  310. }}}
  311. Decrypt with CryptoJS:
  312. {{{
  313. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/rollups/aes.js"></script>
  314. <script>
  315. var decrypted = CryptoJS.AES.decrypt(openSSLEncrypted, "Secret Passphrase");
  316. </script>
  317. }}}
  318. == Encoders ==
  319. CryptoJS can convert from encoding formats such as Base64, Latin1 or Hex to WordArray objects and vica versa.
  320. {{{
  321. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/core-min.js"></script>
  322. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/enc-utf16-min.js"></script>
  323. <script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/enc-base64-min.js"></script>
  324. <script>
  325. var words = CryptoJS.enc.Base64.parse('SGVsbG8sIFdvcmxkIQ==');
  326. var base64 = CryptoJS.enc.Base64.stringify(words);
  327. var words = CryptoJS.enc.Latin1.parse('Hello, World!');
  328. var latin1 = CryptoJS.enc.Latin1.stringify(words);
  329. var words = CryptoJS.enc.Hex.parse('48656c6c6f2c20576f726c6421');
  330. var hex = CryptoJS.enc.Hex.stringify(words);
  331. var words = CryptoJS.enc.Utf8.parse('𤭢');
  332. var utf8 = CryptoJS.enc.Utf8.stringify(words);
  333. var words = CryptoJS.enc.Utf16.parse('Hello, World!');
  334. var utf16 = CryptoJS.enc.Utf16.stringify(words);
  335. var words = CryptoJS.enc.Utf16LE.parse('Hello, World!');
  336. var utf16 = CryptoJS.enc.Utf16LE.stringify(words);
  337. </script>
  338. }}}