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

How to encode using RSA/NONE/OAEPWithSHA-1AndMGF1Padding? #464

Open
watson opened this issue Jan 3, 2017 · 7 comments
Open

How to encode using RSA/NONE/OAEPWithSHA-1AndMGF1Padding? #464

watson opened this issue Jan 3, 2017 · 7 comments

Comments

@watson
Copy link

watson commented Jan 3, 2017

I'm trying to mimic the following Java code:

byte[] data = ... // bytes to encode
PublicKey key = ... // a public key extracted from a pem encoded X.509 certificate
Cipher encoder = Cipher.getInstance("RSA/NONE/OAEPWithSHA-1AndMGF1Padding");
encoder.init(Cipher.ENCRYPT_MODE, key);
byte[] encoded = encoder.doFinal(data);

But I'm not sure I completely understand the documentation in the README.md file 😨 The following is based off a RSA/ECB/OAEPWithSHA-256AndMGF1Padding implementation that I found under the RSA section, but I'm not sure how to change it from ECB to NONE:

var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha1.create(),
  mgf: {
    md: forge.md.sha1.create()
  }
})

Btw, I've also seen this approach used, but I can't figure out if it's just two ways of writing the same thing:

var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha1.create(),
  mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
})
@parsibox
Copy link

i also have this question
please answer this question

@dlongley
Copy link
Member

The presence of "ECB" in "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" doesn't make any sense. "ECB" is a symmetric cipher mode and RSA-OAEP is an asymmetric cipher. So "ECB" has no effect whatsoever ... I presume it has the same meaning as "NONE".

If someone wants to put some runnable code into this issue for both the Java and JavaScript side demonstrating something not working properly, then others could help debug what the issue is.

@MartijnR
Copy link

MartijnR commented Sep 21, 2018

Btw, I've also seen this approach used, but I can't figure out if it's just two ways of writing the same thing:

Thanks for posting this. It helped me a lot. I found out they are not the same thing. The one that matches Java RSA/NONE/OAEPWithSHA-1AndMGF1Padding is:

mgf: forge.mgf.mgf1.create( forge.md.sha1.create() )

The other one results in an exception during decryption: javax.crypto.BadPaddingException: data hash wrong

@TheCoderateKid
Copy link

Btw, I've also seen this approach used, but I can't figure out if it's just two ways of writing the same thing:

Thanks for posting this. It helped me a lot. I found out they are not the same thing. The one that matches Java RSA/NONE/OAEPWithSHA-1AndMGF1Padding is:

mgf: forge.mgf.mgf1.create( forge.md.sha1.create() )

The other one results in an exception during decryption: javax.crypto.BadPaddingException: data hash wrong

Hi I am unable to get RSA/NONE/OAEPWithSHA-1AndMGF1Padding working with this library, any chance for an example of how this type of encryption is done with this library? I have used the suggested approach above to no avail

// this does not work
var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha1.create(),
  mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
})

// this does not work either
var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha1.create(),
  mgf: {
    md: forge.md.sha1.create()
  }
})


// have tried this also

// this does not work either
var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha256.create(),
  mgf: {
    md: forge.md.sha1.create()
  }
})

thanks in advance

@MartijnR
Copy link

MartijnR commented Jan 6, 2023

This is the code we're using: https://github.com/enketo/enketo-express/blob/master/public/js/src/module/encryptor.js. Maybe that helps.

@TheCoderateKid
Copy link

Thanks @MartijnR , this helped out a lot !!!

@TheCoderateKid
Copy link

Just to note, my issue was I needed SHA-512, i did not see this in the documentation, so this worked for me

var encoded = publicKey.encrypt(data, 'RSA-OAEP', {
  md: forge.md.sha512.create(),
  mgf: forge.mgf.mgf1.create(forge.md.sha1.create())
})

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

5 participants