Interop between Javascript "Fritz Schneider's Rijndael Reference Implementation" and .NET
I am using the javascript Fritz Schneider's Rijndael Reference Implementation. I want to decrypt the result in C#.
The input variables provided in Javascript Rijndael function are
function rijndaelEncrypt(plaintext, key, mode) {}
where plaintext is the text to be encrypted, key is the encryption key, and mode selects either ECB or CBC. The mode we are using is ECB.
Now, for Microsoft Rijndael implementation, what should I use for the Initialization Vector to decrypt this?
For the record, the whole javascript encryption goes like this.
function rijndaelEncrypt(plaintext, key, mode) {
var i, aBlock;
var bpb = blockSizeInBits / 8; // bytes per block
var ct; // ciphertext
if (!plaintext || !key)
return;
if (key.length*8 != keySizeInBits)
return;
if (mode == "CBC")
ct = getRandomBytes(bpb); // get IV
else {
mode = "ECB";
ct = new Array();
}
// convert plaintext to byte array and pad with zeros if necessary.
plaintext = formatPlaintext(plaintext);
var expandedKey = new keyExpansion(key);
for (var block=0; block<plaintext.le开发者_JS百科ngth / bpb; block++) {
aBlock = plaintext.slice(block*bpb, (block+1)*bpb);
if (mode == "CBC")
for (var i=0; i<bpb; i++)
aBlock[i] ^= ct[block*bpb + i];
ct = ct.concat(AESencrypt(aBlock, expandedKey));
}
return ct;
}
Thanks. Help much appreciated.
First, ECB mode has no initialization vector. I think you can pass null, an empty array or an array filled by 0 to your RijndaelManaged object.
For your CBC mode implementation here, the initialization vector is the first 128-bit block of the ciphertext array.
if (mode == "CBC")
ct = getRandomBytes(bpb); // get IV
These are filled with random bytes on encryption, and read from here on decryption.
I'm not sure if RijndaelManaged will output the initialization vector for you, simply have a look at the output size. If not, add it yourself.
精彩评论