我尝试使用 .NET v8 在 C# 中实现 RSA 加密功能。加密测试顺利,但解密测试时出现意外异常。
错误详情:
System.Security.Cryptography.CryptographicException
HResult=0xC100000D
Message=Unknown error (0xc100000d)
Source=System.Security.Cryptography
StackTrace:
at Interop.BCrypt.BCryptEncryptRsa(SafeBCryptKeyHandle key, ReadOnlySpan`1 source, Span`1 destination, Void* pPaddingInfo, BCryptEncryptFlags dwFlags)
at System.Security.Cryptography.RSABCrypt.TryEncrypt(ReadOnlySpan`1 data, Span`1 destination, RSAEncryptionPadding padding, Int32& bytesWritten)
at System.Security.Cryptography.RSA.Encrypt(ReadOnlySpan`1 data, Span`1 destination, RSAEncryptionPadding padding)
at System.Security.Cryptography.RSABCrypt.Encrypt(Byte[] data, RSAEncryptionPadding padding)
at FileCryptography.FileEncryptionUtility.Encrypt(Byte[] data, String pemKey) in Class1.cs:line 51
at FileCryptography.FileEncryptionUtility.EncryptFile(String filePath, String pemKey) in Class1.cs:line 25
at FileCryptography.FileEncryptionUtility.File_encryption(Boolean Is_encrypt, String File_path, String PEMkey) in Class1.cs:line 14
at Cry.Form.File_crypt(Boolean Is_encrypt, String File_path) in Form1.cs:line 114
at Cry.Form.Form_Load(Object sender, EventArgs e) in Form1.cs:line 203
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Control.CreateControl(Boolean ignoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)
这是我的代码:
Class1.cs
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace FileCryptography
{
public class FileEncryptionUtility
{
public static void File_encryption(bool Is_encrypt, string File_path, string PEMkey)
{
if (Is_encrypt)
{
EncryptFile(File_path, PEMkey);
}
else
{
DecryptFile(File_path, PEMkey);
}
}
private static void EncryptFile(string filePath, string pemKey)
{
byte[] data = File.ReadAllBytes(filePath);
byte[] encryptedData = Encrypt(data, pemKey);
File.Delete(filePath);
string p1 = Path.GetDirectoryName(filePath);
string p2 = Path.GetFileNameWithoutExtension(filePath);
string p3 = Path.GetExtension(filePath);
string p4 = ".rwnenc";
string p = $"{p1}{p2}{p3}{p4}";
File.WriteAllBytes(p, encryptedData);
File.Delete(filePath);
}
private static void DecryptFile(string filePath, string pemKey)
{
byte[] encryptedData = File.ReadAllBytes(filePath);
byte[] decryptedData = Decrypt(encryptedData, pemKey);
string directory = Path.GetDirectoryName(filePath);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
filenameWithoutExtension = filenameWithoutExtension.Replace(".rwnenc", "");
string newFilePath = Path.Combine(directory, filenameWithoutExtension);
File.WriteAllBytes(newFilePath, decryptedData);
}
private static byte[] Encrypt(byte[] data, string pemKey)
{
using (RSA rsa = RSA.Create())
{
rsa.ImportFromPem(pemKey.ToCharArray());
return rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA512);
}
}
private static byte[] Decrypt(byte[] data, string pemKey)
{
using (RSA rsa = RSA.Create())
{
rsa.ImportFromPem(pemKey.ToCharArray());
return rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA512);
}
}
}
}
我希望程序能够顺利运行,不会遇到指示
0xC100000D
的错误
Unknown Error
我需要紧急答复。
如果您使用公钥进行加密,则必须使用相应的私钥进行解密。
确保密钥解析正确。如果 PEM 密钥包含换行符或格式问题,可能会导致导入失败。