Open GoogleCodeExporter opened 8 years ago
I get the same result using both methods. Here is the code I ran.
<!doctype html>
<script
src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></scri
pt>
<script
src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ctr-mi
n.js"></script>
<script>
var K = CryptoJS.enc.Hex.parse('546c9ee039c8acf804405bf02970ee8b');
var IV = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
var text =
CryptoJS.enc.Hex.parse('2800000000e0ba377e00267777772e66343131366133306335386661
33666430363936323239366363313431362e636f6d00000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00');
var singlePart = CryptoJS.AES.encrypt(text, K, { mode: CryptoJS.mode.CTR, iv:
IV }).ciphertext.toString()
var aes = CryptoJS.algo.AES.createEncryptor(K, { mode: CryptoJS.mode.CTR, iv:
IV });
var multiPart1 = aes.process(text).toString();
var multiPart2 = aes.finalize().toString();
console.log(singlePart === multiPart1 + multiPart2);
console.log(singlePart);
console.log(multiPart1 + multiPart2);
</script>
Original comment by Jeff.Mott.OR
on 5 Feb 2013 at 11:31
Original comment by Jeff.Mott.OR
on 6 Feb 2013 at 4:40
Yes indeed...probably a mismatch in my tests, sorry.
But that did not return the expected result. Adding the option
padding:CryptoJS.pad.NoPadding does return the expected result.
Now, there might still be an issue for progressive ciphering : cryptoJS
implementation does return multiPart1 first and multiPart2 after final, where
multiPart1+multiPart2 is the encryption of "text", which is logical, but other
implementations I know (at least node.js's one, so probably openssl too) return
multiPart1+multiPart2 on update (still mysterious for me, maybe you know why).
So, during streaming, if I want to do :
1- sending party :
Enc=createEncryptor(key,params)
encrypt Buffer1 --> Enc.update(Buffer1)=final for Buffer1
encrypt Buffer2 --> Enc.update(Buffer2)=Buffer2 part of final for
Buffer2+Buffer1
etc
2- receiving party :
Dec=createDecryptor(key,params)
Dec.update(Buffer1) --> decoded Buffer1
Dec.update(Buffer2) --> Buffer2 decoded part of decoded (Buffer2+Buffer1)
etc
The rational might be unclear to do this, but as far as I see (and have
experienced with node-Tor) this is how the Tor protocol is working.
I don't see how to do this since final does end the encryptor. I have tried :
crypto.createCipheriv=function(algo,key,iv) {
algo=algo.split('-');
key=abv2wa(key);
var params={mode:CryptoJS.mode[algo[2].toUpperCase()],iv:abv2wa(iv),padding:CryptoJS.pad.NoPadding};
var enc=CryptoJS.algo.AES.createEncryptor(key,params);
enc.update=function(data) {
var m=this._data.sigBytes*2;
var tmp=this.process(abv2wa(data)).toString(CryptoJS.enc.Hex);
return [tmp.substr(m),this.clone().finalize().toString(CryptoJS.enc.Hex)].join('');
};
enc.final=function() {return this.finalize().toString(CryptoJS.enc.Hex);};
return enc;
};
Normally this should work but does not, it seems tha this.clone().finalize is
modifying something (object in common between clone and cloned?) that affects
the cloned encryptor but I can't find out what for now.
Original comment by vitteaym...@gmail.com
on 6 Feb 2013 at 4:44
> But that did not return the expected result. Adding the option
padding:CryptoJS.pad.NoPadding does return the expected result.
The default padding is PKCS5, so you'll have to explicitly specify if you want
to use a different padding or no padding.
> other implementations I know (at least node.js's one, so probably openssl
too) return multiPart1+multiPart2 on update
Since CryptoJS might need to apply a padding, it can't encrypt any partial
blocks until it knows whether it's the last block. Calling finalize() is how
CryptoJS knows there are no more blocks coming, and it can then process the
remaining partial block.
> So, during streaming, if I want to do :
Is the issue that another library expects exactly two message parts? No more,
no less?
Original comment by Jeff.Mott.OR
on 6 Feb 2013 at 7:10
> Since CryptoJS might need to apply a padding, it can't encrypt any partial
blocks until it knows whether it's the last block. Calling finalize() is how
CryptoJS knows there are no more blocks coming, and it can then process the
remaining partial block.
Then why the processing is not different if we explicitly asked for no padding ?
> Is the issue that another library expects exactly two message parts? No more,
no less?
Not sure to understand the question but the expectation is what I described, so
probably the answer is yes
Original comment by vitteaym...@gmail.com
on 6 Feb 2013 at 10:21
I'll accept that CryptoJS could -- and probably should -- adjust its block
processing depending on how many bits a padding might add or remove, which in
the case of no padding would be zero.
Original comment by Jeff.Mott.OR
on 7 Feb 2013 at 7:06
Original issue reported on code.google.com by
vitteaym...@gmail.com
on 5 Feb 2013 at 9:26