Related
I have same problem as this question. I see the answer but I couldn't understand how I can do it. Is there any other suggestions?
My code is:
var eccrypto = require("eccrypto");
var w,actual;
var publicKey = Buffer.from([4, 86, 82, 58, 244, 11, 140, 41, 132, 245, 184, 162, 163, 98, 49, 119, 168, 235, 252, 50, 6, 91, 147, 191, 190, 61, 65, 63, 101, 164, 132, 213, 188, 106, 26, 203, 171, 215, 240, 151, 7, 193, 10, 151, 103, 107, 1, 135, 117, 225, 5, 41, 55, 57, 18, 205, 98, 178, 82, 135, 170, 111, 188, 98, 57],'hex');
var privateKey= Buffer.from([238, 239, 199, 101, 188, 134, 13, 13, 195, 172, 125, 168, 225, 189, 72, 148, 225, 200, 127, 218, 204, 11, 150, 146, 180, 243, 195, 109, 200, 119, 50, 20],'hex');
eccrypto.encrypt(publicKey, Buffer.from("message")).then(function(encrypted) {
console.log(encrypted)
let encoded =JSON.stringify(encrypted)
w=encoded;
console.log(encoded)
actual = JSON.parse((encoded))
console.log(actual)
});
eccrypto.decrypt(privateKey,actual).then(function(plaintext) {
console.log("Message to part B:", plaintext.toString());
});
When I use actual variable I have this error:
Uncaught (in promise) Error: Bad public key
this the output of encrypted :
this the output of encoded :
and this the output of actual "there is some things changes i think, is not it ?":
thank you advance.
While parsing, you need to send a reviver function, which conditionally parses the object. If you see how encoded looks, you will understand why the reviver function needs to be written this way.
I think you need to remove the "hex" encoding parameter from your Buffer.from.
i am trying to inflate a buffer with the zlib module in NodeJs. I had the problem that i always got the "unexpected end of file" error so i used my original data and let nodejs deflate it to compare the byte values.
(1) So this is the value that i have:
120, 156, 106, 46, 97, 96, 96, 96, 73, 97, 96, 76, 1, 49, 152, 82, 24, 216, 226, 75, 138, 18, 147, 83, 115, 128, 60, 198, 92, 32, 161, 17, 173, 148, 158, 88, 146, 90, 158, 88, 169, 91, 80, 148, 162, 155, 155, 152, 153, 167, 91, 104, 96, 94, 172, 164, 83, 173, 148, 155, 153, 92, 148, 95, 172, 100, 101, 160, 103, 80, 27, 155, 149, 194, 32, 148, 145, 154, 88, 84, 146, 148, 154, 88, 18, 159, 153, 87, 146, 90, 84, 150, 152, 147, 196, 192, 176, 80, 41, 133, 129, 41, 191, 32, 145, 11, 104, 75, 113, 10, 3, 115, 94, 102, 14, 144, 85, 2, 97, 1, 0, 0, 0, 255, 255
(2) And this is what zlib.deflateSync gave me:
120, 156, 1, 125, 0, 130, 255, 120, 156, 106, 46, 97, 96, 96, 96, 73, 97, 96, 76, 1, 49, 152, 82, 24, 216, 226, 75, 138, 18, 147, 83, 115, 128, 60, 198, 92, 32, 161, 17, 173, 148, 158, 88, 146, 90, 158, 88, 169, 91, 80, 148, 162, 155, 155, 152, 153, 167, 91, 104, 96, 94, 172, 164, 83, 173, 148, 155, 153, 92, 148, 95, 172, 100, 101, 160, 103, 80, 27, 155, 149, 194, 32, 148, 145, 154, 88, 84, 146, 148, 154, 88, 18, 159, 153, 87, 146, 90, 84, 150, 152, 147, 196, 192, 176, 80, 41, 133, 129, 41, 191, 32, 145, 11, 104, 75, 113, 10, 3, 115, 94, 102, 14, 144, 85, 2, 97, 1, 0, 0, 0, 255, 255, 164, 112, 54, 45, (bold = same)
So as far as i can tell from the RFC the first two bytes are the compression method and flags and therefor both have it. The other bold part is the compressed data. So far so good.
The first part that doesn't match is the DICTID part. But according to the RFC it is only four bytes long so what are the other three bytes (255, 120, 156)? The last part that is not bold is the ADLER32 checksum of the compressed data.
Question: Is zlib (nodejs) somehow able to inflate data without the DICTID and ADLER32 checksum?
Current Code Snippet:
const data = /* Array from (1) */;
const buffer = Buffer.from(data);
const out = zlib.inflateSync(buffer);
Output: Error: unexpected end of file
Background: What i am ultimately trying to do is the following but in javascript instead of python.
Note: The command line program zlib-flate is somehow able to inflate the bytes from (1) so i am assuming i do something wrong.
After hours of trail and error i finally figured it out what the problem was.
To be able to inflate the bytes from (1) you have to set the finishFlush option:
const result = zlib.inflateSync(buffer, {finishFlush: zlib.constants.Z_SYNC_FLUSH});
I am using eccrypto.js library for public key encryption.
Here I want to encrypt the message of A using public key of B and then let B to decrypt using private key of B.
In the library this is done by:
// Encrypting the message for B.
eccrypto.encrypt(publicKeyB, Buffer.from("msg to b")).then(function(encrypted) {
// B decrypting the message.
eccrypto.decrypt(privateKeyB, encrypted).then(function(plaintext) {
console.log("Message to part B:", plaintext.toString());
});
});
if I console log the encrypted value I got the object as:
{iv: Uint8Array(16), ephemPublicKey: Uint8Array(65), ciphertext: Uint8Array(48), mac: Uint8Array(32)}
ciphertext: Uint8Array(48) [13, 240, 10, 109, 88, 109, 108, 153, 213, 115, 40, 237, 66, 232, 251, 120, 27, 67, 119, 231, 17, 143, 78, 69, 43, 76, 214, 74, 132, 127, 220, 131, 44, 144, 221, 133, 48, 124, 239, 158, 226, 22, 119, 200, 170, 101, 241, 82]
ephemPublicKey: Uint8Array(65) [4, 84, 253, 207, 251, 2, 157, 203, 14, 233, 166, 216, 107, 1, 23, 90, 229, 209, 150, 58, 95, 253, 214, 183, 148, 167, 224, 15, 224, 244, 176, 165, 84, 121, 70, 4, 175, 186, 189, 104, 211, 207, 255, 195, 20, 128, 200, 237, 7, 9, 173, 234, 14, 208, 208, 68, 46, 76, 38, 26, 107, 41, 10, 188, 108]
iv: Uint8Array(16) [230, 246, 79, 17, 203, 191, 117, 7, 57, 149, 198, 68, 193, 220, 159, 56]
mac: Uint8Array(32) [202, 77, 212, 211, 27, 186, 174, 106, 211, 145, 100, 81, 100, 68, 61, 172, 175, 188, 213, 49, 63, 92, 172, 83, 30, 22, 47, 93, 60, 215, 33, 116]
Note: I have to store this in json format so i have converted it into json data.
Now if I convert this object to json and recover the json data the recovered object is not same.
Json object:
{
"iv":{"type":"Buffer","data":[226,253,245,0,227,222,47,37,65,177,171,68,201,142,242,35]},
"ephemPublicKey":{"type":"Buffer","data":[4,9,137,99,138,202,169,89,90,209,92,130,156,105,170,132,192,250,88,232,15,250,33,107,38,13,129,178,21,237,77,136,215,39,215,123,140,226,102,98,39,110,192,209,79,214,138,83,174,192,100,183,157,44,56,128,38,52,170,244,42,213,199,57,232]},
"ciphertext":{"type":"Buffer","data":[135,147,187,164,109,39,204,244,195,161,65,24,178,160,132,146,200,35,113,120,164,140,20,223,225,104,23,111,13,155,193,26,35,73,236,77,209,246,85,16,77,30,250,122,206,242,111,63]},
"mac":{"type":"Buffer","data":[79,195,220,150,230,150,13,187,9,131,12,81,151,107,29,216,138,143,85,52,153,71,179,167,243,141,107,88,97,206,110,107]}}
The recovered object (JSON.parse(encrypted)) is:
{iv: {…}, ephemPublicKey: {…}, ciphertext: {…}, mac: {…}}
ciphertext:
data: (48) [135, 147, 187, 164, 109, 39, 204, 244, 195, 161, 65, 24, 178, 160, 132, 146, 200, 35, 113, 120, 164, 140, 20, 223, 225, 104, 23, 111, 13, 155, 193, 26, 35, 73, 236, 77, 209, 246, 85, 16, 77, 30, 250, 122, 206, 242, 111, 63]
type: "Buffer"
__proto__: Object
ephemPublicKey:
data: (65) [4, 9, 137, 99, 138, 202, 169, 89, 90, 209, 92, 130, 156, 105, 170, 132, 192, 250, 88, 232, 15, 250, 33, 107, 38, 13, 129, 178, 21, 237, 77, 136, 215, 39, 215, 123, 140, 226, 102, 98, 39, 110, 192, 209, 79, 214, 138, 83, 174, 192, 100, 183, 157, 44, 56, 128, 38, 52, 170, 244, 42, 213, 199, 57, 232]
type: "Buffer"
__proto__: Object
iv:
data: (16) [226, 253, 245, 0, 227, 222, 47, 37, 65, 177, 171, 68, 201, 142, 242, 35]
type: "Buffer"
__proto__: Object
mac:
data: (32) [79, 195, 220, 150, 230, 150, 13, 187, 9, 131, 12, 81, 151, 107, 29, 216, 138, 143, 85, 52, 153, 71, 179, 167, 243, 141, 107, 88, 97, 206, 110, 107]
type: "Buffer"
__proto__: Object
__proto__: Object
if i used the recovered object to decrypt I am getting the error as bad public key.
My code is:
let PublicKey = Buffer.from("0418c7ced07c0c17f42b132747c70fddb6b31ea0ad349c2e9f800f48f0a73c2ea028d41b239077a48136ce546f9d2811bf1ec311c56e6a41f33906a1fc2472e451", 'hex')
eccrypto.encrypt(PublicKey, Buffer.from('Message to encrypt').then(function (encrypted) {
console.log(encrypted)
let encoded = JSON.stringify(encrypted)
console.log(encoded)
var actual = JSON.parse((encoded))
console.log(actual)
let pk = Buffer.from("9a2d66404b69023c2c45da81ca4b696a8234b7ae53ea6b7ffc0d6bdd0e0e3279", 'hex')
eccrypto.decrypt(pk, actual).then(function (plaintext) {
console.log("Message to part B:", plaintext.toString());
});
});
I am getting error as bad public key.
If i used the encrypted variable instead of acutal in the eccrypto.decrypt(pk,encrypted)...
then i get the decrypted value.
You can add a second argument to JSON.parse to allow you to inject code to help interpret the JSON as it's deserialized. It's called a reviver in the documentation.
You will get each key, value pair in the object.
Here is some sample code:
JSON.parse('{"p": 5}', (key, value) =>
typeof value === 'number'
? value * 2 // return value * 2 for numbers
: value // return everything else unchanged
);
Here is mode info on MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
In your example, I think you want to identify the values as type buffer and then replace them with UInt8Array
or create a new object like this:
const newWorkingObject = {
ciphertext: Buffer.from([...recoveredObject.ciphertext.data]),
ephemPublicKey: Buffer.from([...recoveredObject.ephemPublicKey.data]),
iv: Buffer.from([...recoveredObject.iv.data]),
mac: Buffer.from([...recoveredObject.mac.data]),
}
I have encountered an interesting issue.
I'm using node v8.1.4
I have the following buffer.
[ 191, 164, 235, 131, 30, 28, 164, 179, 101, 138, 94, 36, 115, 176, 83, 193, 9, 177, 85, 228, 189, 193, 127, 71, 165, 16, 211, 132, 228, 241, 57, 207, 254, 152, 122, 98, 100, 71, 67, 100, 29, 218, 165, 101, 25, 17, 177, 173, 92, 173, 162, 186, 198, 1, 80, 94, 228, 165, 124, 171, 78, 49, 145, 158 ]
When i try to convert it to utf8 using nodejs and using browser i get different results. even length of string is not the same.
Is there a way to convert string to utf8 in browser same way as node js do?
It seems that some characters that some sequence which nodejs replace to U+FFFD are more lengthy than the replaced sequence in browser. so output utf8 string is different
Code i use in browser and in nodejs is same
i have buffer object tmpString
tmpString.toString('utf-8')
tmpString.toString('utf-8').length differs in browser and nodejs for the same source bytes.
In nodejs i use native buffer implementation, for browser webpack loads polyfill (feross/buffer i think)
i think more accurately would say that i try to interpret buffer bytes as UTF8 string.
Have you tried the TextEncoder/TextDecoder APIs? I've used them for converting strings in both nodejs and the browser and haven't seen any differences.
E.g.:
const encoder = new TextEncoder('utf-8');
const decoder = new TextDecoder('utf-8');
const foo = 'Hello world!';
const encoded = encoder.encode(foo);
console.log(encoded);
const decoded = decoder.decode(encoded);
console.log(decoded);
I have to decrypt some strings which are AES encrypted.
Example encrypted string: 129212143036071008133136215105140171136216244116
I have a key, and a vector (iv) supplied to me in a byte-array format:
Key: [ 123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209 ]
Vector (iv): [ 146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136 ]
I should be able to decrypt the string and get:
Correct output: testtest
I'm trying to use Crypto.js but I can't find a way to use the supplied key and vector. I can't find a way to convert the byte-arrays to hex.
var encrypted = '129212143036071008133136215105140171136216244116';
var key = CryptoJS.enc.Hex.parse([ 123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209 ]);
var iv = CryptoJS.enc.Hex.parse([ 146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136 ]);
var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv });
console.log('Output: '+decrypted.toString(CryptoJS.enc.Utf8)); //Should be "testtest"
I would be so grateful if anyone could show me how to decrypt the example string using the key and vector with Crypto.js OR any other js method.
Thanks so much for any help,
Kind regards
I can't manage to decrypt your original string, but I can successful use it to encrypt and decrypt a new string. In your initial implementation, an error occurs in aes.js because it expects key and iv to be strings rather than arrays. I have corrected this code example below:
//var encrypted = '129212143036071008133136215105140171136216244116';
var key = CryptoJS.enc.Hex.parse(CryptoJS.lib.ByteArray([123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209]));
var iv = CryptoJS.enc.Hex.parse(CryptoJS.lib.ByteArray([146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136]));
var message = 'testest'
var encrypted = CryptoJS.AES.encrypt(message, key, {
'iv': iv
});
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
'iv': iv
});
document.write('Output: ' + decrypted.toString(CryptoJS.enc.Utf8)); //Should be "testtest"
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/pad-nopadding-min.js"></script>
<script src="https://greasyfork.org/scripts/6696-cryptojs-lib-bytearray/code/CryptoJSlibByteArray.js"></script>
Bit of an old post, and I think the API has changed somewhat since then as it now uses WordArray rather than ByteArray.
But I was running into the same issue. Turns out, you can simply provide a base64-encoded string into decrypt(). So all you need to do is to create a base64-encoded string from your original message. I'm not sure what format your message is in now, but I'm going to assume it's a hex string.
var origCipher = "129212143036071008133136215105140171136216244116"; // is this supposed to be a hex string?
var origKey = [ 123, 217, 20, 11, 24, 26, 85, 45, 114, 184, 27, 162, 37, 115, 222, 209, 241, 24, 175, 144, 175, 53, 196, 29, 24, 23, 17, 218, 131, 226, 53, 209 ];
var origIV = [ 146, 66, 191, 151, 23, 3, 113, 119, 231, 131, 133, 112, 79, 32, 114, 136 ];
var key = CryptoJS.lib.WordArray.create(new UInt8Array(origKey));
var iv = CryptoJS.lib.WordArray.create(new UInt8Array(origIV));
var cipher = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse(origCipher));
var plain = CryptoJS.AES.decrypt(cipher, key, { iv: iv });
var plaintext = CryptoJS.enc.UTF8.stringify(plain);
Above code doesn't result in a correct result, though, so your input ciphertext is probably not a hex string.
I ended up using a .net ashx generic handler, to which i post the data over ssl, and the server-side decryption takes place using the simpleAES class.
This class uses block-size as a parameter which seems to make the difference and using this approach I was able to decrypt all the strings i needed to.
More info about simpleAES here: https://github.com/huanlin/YetAnotherLibrary/blob/master/Source/Yalib/Cryptography/SimpleAes.cs
Thanks again for the help!