javascript - How to Decrypt RSA encrypted string from JSEncrypt using C# BouncyCastle -
i know there few posts regarding this, none of them solve issue cut chase.
i have encrypted string using jsencrypt (javascript) following code:
var rsa = new jsencrypt(); rsa.setpublickey(pubkey); var encrypted = rsa.encrypt($(this).text());
the public key stored in var so:
var pubkey = "-----begin public key-----\ migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqckskehcz6ztvnxkghbckfeenkf\ 38j1hegww9uazlg5a7m/1aahkqkvkuofox+zsyh4cjr/mevmrea2ucpjkljxk14m\ 74nzswyupxaikpfwxe7qvn1/0tiqahvheismqielil+ghiyac32e/wuyjhgfjbwl\ ej/hbdtkt6o/qajwkwidaqab\ -----end public key-----";
i use generated cipher text , send c# needs decrypt it. using bouncy castle , have written code such:
public string decrypt(string ciphertext) { var bytestodecrypt = convert.frombase64string(ciphertext); asymmetriccipherkeypair keypair; using (var reader = file.opentext(appdomain.currentdomain.basedirectory + @"/scripts/private.pem")) { keypair = (asymmetriccipherkeypair)new pemreader(reader).readobject(); } var engine = new rsaengine(); engine.init(false, keypair.private); var decrypted = engine.processblock(bytestodecrypt, 0, bytestodecrypt.length); var decryptedmessage = encoding.utf8.getstring(decrypted); return decryptedmessage; }
it decryption without error, output looks this:
"\u0002��lt�j;�ol�\u008fy�l:gk=\u0002�(�����\\\u0003ܢ�fmt�\u001eoqr�\\\u000f�s�a��+ښ�|����\u0001j�s�w[\u001egſ\u0012^7�\u000f44qy\a*�t�~tu�ei�o/<�\u000f!s:�]\u0018v<7s\n:x@���gȀ\u0016.w���lb�\0"
i have tried use pksc#1 having this:
public string decrypt(string ciphertext) { var bytestodecrypt = convert.frombase64string(ciphertext); asymmetriccipherkeypair keypair; using (var reader = file.opentext(appdomain.currentdomain.basedirectory + @"/scripts/private.pem")) { keypair = (asymmetriccipherkeypair)new pemreader(reader).readobject(); } pkcs1encoding engine = new pkcs1encoding(new rsaengine()); engine.init(false, keypair.private); var decrypted = engine.processblock(bytestodecrypt, 0, bytestodecrypt.length); var decryptedmessage = encoding.utf8.getstring(decrypted); return decryptedmessage; }
i have wrong here decryptedmessage = "" , decrypted bytes = bytes[0]
how fix decrypts ciphertext correctly.
as far know jsencrypt encrypts using pksc1 encoding default?
any appreciated.
please avoid rude comments, learning correctly why posting here.
thank in advance!
Comments
Post a Comment