Created
August 16, 2011 14:46
-
-
Save scotttam/1149256 to your computer and use it in GitHub Desktop.
Javascript example generating a PBKDF2 key and encrypting/decrypting using AES/256/CBC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Requires: | |
//JQuery | |
//slowAES (http://code.google.com/p/slowaes/) | |
//pbkdf2 (1.1) by Parvez Anandam (http://anandam.name/pbkdf2) | |
//sha1 (2.1a) by Paul Johnston (http://pajhome.org.uk/crypt/md5) | |
(function(window, document, $, undefined){ | |
$(document).ready(function() { | |
var startTime = new Date(); | |
var keySizeInBits = slowAES.aes.keySize.SIZE_256; | |
var keySizeInBytes = keySizeInBits/8; | |
var mode = slowAES.modeOfOperation.CBC; | |
var iv = "1234567890123456"; | |
var iterations = 2048; | |
var derivedKey = null; | |
var mypbkdf2 = new PBKDF2("THE_DUMMY_PASSWORD_IS_RHUBARB", iv, iterations, keySizeInBytes); | |
var result_callback = function(key) { | |
console.log("Callback"); | |
derivedKey = key; | |
console.log("The key " + derivedKey); | |
var encryptedByteArray = encryptString("HITHERE", derivedKey, iv); | |
console.log("The encrypted string " + encryptedByteArray); | |
var decryptedString = decryptString(encryptedByteArray, derivedKey, iv); | |
console.log("The decrypted string " + decryptedString); | |
}; | |
mypbkdf2.deriveKey(function(){}, result_callback); | |
var endTime = new Date(); | |
console.log("Time to encrypt/decrypt: " + (endTime - startTime)); | |
}); | |
function hexStringToByteArray(s) | |
{ | |
var r = Array(s.length/2); | |
for (var i = 0; i < s.length; i+=2) { | |
r[i/2] = parseInt(s.substr(i, 2), 16); | |
} | |
return r; | |
} | |
function encryptString(plainText, key, iv) | |
{ | |
var bytesToEncrypt = cryptoHelpers.convertStringToByteArray(plainText); | |
return encryptBytes(bytesToEncrypt, key, iv); | |
} | |
function decryptString(encryptedByteArray, key, iv) | |
{ | |
var bytes = decryptBytes(encryptedByteArray, key, iv); | |
var decryptedString = cryptoHelpers.convertByteArrayToString(bytes); | |
return decryptedString; | |
} | |
function encryptBytes(plainText, key, iv) | |
{ | |
var t = typeof plainText; | |
if (t == "string") { | |
plainText = hexStringToByteArray(plainText); | |
} | |
var result = slowAES.encrypt(plainText, slowAES.modeOfOperation.CBC, key, iv); | |
return result; | |
} | |
function decryptBytes(encryptedByteArray, key, iv) | |
{ | |
var result = slowAES.decrypt(encryptedByteArray, slowAES.modeOfOperation.CBC, key, iv); | |
return result; | |
} | |
})(window, document, $); |
Did you got the solution? because I'm also searching for the same.
hey! i have a pdf file that i need to open but i cant crack the damn password. is it by any chance u can help me?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just found out this piece of code after some long research on Google about implementing slowAES and pbkdf2.
However, I do still have a question: after I found out what are the values that I use on the C# Rijndael code, where do I use the configuration values [iterations = 1000] ?
Also, the C# version of the algorithm takes a salt and an encryptionPassword for generating the key. Any idea where and how or what do I have to modify?
Thanks.