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
Alexander OliverPosted Feb 26, 2024, 2:26 PM
?The blunders message "Specified padding mode isn't always legitimate for this set of rules" typically occurs in cryptography or encryption-related contexts. It suggests that the padding mode detailed for an encryption algorithm isn't well suited or supported by way of that set of rules.To resolve this trouble, you'll want to check the documentation or specs of the encryption algorithm you are using and ensure that you're the use of a padding mode this is supported by means of that set of rules. Common padding modes encompass PKCS7, ISO 10126, and ZeroPadding. Ensure that the padding mode you've selected matches the necessities of the encryption algorithm you are enforcing.
https://bestessaywriter.co.uk/marketing-essay-help
Chitra MishraPosted Dec 14, 2023, 11:01 AM
This modification ensures that each piece of data is encrypted separately before being written to the file. The EncryptAndWrite method is introduced to handle the encryption process for each string individually.salesforce cpq training
salesforce cpq trainingBen JonsonPosted Nov 16, 2023, 5:24 AM
Hello, In my opinion this issue with the padding mode when using OAEP with SHA-256 in your RSA encryption. The problem lies in the placement of the loop that encrypts the data inside the loop that concatenates the strings. Each string should be encrypted separately before being concatenated. Here's a modified version of your SaveFile method:
This modification ensures that each piece of data is encrypted separately before being written to the file. The EncryptAndWrite method is introduced to handle the encryption process for each string individually.
https://stackoverflow.com/questions/46533454/specified-padding-mode-is-not-valid-for-this-algorithm-c-sharp-system-securihybris
I hope this will help you. Regards Ben
Amit MohantyPosted Mar 7, 2023, 6:22 AM
You're running on .Net Framework 4.8 on Windows just replace RSACryptoServiceProvider with RSACng.
Modify your code as below
Or you can use this
Naimish MakwanaPosted Mar 7, 2023, 6:14 AM
Please check below link
https://stackoverflow.com/a/56508137/3054222