Ledger Nano X - The secure hardware wallet
SHARE
TWEET

aes 256 cbc

krot Apr 22nd, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Code goes here
  2. var keySize = 256;
  3. var ivSize = 128;
  4. var iterations = 100;
  5.  
  6. var message = "Hello World";
  7. var password = "Secret Password";
  8.  
  9.  
  10. function encrypt (msg, pass) {
  11.   var salt = CryptoJS.lib.WordArray.random(128/8);
  12.  
  13.   var key = CryptoJS.PBKDF2(pass, salt, {
  14.       keySize: keySize/32,
  15.       iterations: iterations
  16.     });
  17.  
  18.   var iv = CryptoJS.lib.WordArray.random(128/8);
  19.  
  20.   var encrypted = CryptoJS.AES.encrypt(msg, key, {
  21.     iv: iv,
  22.     padding: CryptoJS.pad.Pkcs7,
  23.     mode: CryptoJS.mode.CBC
  24.    
  25.   });
  26.  
  27.   // salt, iv will be hex 32 in length
  28.   // append them to the ciphertext for use  in decryption
  29.   var transitmessage = salt.toString()+ iv.toString() + encrypted.toString();
  30.   return transitmessage;
  31. }
  32.  
  33. function decrypt (transitmessage, pass) {
  34.   var salt = CryptoJS.enc.Hex.parse(transitmessage.substr(0, 32));
  35.   var iv = CryptoJS.enc.Hex.parse(transitmessage.substr(32, 32))
  36.   var encrypted = transitmessage.substring(64);
  37.  
  38.   var key = CryptoJS.PBKDF2(pass, salt, {
  39.       keySize: keySize/32,
  40.       iterations: iterations
  41.     });
  42.  
  43.   var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
  44.     iv: iv,
  45.     padding: CryptoJS.pad.Pkcs7,
  46.     mode: CryptoJS.mode.CBC
  47.    
  48.   })
  49.   return decrypted;
  50. }
  51.  
  52. var encrypted = encrypt(message, password);
  53. var decrypted = decrypt(encrypted, password);
  54.  
  55. $('#encrypted').text("Encrypted: "+encrypted);
  56. $('#decrypted').text("Decrypted: "+ decrypted.toString(CryptoJS.enc.Utf8) );
  57.  
  58.  
  59.  
  60. <!DOCTYPE html>
  61. <html>
  62.  
  63.   <head>
  64.     <script src="https://code.jquery.com/jquery-3.1.1.min.js"
  65.   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  66.   crossorigin="anonymous"></script>
  67.    
  68.   <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
  69.   <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/pbkdf2.js"></script>
  70.   </head>
  71.  
  72.   <body>
  73.     <h1>CryptoJs AES Encryption/Decryption</h1>
  74.     <div id="encrypted"></div>
  75.     <div id="decrypted"></div>
  76.     <script src="script.js"></script>
  77.   </body>
  78.  
  79. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top