0

I am learning about Cryptography in .NET and I wrote the following function as a test:

byte[] foo(byte[] input, string keyContainerName)
{
   CngKey key = CngKey.Open(keyContainerName);
   RSACng rsa = new RSACng(key);
   rsa.KeySize = 2048;
   byte[] v = rsa.Encrypt(input, RSAEncryptionPadding.OaepSHA512);

   CngKey keyb = CngKey.Open(keyContainerName);
   RSACng rsab = new RSACng(keyb);
   rsab.KeySize = 2048;
   return rsab.Decrypt(v, RSAEncryptionPadding.OaepSHA512);
}

When I try executing it, rsab.Decrypt() throws a Cryptographic exception with the message: "The parameter is incorrect.".

Why is this happening? Where did I go wrong?

P.S. I previously created a key pair in the KSP with CngKey.Create(). foo is called with keyContainerName beeing the keyName passed to CngKey.Create().

  • Are you experimenting with RSA cryptographic functions by writing your own or you want to use RSA in some sort of application. In the second case a better approach would be to use an encryption library like 'ExpressSecurity' – Sangeeth Nandakumar May 06 '21 at 13:21
  • I am creating a cryptographic app that will provide the user the ability to encrypt/decrypt text messages (or files) using symmetric and asymmetric algorithms (in background, only a symmetric key would be encrypted). The asymmetric methods wrote by me wouldn't work so I started testing. '''foo''' is a synthesis of what is not working for me. //edit: I searched for 'ExpressSecurity'. My app is an offline .NET Framework Windows Forms project. It is used to encrypt/decrypt things locally (sent using other means). – XpoizonEximus May 06 '21 at 13:31
  • The encrypt key and decrypt key has to be the same. Every time you call new RSACng a new key is generated so you decrypt key is different from the encrypt key. – jdweng May 06 '21 at 14:26
  • If that's the case, which is the role of the Key Storage Provider? – XpoizonEximus May 06 '21 at 19:54
  • If your pre-created key is 2048 bits, assigning the KeySize property at best does nothing. If it wasn’t, assigning the property detached from your key and made a new random one. – bartonjs Jan 12 '22 at 12:56

1 Answers1

0

If you want to create an app that does symmetric and asymmetric encryption and decryption, You can try integrating ExpressSecurity library via NuGet

More info: https://github.com/sangeethnandakumar/Express-Security-Library

AES - Symetric Encryption (For files)

var password = "sangeeth123";
var inputPath = "C:\sample.txt";
var outputPath = "C:\sample.txt.aes";

//AES Encription
AESEncription.AES_Encrypt(inputPath, password);
//AES Description
AESEncription.AES_Decrypt(outputPath, password);

RSA - Asymmetric Encryption (For strings and text)

//Generate Keys
var publicKeyPath = "C:\public_key.rsa";
var privateKeyPath = "C:\private_key.rsa";
RSAEncription.MakeKey(publicKeyPath, privateKeyPath);

var input = "sangeeth"

//RSA Encription
var ciphertext = RSAEncription.EncryptString(input, publicKeyPath);
//RSA Description
input = RSAEncription.DecryptString(ciphertext, privateKeyPath);
Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23