Advertisement
Kyojin96

C# side

Feb 11th, 2017
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1.  static void Main()
  2.         {
  3.            
  4.             //string publicKeyFile = File.ReadAllText("E:\\publickey.rsa");
  5.             string privateKeyFile = File.ReadAllText("E:\\privatekey.rsa");
  6.             string cipherFile = File.ReadAllText("E:\\cipher.rsa");
  7.  
  8.             //Convert base64 to DER
  9.             byte[] privateKeyArr = Convert.FromBase64String(privateKeyFile);
  10.  
  11.             Console.WriteLine(cipherFile.Length);
  12.  
  13.             //Dump the bytes to .der file
  14.             File.WriteAllBytes("private-key.der", privateKeyArr);
  15.  
  16.             byte[] cipherArr = Convert.FromBase64String(cipherFile);
  17.  
  18.             //Load the der file
  19.             AsnKeyParser keyParser = new AsnKeyParser("private-key.der");
  20.             RSAParameters privateKey = keyParser.ParseRSAPrivateKey();
  21.  
  22.             CspParameters csp = new CspParameters();
  23.             csp.KeyContainerName = "RSA Test (OK to Delete)";
  24.             csp.ProviderType = 1;
  25.             csp.KeyNumber = 1;
  26.  
  27.             RSACryptoServiceProvider rsp = new RSACryptoServiceProvider(csp);
  28.             rsp.PersistKeyInCsp = false;
  29.             rsp.ImportParameters(privateKey);
  30.  
  31.             //Causes exception here..
  32.             var data = rsp.Decrypt(cipherArr, true);
  33.                
  34.             string text = Encoding.Default.GetString(data);
  35.            
  36.             Console.WriteLine(text);
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement