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

The oaep padding is not compatiable to other lib. #57

Closed
wenjoy opened this issue Aug 4, 2020 · 9 comments
Closed

The oaep padding is not compatiable to other lib. #57

wenjoy opened this issue Aug 4, 2020 · 9 comments

Comments

@wenjoy
Copy link

wenjoy commented Aug 4, 2020

I try to use this crate to encrypt data and decrypt use node's crypto and this online tool.

this is my example code:

use rsa::{PublicKey, RSAPublicKey, RSAPrivateKey, PaddingScheme};
use rand::rngs::OsRng;
use sha2::Sha256;

pub fn encrypt_(key: &[u8], plaintext: &[u8]) -> Vec<u8> {
  let mut rng = OsRng;
  let data = plaintext;
  let public_key = RSAPublicKey::from_pkcs8(key).expect("get private key error");
  let padding = PaddingScheme::new_oaep::<Sha256>();
  let enc_data = public_key.encrypt(&mut rng, padding, &data[..]).expect("failed to decrypt");
  enc_data
}

then try to decrypt by crypto

const crypto = require('crypto');
const fs = require('fs');
const privateKey = fs.readFileSync('./private.pem');
const publicKey = fs.readFileSync('./public.pem');
const cipher = fs.readFileSync('./cipher') // I persisted the encrypt data into this file without base64 encoded

function decryptOthers() {
  const plainText = crypto.privateDecrypt(privateKey, cipher);
  console.log('plainText: ', plainText.toString());
}

then I got this error error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

here is my private.pem

-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC3tiVTqKHkGZAl
bW9NsOjT87HkoIjlQM8el9qhsSBZJRzZr3cbZuV57qbhC2Nls7RurYGjd6+sNL4d
mcbjzpaSSUK9WxRi/t0Uc5cAeUiPWJ4tsf4C+zPg3cA/PE4UhbOcJxsXMQ6QMCSg
9Kvqbx6IGtejvMGLsnBc9lQrY5mKzmJBEtQ8FGyZDIDWhYCvhD8PJf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uqBAS6sEzU2CerAwAew98giHI
O8sfzs6iyEGg4cxWT4Dg8cD7gJPa/p/XNGiwG2vOb4lm52tyxeCf2blGfDQt9+BY
OZ+AiwD91soyMjSqS3VRYKKWqDb1871P42l8hK0CgYAv1ncq/QPZMM6yg97hHCNL
dXIlRASa1G8tHJGxdz0O0z1+tMwHR6GgN9cgKTT8I6VvhhQfQs8Lj0+itMcVnVbt
lVVqu1DNdnAT+VLFECOyxbn28GKeKBWLVih7DXCAz4UhwiYlAmdMnqCFcDy2XBs9
ZINP8KKsJFd50ijb8hRo5w==
-----END PRIVATE KEY-----

and public.pem

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt7YlU6ih5BmQJW1vTbDo
0/Ox5KCI5UDPHpfaobEgWSUc2a93G2blee6m4QtjZbO0bq2Bo3evrDS+HZnG486W
kklCvVsUYv7dFHOXAHlIj1ieLbH+Avsz4N3APzxOFIWznCcbFzEOkDAkoPSr6m8e
iBrXo7zBi7JwXPZUK2OZis5iQRLUPBRsmQyA1oWAr4Q/DyX/1Qli4fXjDOrEK2ZT
vOAQeawdYruCtt86+AlE+YU+/OeRQnwIQoLk/kuIcvi2U086Dm8A7U/EKEmqwofW
PE7uOt6K5GI894f7KB3IvFa8rnt4L3LvJfaQv3E2GaetbxCkzyxxh2zXmk8eVq33
4wIDAQAB
-----END PUBLIC KEY-----

and I also tried on this online tool. still no lucky.

but if:

  1. I encrypt with node's crypto then decrypt with this online tool. it works.
  2. I encrypt with this rust lib,but use pkcs1 padding, and select pkcs1 in online tool. it works.

So I can infer that it's the oaep padding lead to incompatible.

@dignifiedquire
Copy link
Member

@wenjoy
Copy link
Author

wenjoy commented Aug 4, 2020

@dignifiedquire Thanks for rapidly reply. I thought is the sha1 issue. But I cant figure out how to use sha1 in this crate.

use rsa::{PublicKey, RSAPublicKey, RSAPrivateKey, PaddingScheme};
use rand::rngs::OsRng;
use sha1::Sha1;

pub fn encrypt_(key: &[u8], plaintext: &[u8]) -> Vec<u8> {
  let mut rng = OsRng;
  let data = plaintext;
  let public_key = RSAPublicKey::from_pkcs8(key).expect("get private key error");
  // pkcs#1 padding
  // let padding = PaddingScheme::new_pkcs1v15_encrypt();
  let padding = PaddingScheme::new_oaep::<Sha1>();
  let enc_data = public_key.encrypt(&mut rng, padding, &data[..]).expect("failed to decrypt");
  enc_data
}

I tried this, but it complain. And I cant find out what other than Sha256 I can adapt, after go through the document.

@tarcieri
Copy link
Member

tarcieri commented Aug 4, 2020

@wenjoy
Copy link
Author

wenjoy commented Aug 4, 2020

@tarcieri Thanks. But I do need to interact with some legacy code. Is there any way I can use this lib to achieve?

@dignifiedquire
Copy link
Member

dignifiedquire commented Aug 5, 2020

@wenjoy what is the issue you are encountering with passing Sha1? We do have a test making sure the code does work with Sha1: https://github.com/RustCrypto/RSA/blob/master/src/key.rs#L904

@wenjoy
Copy link
Author

wenjoy commented Aug 5, 2020

@dignifiedquire I got this error:

the trait bound `sha1::Sha1: digest::Update` is not satisfied

the trait `digest::Update` is not implemented for `sha1::Sha1`

But the link you post inspired me to check the dependencies, then I solved it!

Thanks so much @dignifiedquire !

That's the thing. Previously my dependencies likes this:

sha1 = "*

after compare with this lib's , I found it should be this:

sha-1 = "*"

Now it works like a charm. But I have to say why its name so weird. Just following sha2 sha3 wouldn't be nice?

Anyway, thanks again @dignifiedquire.

@wenjoy wenjoy closed this as completed Aug 5, 2020
@newpavlov
Copy link
Member

@wenjoy
See the following remark in the hashes repository: https://github.com/RustCrypto/hashes#crate-names

@dignifiedquire
Copy link
Member

glad to hear it is working now @wenjoy

@wenjoy
Copy link
Author

wenjoy commented Aug 6, 2020

@newpavlov Now that does make sense. Thanks buddy.

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

4 participants