c# - How do you decrypt a xml file using BouncyCastle and .pem file? -


i have xml file has been encrypted , pem file contains private key , certificate.

var bytestodecrypt = file.readallbytes(@"encrypted.xml.pem");  asymmetriccipherkeypair keypair;  using (var reader = file.opentext(@"privatekeyandcert.pem")) {      keypair = (asymmetriccipherkeypair)new org.bouncycastle.openssl.pemreader(reader).readobject(); }  var decrypted = rsadecrypt(bytestodecrypt, keypair.private); var xml = encoding.utf8.getstring(decrypted); 

1st attempt of decoding throws exception "input large rsa cipher."

public static byte[] rsadecrypt(byte[] data, asymmetrickeyparameter key) {     var decryptengine = new rsaengine();     decryptengine.init(false, key);     return decryptengine.processblock(data, 0, data.length); } 

2nd attempt completes output random chars , not xml.

public static byte[] rsadecryptchunk(byte[] data, asymmetrickeyparameter key) {     try     {         var engine = new rsaengine();         engine.init(false, key);          int blocksize = engine.getinputblocksize();          list<byte> output = new list<byte>();          (int chunkposition = 0; chunkposition < data.length; chunkposition += blocksize)         {             int chunksize = math.min(blocksize, data.length - ((chunkposition / blocksize) * blocksize));             output.addrange(engine.processblock(data, chunkposition, chunksize));         }          return output.toarray();     }     catch (exception ex)     {          return null;     } } 

the following command decrypts openssl know files correct:

openssl smime -decrypt -inform pem -in "encrypted file path" -out "unencrypted file path" -inkey ".pem file path" 


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -