Hi. This is my code.
C#
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);
}
}
}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

Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 7, 2023, 1:14 AM EST
Please check below link
https://stackoverflow.com/a/56508137/3054222
Amit MohantyRank #112 · 20K reputationPosted Mar 7, 2023, 1:22 AM EST
You're running on .Net Framework 4.8 on Windows just replace RSACryptoServiceProvider with RSACng.
Modify your code as below
var rsa = new RSACng(); rsa.KeySize = 2048; byte[] data = Encoding.UTF8.GetBytes(DATAS); byte[] encryptedData = rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256); string base64EncryptedData = Convert.ToBase64String(encryptedData); Console.WriteLine(base64EncryptedData);Or you can use this
// Create an RSA object and generate a key pair. RSA rsa = new RSACng(); RSAParameters publicKey = rsa.ExportParameters(false); RSAParameters privateKey = rsa.ExportParameters(true); // Convert the message to bytes. string message = "Hello, world!"; byte[] messageBytes = Encoding.UTF8.GetBytes(message); // Create an RSAEncryptionPadding object with OAEP padding and SHA-256 as the hash algorithm. RSAEncryptionPadding padding = RSAEncryptionPadding.OaepSHA256; // Encrypt the message using the padding mode. byte[] encryptedMessage = rsa.Encrypt(messageBytes, padding); // Decrypt the message using the private key. byte[] decryptedMessage = rsa.Decrypt(encryptedMessage, padding); // Convert the decrypted message bytes back to a string. string decryptedString = Encoding.UTF8.GetString(decryptedMessage); // Print the original message, encrypted message, and decrypted message. Console.WriteLine($"Original message: {message}"); Console.WriteLine($"Encrypted message: {Convert.ToBase64String(encryptedMessage)}"); Console.WriteLine($"Decrypted message: {decryptedString}");Ben Jonson11 reputationPosted Nov 16, 2023, 12:24 AM EST
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:
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; // Encrypt and write each string separately EncryptAndWrite(OSInfo, rsa, sw); EncryptAndWrite(BiosSerial, rsa, sw); EncryptAndWrite(HDDSerial, rsa, sw); EncryptAndWrite(MotherboardSerial, rsa, sw); EncryptAndWrite(MacAdress, rsa, sw); } } private void EncryptAndWrite(string data, RSACryptoServiceProvider rsa, StreamWriter sw) { byte[] byteData = Encoding.UTF8.GetBytes(data); byte[] encryptedData = rsa.Encrypt(byteData, RSAEncryptionPadding.OaepSHA256); string base64EncryptedData = Convert.ToBase64String(encryptedData); sw.WriteLine(base64EncryptedData); }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
Chitra Mishra6 reputationPosted Dec 14, 2023, 6:01 AM EST
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 trainingAlexander Oliver5 reputationPosted Feb 26, 2024, 9:26 AM EST
?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