java - Can't decrypt Forge-encrypted data -
i use create private key , encrypt:
var forge = require('node-forge'); var fs = require('fs'); var bytes = forge.random.getbytessync(16); console.log("random plaintext", forge.util.bytestohex(bytes)); var keypair = forge.pki.rsa.generatekeypair({bits: 2048, e: 17}); var encrypted = keypair.publickey.encrypt(bytes, 'rsa-oaep', { md: forge.md.sha256.create(), mgf1: { md: forge.md.sha1.create() } }); console.log("ciphertext", forge.util.bytestohex(encrypted)); fs.writefilesync('ciphertext', encrypted); var pem = forge.pki.privatekeytopem(keypair.privatekey); fs.writefilesync('prikey.pem', pem); console.log("private key", forge.util.bytestohex(pem));
i try decrypting using either:
public class cryptotest { public static void main(string[] args) throws exception { pemparser parser = new pemparser(new filereader("/tmp/prikey.pem")); security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider()); jcapemkeyconverter converter = new jcapemkeyconverter().setprovider("bc"); keypair kp = converter.getkeypair((pemkeypair) parser.readobject()); rsapublickey pubkey = (rsapublickey) kp.getpublic(); rsaprivatekey privkey = (rsaprivatekey) kp.getprivate(); byte[] ct = files.readallbytes(paths.get("/tmp/ciphertext")); cipher oaepfrominit = cipher.getinstance("rsa/ecb/oaeppadding"); oaepparameterspec oaepparams = new oaepparameterspec("sha-256", "mgf1", new mgf1parameterspec("sha-1"), pspecified.default); oaepfrominit.init(cipher.decrypt_mode, privkey, oaepparams); byte[] pt = oaepfrominit.dofinal(ct); } }
or
openssl rsautl -decrypt -inkey prikey.pem -oaep -in ciphertext -out plaintext
the java code throws error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: data rsa block @ org.bouncycastle.jcajce.provider.asymmetric.rsa.cipherspi.enginedofinal(unknown source) @ javax.crypto.cipher.dofinal(cipher.java:1966) @ cryptotest.cryptotest.main(cryptotest.java:47)
and openssl says:
rsa operation error 140258189264528:error:0406506c:rsa routines:rsa_eay_private_decrypt:data greater mod len:rsa_eay.c:518:
what problem?
it necessary keep coded message in binary encoding:
fs.writefilesync('ciphertext', encrypted, {encoding: 'binary'});
Comments
Post a Comment