Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix crypto encryption/decryption with Base64. #1205

Closed
wants to merge 1 commit into from

4 participants

@cesare

This resolves the issue at #738
Cipher::CipherFinal now handles previous Base64 overhang properly.

@koichik koichik closed this in e357acc
@koichik
Owner

I have landed this into the v0.4 branch.
Thanks!

@donpark

fabulous guys. looking forward to undoing crappy workarounds when v0.4.11 is released.

@bnoordhuis

Cherry-picked into master in d3a84be.

@igorzi igorzi referenced this pull request from a commit in igorzi/node
@cesare cesare Fix crypto encryption/decryption with Base64.
Fixes #738.
Fixes #1205.
d3a84be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 19, 2011
  1. @cesare
This page is out of date. Refresh to see the latest.
Showing with 32 additions and 0 deletions.
  1. +13 −0 src/node_crypto.cc
  2. +19 −0 test/simple/test-crypto.js
View
13 src/node_crypto.cc
@@ -1825,6 +1825,19 @@ class Cipher : public ObjectWrap {
outString = Encode(out_hexdigest, out_hex_len, BINARY);
delete [] out_hexdigest;
} else if (strcasecmp(*encoding, "base64") == 0) {
+ // Check to see if we need to add in previous base64 overhang
+ if (cipher->incomplete_base64!=NULL){
+ unsigned char* complete_base64 = new unsigned char[out_len+cipher->incomplete_base64_len+1];
+ memcpy(complete_base64, cipher->incomplete_base64, cipher->incomplete_base64_len);
+ memcpy(&complete_base64[cipher->incomplete_base64_len], out_value, out_len);
+ delete [] out_value;
+
+ delete [] cipher->incomplete_base64;
+ cipher->incomplete_base64=NULL;
+
+ out_value=complete_base64;
+ out_len += cipher->incomplete_base64_len;
+ }
base64(out_value, out_len, &out_hexdigest, &out_hex_len);
outString = Encode(out_hexdigest, out_hex_len, BINARY);
delete [] out_hexdigest;
View
19 test/simple/test-crypto.js
@@ -128,6 +128,25 @@ txt += decipher.final('utf8');
assert.equal(txt, plaintext, 'encryption and decryption');
+// encryption and decryption with Base64
+// reported in https://github.com/joyent/node/issues/738
+var plaintext =
+ '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
+ 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJjAfaFg**';
+var cipher = crypto.createCipher('aes256', '0123456789abcdef');
+
+// encrypt plaintext which is in utf8 format
+// to a ciphertext which will be in Base64
+var ciph = cipher.update(plaintext, 'utf8', 'base64');
+ciph += cipher.final('base64');
+
+var decipher = crypto.createDecipher('aes256', '0123456789abcdef');
+var txt = decipher.update(ciph, 'base64', 'utf8');
+txt += decipher.final('utf8');
+
+assert.equal(txt, plaintext, 'encryption and decryption with Base64');
+
+
// Test encyrption and decryption with explicit key and iv
var encryption_key = '0123456789abcd0123456789';
var iv = '12345678';
Something went wrong with that request. Please try again.