Skip to content

Instantly share code, notes, and snippets.

@scotttam
Created August 16, 2011 14:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotttam/1149256 to your computer and use it in GitHub Desktop.
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
//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, $);
@robertstefan
Copy link

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?

private static RijndaelManaged GetAlgorithm(string encryptionPassword) {
            // Create an encryption key from the encryptionPassword and salt.
            var key = new Rfc2898DeriveBytes(encryptionPassword, Salt);

            // Declare that we are going to use the Rijndael algorithm with the key that we've just got.
            var algorithm = new RijndaelManaged();

            //encryptionKeySize = 256 / 8,
            int bytesForKey = algorithm.KeySize / 8;

            //encryptionBlockSize = 128 / 8
            int bytesForIV = algorithm.BlockSize / 8;

            algorithm.Key = key.GetBytes(bytesForKey);
            algorithm.IV = key.GetBytes(bytesForIV);
            return algorithm;
        }

Thanks.

@obaidjawad
Copy link

Did you got the solution? because I'm also searching for the same.

@flumhead
Copy link

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