How to replicate java encryption in c# -
i have public modulus , exponent key. encoding test piece of text , getting different results java code trying replicate.
the java code here:
rsapublickeyspec rsapublicspec = new rsapublickeyspec(modulus, exponent); keyfactory keyfactory = keyfactory.getinstance("rsa"); publickey publickey = keyfactory.generatepublic(rsapublicspec); x509encodedkeyspec encodedpublickeyspec = new x509encodedkeyspec(publickey.getencoded()); keyfactory keyfactory = keyfactory.getinstance(“rsa”); publickey publickey = keyfactory.generatepublic(encodedpublickeyspec); cipher cipher = cipher.getinstance(“rsa / ecb / pkcs1padding”); cipher.init(cipher.encrypt_mode, publickey); byte[] encryptedbytes = cipher.dofinal(cleartextstring.getbytes()); base64encoder b64 = new base64encoder(); string base64encodedstr = b64.encode(encryptedbytes);
what have in c# right using bouncy castle is:
biginteger publicmodulus = new biginteger(1, convert.frombase64string(publickeystring)); biginteger publicexponent = new biginteger(1,convert.frombase64string("aqab")); rsakeyparameters pubparameters = new rsakeyparameters(false, publicmodulus, publicexponent); iasymmetricblockcipher eng = new pkcs1encoding(new rsaengine()); eng.init(true, pubparameters); byte[] plaintext = encoding.utf8.getbytes("test data"); byte[] encdata = eng.processblock(plaintext, 0, plaintext.length); console.writeline(convert.tobase64string(encdata));
i'm confused why java code gets public key , x509 version (and whether need in c# implementation). i'm not sure if need compensate endianness of c# vs. java.
appreciate help.
the confusing code didn't anything. also, bouncy castle wasn't needed once got modulus base64 string. here's worked.
var publickey = "<rsakeyvalue><modulus>base64modulusgoeshere</modulus><exponent>aqab</exponent></rsakeyvalue>"; var sbytes = encoding.utf8.getbytes(s); var rsa = new rsacryptoserviceprovider(2048); rsa.fromxmlstring(publickey); var encdata = rsa.encrypt(sbytes, false); enc = convert.tobase64string(encdata);
Comments
Post a Comment