Advertisement
UrQuan

Kripto - 23.3.

Mar 23rd, 2016
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var crypto = require('crypto');
  2. const algorithm = 'aes-128-ecb';
  3. //const algorithm = 'aes-128-cbc';
  4.  
  5.  
  6. function encrypt(key, iv, plaintext) { // IV is ignored in ECB mode
  7.     var cipher = crypto.createCipheriv(algorithm, key, iv);    
  8.     cipher.setAutoPadding(false); // IMPORTANT: By default OpenSSL uses PKCS#7 padding.   (http://www.di-mgt.com.au/cryptopad.html)  
  9.     var ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);        
  10.     return ciphertext;
  11. }
  12.      
  13.  
  14. function decrypt(key, iv, ciphertext) {
  15.     var decipher = crypto.createDecipheriv(algorithm, key, iv);    
  16.     decipher.setAutoPadding(false); // IMPORTANT: By default OpenSSL uses PKCS#7 padding.    
  17.     var plaintext = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
  18.     return plaintext;
  19. }
  20.  
  21. var plaintext = new Buffer("OvoJeSkrivenTxt1OvoJeSkrivenTxt2", "utf8");
  22. console.log("Plaintext: ", plaintext.toString());
  23.  
  24. var salt = "Ovo je moja sol.";
  25. crypto.pbkdf2("hunter2", salt, 60000, 16, "sha256", function(err, key){
  26.     console.log("Key:       ", key);
  27.     var ciphertext = encrypt(key, iv, plaintext);
  28.     console.log("Encrypted: ", ciphertext);
  29.  
  30.     var decryptedtext = decrypt(key, iv, ciphertext);
  31.     console.log("Decrypted: ", decryptedtext.toString());
  32. });
  33. //var key = crypto.randomBytes(16);
  34.  
  35.  
  36. var iv = new Buffer("");
  37. //console.log("IV: ", iv);
  38.  
  39. /*
  40. console.log("Key:       ", key);
  41.  
  42.  
  43. var ciphertext = encrypt(key, iv, plaintext);
  44. console.log("Encrypted: ", ciphertext);
  45.  
  46. var decryptedtext = decrypt(key, iv, ciphertext);
  47. console.log("Decrypted: ", decryptedtext.toString());
  48. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement