Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsupported state or unable to authenticate data #29

Closed
AllanOricil opened this issue Aug 31, 2020 · 19 comments
Closed

Unsupported state or unable to authenticate data #29

AllanOricil opened this issue Aug 31, 2020 · 19 comments

Comments

@AllanOricil
Copy link

AllanOricil commented Aug 31, 2020

I can't decrypt this string. I encrypted a string with the following key, which I read from a file using fs and encoding utf-8. Then I tried to decrypt it and got the error in the image below.

My Key: attention, the key below has an empty line at the end

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtLUeYvKWtAzhgeCM/bdQrirG+1m1OyDgM5bJfzyOE0Qd7Zyb
IhLrJ/jQpuYQ3tqdwXJBozRLpj9S4zid7S75SF+bvPGU5QY9fhCxJYbyf8yaiQ+v
vIQf6cbhOYOKtm16rRdxX2wJj28ETx8SWmRgTkLGlDmGkiUSAWr/8QMWSYuCnG1c
470v3f/PvUC4XlohZWBPs2uZGUge27shKluclGwryojH6AaCr/h5fSjHBQxcChp2
Yp/Qx2QU5fVbQpZNZ2cvvGCCTJbu4CX381jL+T4twQa5JWPZC1u1sI/VvmEVCRto
VhcErP/lCRJk2Qcp1c4A+QFySb+l9HGkVwq3BwIDAQABAoIBADecJSW+EbKG5D7b
o4fQifuPHnzrNmJj05sMpCWLtQ0owS0GLJdnywYaA9vYoQhzrmiBiMF4rm8Svac/
HRqIgh5LjdmvXmq8aOAKsvkhdi7uAAB9207gOhHvKhBQ8rcBKedqd3EuBiMlvuW6
YiN/EEQFOKcUrNz8m3XkP68jjBBFYYSp3CKTyNjz2e5uYMNvWyPnzWCy2PoAYzoJ
SMV/WYHWf6WIt6xNkJ83HjQmR3hnP2d7z7c9ahxods4Uorxr1orYCLOsW1QuU/mG
ZEnfpMDq93l3M6FuEfLn7olF131UKMMNRow8GmnYn8pNIUsb0QPI0btXADEPEDHI
34gEkAECgYEA7OpYmkbKLFmeF7KzBCENKjsb1hFHJrXzutIXHRGXS3IxOJtkDd+g
WEGq16OXDF/3xie3K2IHjnzllEXtuTsxdh32dmiRCcQxQyPLDRMLgSZPjtOsuDYG
XHSzVRXDzFz3KYIB+AF8Aew4Auba7BOZY3yFVbZ7Eq5I9X15OWYBKwECgYEAw0Oo
GEMuyA1PGWVVJxDsT5I22f7AGN0RM6unBtqeWdoHwFwFylFySfOJReiKkyjnmkRX
BBVyxV1zZJ3aiSLuZkgjZtL8EAi7ReLPuWtfFJc53LaIco7fuoUQ89Uhh5AuPBCE
cvOYL0G0+Y3LR1CVu2MCVBRpyz8QuF/n2U/UigcCgYAjsrAFFxORmTNUGM4WmjBU
o7FvEQ1qjNIb3Vo0PBlS+fLmt6tc18xeYA6xDWpOKAu6pPNZqxy+YJ1rORo35e5v
ANbJQrpQAWr21ebbonoCTNaCC+0jmKheuArpkCz/lyZ676cCSp733zMVZNmp5s4f
Cn1AzA1BZFeD/hMNDgyxAQKBgFhDZOBSvfq/yVl5bJxUEf2F4jV36e1TQEtS9BUt
DRcgbxCMiDGDz12OMCbhjoImo0ogShUWfOKp81cv43DJ0Wl6gkYgaL/2snBMrgjq
d/gew5D5EYR3ALqXG6qcVKOqkLul27j6U3hJAz0q0xBRZN1nS84lzAyLXUt6Tpr0
cuZRAoGBAKb0s3EHlDlTfCV9XBhJxXMBpmTDBI7n9YswyVsjo83VS13Ux3NqGuVA
2vb7GdgEOWzAWne03I+A47b6EDCwVyduLCQ8UqKmda6xSbf93Vwzet4RgzyO6xTL
UkecGtAgt9zQZ349rtyJjZz9ePCCkH/kTfUHXrxTjfhLyYVqlaJy
-----END RSA PRIVATE KEY-----

Encrypted String:
92df937f687b10f8c3c4ff6629bec84a0a5acfb9ba76243ee9d24e81a9b005394028740197f19aa27ccff3a373897f164c70837abf3eab0a193276a90a78ab2bbe636270df2809064f42ffbe1b3b066af627c3629d6bbb253f82b02434772065f3a552509fbbdbe7702c7c2dd5dc8e37fab94f00b62428e7df025e77f9bb0f9ebe0f830cd1ff39e4875d6c048efb856d3d224bb5db02d53b88bd7a14a6feb2a152e0c66a4eb4adf11f1b190a096a90e22a3a3cd4b64b5e

Error:
image

@AllanOricil
Copy link
Author

After some investigations I discovered that this method decipher.final('utf8') is that is throwing the error

image

@AllanOricil
Copy link
Author

I think the error is related to your code not passing 'hex' to the cypher.final(), like `cypher.final('hex')
I saw some examples and everyone passes an argument to chyper.final() but your code does not have it.

@proton1k
Copy link

proton1k commented Sep 2, 2020

@AllanOricil so did you solve this at the end? I am getting same error message and researching for the fix. What was your solution?

@AllanOricil
Copy link
Author

@o1dnik could not fix :/

@MauriceButler
Copy link
Owner

@AllanOricil Do you know the value that was encrypted and is it something you can share here? Was the value encrypted using cryptr?

I can not seem to fail the encryption or decryption using the private key you have provided as the secret.

The cipher.final() call does not need an encoding. It is returning a Buffer which is being concatenated with other data and converted to hex a few lines later.

@AllanOricil
Copy link
Author

@MauriceButler I can get another message to encrypt and decrypt to you later today. I tested the encryption with Crypto-js and it also did not work. This lib and crypto-js only worked well for me when I used a simpler string as my key, like mpnprfuQ2VrjEm5B. And I know it worked because the only thing I did was to change the Encryption Key.

@AllanOricil
Copy link
Author

@AllanOricil Do you know the value that was encrypted and is it something you can share here? Was the value encrypted using cryptr?

I can not seem to fail the encryption or decryption using the private key you have provided as the secret.

The cipher.final() call does not need an encoding. It is returning a Buffer which is being concatenated with other data and converted to hex a few lines later.

Try to decrypt the encrypted string with the key above. See if you can. You will probably have the same error as me. This cleary shows there is a limitation in the encryption.

@AllanOricil
Copy link
Author

@MauriceButler here is a message to encrypt: 5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c

this is the key: please, keep the blank line at the end of the key to keep the test consistent

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtLUeYvKWtAzhgeCM/bdQrirG+1m1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qjNIb3Vo0PBlS+fLmt6tc18xeYA6xDWpOKAu6pPNZqxy+YJ1rORo35e5v
ANbJQrpQAWr21ebbonoCTNaCC+0jmKheuArpkCz/lyZ676cCSp733zMVZNmp5s4f
Cn1AzA1BZFeD/hMNDgyxAQKBgFhDZOBSvfq/yVl5bJxUEf2F4jV36e1TQEtS9BUt
DRcgbxCMiDGDz12OMCbhjoImo0ogShUWfOKp81cv43DJ0Wl6gkYgaL/2snBMrgjq
d/gew5D5EYR3ALqXG6qcVKOqkLul27j6U3hJAz0q0xBRZN1nS84lzAyLXUt6Tpr0
cuZRAoGBAKb0s3EHlDlTfCV9XBhJxXMBpmTDBI7n9YswyVsjo83VS13Ux3NqGuVA
2vb7GdgEOWzAWne03I+A47b6EDCwVyduLCQ8UqKmda6xSbf93Vwzet4RgzyO6xTL
UkecGtAgt9zQZ349rtyJjZz9ePCCkH/kTfUHXrxTjfhLyYVqlaJy
-----END RSA PRIVATE KEY-----

@MauriceButler
Copy link
Owner

MauriceButler commented Sep 7, 2020

So With the key and value you just provided, I can encrypt and decrypt the value correctly.

Code for a test with a large secret below

const largeSecret = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtLUeYvKWtAzhgeCM/bdQrirG+1m1OyDgM5bJfzyOE0Qd7Zyb
IhLrJ/jQpuYQ3tqdwXJBozRLpj9S4zid7S75SF+bvPGU5QY9fhCxJYbyf8yaiQ+v
vIQf6cbhOYOKtm16rRdxX2wJj28ETx8SWmRgTkLGlDmGkiUSAWr/8QMWSYuCnG1c
470v3f/PvUC4XlohZWBPs2uZGUge27shKluclGwryojH6AaCr/h5fSjHBQxcChp2
Yp/Qx2QU5fVbQpZNZ2cvvGCCTJbu4CX381jL+T4twQa5JWPZC1u1sI/VvmEVCRto
VhcErP/lCRJk2Qcp1c4A+QFySb+l9HGkVwq3BwIDAQABAoIBADecJSW+EbKG5D7b
o4fQifuPHnzrNmJj05sMpCWLtQ0owS0GLJdnywYaA9vYoQhzrmiBiM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I+A47b6EDCwVyduLCQ8UqKmda6xSbf93Vwzet4RgzyO6xTL
UkecGtAgt9zQZ349rtyJjZz9ePCCkH/kTfUHXrxTjfhLyYVqlaJy
-----END RSA PRIVATE KEY-----
`;

