Vusal Mastaliyev

Vusal Mastaliyev

  • 1.6k
  • 30
  • 6.1k

OaepSHA256 - Specified padding mode is not valid for this algorithm

Mar 7 2023 5:16 AM

Hi. This is my code. 

public SLQ()
{
   InitializeComponent();
   rsa = new RSACryptoServiceProvider();
   rsa.KeySize = 2048;
}

   RSACryptoServiceProvider rsa;
   string fileName;


public void SaveFile(RSACryptoServiceProvider rsa)
{
   string path = Application.StartupPath;
   fileName = Path.Combine(path, "PCInfo.txt");

   using (StreamWriter sw = new StreamWriter(fileName))
   {
      string OSInfo = "OS Info: " + WritePCInfo.OSInfo;
      string BiosSerial = "BIOS Serial: " + WritePCInfo.BiosSerial;
      string HDDSerial = "HDD Serial: " + WritePCInfo.HDDSerial;
      string MotherboardSerial = "Motherboard Serial: " + WritePCInfo.MotherboardSerial;
      string MacAdress = "MAC Address: " + WritePCInfo.MacAdress;

      // Concatenate the strings
      string DATAS = "";
      string[] infoArray = { OSInfo, BiosSerial, HDDSerial, MotherboardSerial, MacAdress };
   
      for (int i = 0; i < infoArray.Length; i++)
        {
            DATAS += infoArray[i];
            byte[] data = Encoding.UTF8.GetBytes(DATAS);
            byte[] encryptedData = rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256);
            string base64EncryptedData = Convert.ToBase64String(encryptedData);
            sw.WriteLine(base64EncryptedData);
        }
   }
}
Error message: Specified padding mode is not valid for this algorithm.  

It works correctly when I replace "RSAEncryptionPadding.OaepSHA256" with "RSAEncryptionPadding.Pkcs1". I want to encrypt with OaepSHA256 or OaepSHA512. How can I do it?

I am using .Net framework 4.8


Answers (5)