const input = '5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c';

const cryptr = new Cryptr(largeSecret);

const encryptedString = cryptr.encrypt(input);
console.log(encryptedString); // 'd9752e5656b27017a826db1902bae6b65f751600020ac8a77c23bbec2fde437bc97412859864364a8d71959b80f186f0fbc2bf2298043332fee74fb06d55b96a5ff6b0757d5708e3a00c52c537e08a9a0d6048d2eaae82a9fd858a83fe5bdc7ffb494564595d38b0193bdfc0e4142088db022681f39a66ef1c3d6705053c92906ecc80119ae18b69d6bda066edc9e7b156c788b7ae5922fc58b2e9504ed690786650e544579ec1761b2655eabd2a3a9bfe2c69eb36b5dd'

const decryptedString = cryptr.decrypt(encryptedString);
console.log(decryptedString); // '5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c' 

console.log(decryptedString === input); // true

So I can not see a problem with value encrypted and decrypted with this library using a large secret like that.

I agree that the value you provided in the initial report does not decrypt but I can't reproduce that from a value encrypted from this library.

Can you confirm the above code works for you? If you can make it fail I am happy to have a look further but I cant see an issue at the moment.

@AllanOricil
Copy link
Author

@MauriceButler I did this test and I also was able to do it in one round, like your code above.
The problem starts when you decrypt the message in a separate file/time/process, like in a real world scenario.

Do this:
Encrypt and print the result.
Copy the result message.
Create another file to decrypt and pass the message and the key.

Also, you have to add an empty line in the key at the end. Could you also read the key from a file, like Im doing. DON'T FORGET TO ADD THE EMPTY LINE!!!

const largeSecret = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtLUeYvKWtAzhgeCM/bdQrirG+1m1OyDgM5bJfzyOE0Qd7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t6tc18xeYA6xDWpOKAu6pPNZqxy+YJ1rORo35e5v
ANbJQrpQAWr21ebbonoCTNaCC+0jmKheuArpkCz/lyZ676cCSp733zMVZNmp5s4f
Cn1AzA1BZFeD/hMNDgyxAQKBgFhDZOBSvfq/yVl5bJxUEf2F4jV36e1TQEtS9BUt
DRcgbxCMiDGDz12OMCbhjoImo0ogShUWfOKp81cv43DJ0Wl6gkYgaL/2snBMrgjq
d/gew5D5EYR3ALqXG6qcVKOqkLul27j6U3hJAz0q0xBRZN1nS84lzAyLXUt6Tpr0
cuZRAoGBAKb0s3EHlDlTfCV9XBhJxXMBpmTDBI7n9YswyVsjo83VS13Ux3NqGuVA
2vb7GdgEOWzAWne03I+A47b6EDCwVyduLCQ8UqKmda6xSbf93Vwzet4RgzyO6xTL
UkecGtAgt9zQZ349rtyJjZz9ePCCkH/kTfUHXrxTjfhLyYVqlaJy
-----END RSA PRIVATE KEY-----

`;

@MauriceButler
Copy link
Owner

Hi @AllanOricil

I still am unable to break it. Only thing I can think of is the way you are either reading or writing values to disk or then converting them to UTF-8 is incorrect.

If you can produce a failing example I am happy to keep looking into this but at this stage I cant find any issue.

The below code writes and reads all data from disk. Still works correctly, even if done later and not in the same process.

const fs = require('fs');
const key = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtLUeYvKWtAzhgeCM/bdQrirG+1m1OyDgM5bJfzyOE0Qd7Zyb
IhLrJ/jQpuYQ3tqdwXJBozRLpj9S4zid7S75SF+bvPGU5QY9fhCxJYbyf8yaiQ+v
vIQf6cbhOYOKtm16rRdxX2wJj28ETx8SWmRgTkLGlDmGkiUSAWr/8QMWSYuCnG1c
470v3f/PvUC4XlohZWBPs2uZGUge27shKluclGwryojH6AaCr/h5fSjHBQxcChp2
Yp/Qx2QU5fVbQpZNZ2cvvGCCTJbu4CX381jL+T4twQa5JWPZC1u1sI/VvmEVCRto
VhcErP/lCRJk2Qcp1c4A+QFySb+l9HGkVwq3BwIDAQABAoIBADecJSW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DlTfCV9XBhJxXMBpmTDBI7n9YswyVsjo83VS13Ux3NqGuVA
2vb7GdgEOWzAWne03I+A47b6EDCwVyduLCQ8UqKmda6xSbf93Vwzet4RgzyO6xTL
UkecGtAgt9zQZ349rtyJjZz9ePCCkH/kTfUHXrxTjfhLyYVqlaJy
-----END RSA PRIVATE KEY-----

`;

fs.writeFileSync('./key.txt', key);

const largeSecret = fs.readFileSync('./key.txt', 'utf-8');
const cryptr = new Cryptr(largeSecret);

const originalInput = '5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c';
fs.writeFileSync('./input.txt', originalInput);
const input = fs.readFileSync('./input.txt', 'utf-8');

const encryptedString = cryptr.encrypt(input);
fs.writeFileSync('./encrypted.txt', encryptedString);

const valueToDecrypt = fs.readFileSync('./encrypted.txt', 'utf-8');
const decryptedString = cryptr.decrypt(valueToDecrypt);

console.log(decryptedString === originalInput); // <-- true

Here are the files I wrote to disk.
key.txt
input.txt
encrypted.txt

@AllanOricil
Copy link
Author

AllanOricil commented Sep 9, 2020

@MauriceButler Hi, please, follow the steps bellow:

  • Create three files encrypt.js, decrypt.js, and server.txt on the same project folder. The encrypt and decrypt codes are shown below.
  • Run the encrypt script to get the encrypted output message. Then, in a separate process and time, add this message into the decrypt script where it says ENCRYPTED_MESSAGE and run it. The error should appear at this point.

Obs: When I run the encryption and decryption in the same script, like you did in the last two attempts, I don't get any exceptions. It only happens when I run them separated.
Obs2: The error happens on my Windows 10 and in my Ubuntu 20 machine on AWS (EC2).

encrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const encryptedMessage = cryptr.encrypt('5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c');
console.log(encryptedMessage);

decrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const decryptedMessage = cryptr.decrypt(ENCRYPTED_MESSAGE);
console.log(decryptedMessage);

@michael-trinity
Copy link

@MauriceButler Hi, please, follow the steps bellow:

  • Create three files encrypt.js, decrypt.js, and server.txt on the same project folder. The encrypt and decrypt codes are shown below.
  • Run the encrypt script to get the encrypted output message. Then, in a separate process and time, add this message into the decrypt script where it says ENCRYPTED_MESSAGE and run it. The error should appear at this point.

Obs: When I run the encryption and decryption in the same script, like you did in the last two attempts, I don't get any exceptions. It only happens when I run them separated.
Obs2: The error happens on my Windows 10 and in my Ubuntu 20 machine on AWS (EC2).

encrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const encryptedMessage = cryptr.encrypt('5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c');
console.log(encryptedMessage);

decrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const decryptedMessage = cryptr.decrypt(ENCRYPTED_MESSAGE);
console.log(decryptedMessage);

Hi I was looking at this package to use as well, I tried your test to see if it works, but it works for me without any issues.

I ran the encrypt and decrypt in a separate process and no problems at all.

image

@AllanOricil
Copy link
Author

AllanOricil commented Oct 20, 2020

@MauriceButler Hi, please, follow the steps bellow:

  • Create three files encrypt.js, decrypt.js, and server.txt on the same project folder. The encrypt and decrypt codes are shown below.
  • Run the encrypt script to get the encrypted output message. Then, in a separate process and time, add this message into the decrypt script where it says ENCRYPTED_MESSAGE and run it. The error should appear at this point.

Obs: When I run the encryption and decryption in the same script, like you did in the last two attempts, I don't get any exceptions. It only happens when I run them separated.
Obs2: The error happens on my Windows 10 and in my Ubuntu 20 machine on AWS (EC2).
encrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const encryptedMessage = cryptr.encrypt('5Aep861OVg8QiCb44ByDkG6vdPo8AkeO5Tgs9O7gGeOlF4jqpP_p8ibvCms_Vo.EXVhYN7wDfagTUsIBlhsNX9c');
console.log(encryptedMessage);

decrypt.js

const fs = require('fs');
const Cryptr = require('cryptr');

const serverKey = fs.readFileSync('./server.txt', { encoding: 'utf-8' } );
const cryptr = new Cryptr(serverKey);
const decryptedMessage = cryptr.decrypt(ENCRYPTED_MESSAGE);
console.log(decryptedMessage);

Hi I was looking at this package to use as well, I tried your test to see if it works, but it works for me without any issues.

I ran the encrypt and decrypt in a separate process and no problems at all.

image

@michael-trinity publish this repo on github and share with me. I will run it without changing anything and see the results. I think it is weird, because the problem only happens when I separate the encryption and decryption in different processes. Also, @o1dnik reported the same error message. And we don't know what we might be doing wrong.

@MauriceButler
Copy link
Owner

As far as I can tell there is no problem with this module itself, but more with you saving / reading from disk.

I am only guessing but something like different line endings could cause issues.

I am going to close this issue now as there is nothing else I can assist with but please feel free to add anything you find to the issue for others who might have similar problems later on.

@crackedpotato007
Copy link

same issue here
https://sourceb.in/0OKwvFvNmH

@crackedpotato007
Copy link

Ok so i found the issue. Make sure your key size is max of 64bytes

@Matt-CBI
Copy link

Matt-CBI commented May 7, 2023

It failed for me when it was exactly 64 bytes! So do make sure it is less. Thanks @arnav7633

@AllanOricil
Copy link
Author

What am I doing with my life??? 3y passed and I'm still unhappy because people don't take me seriously. I told u this was not working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants