我盯着这看了很长一段时间,感谢MSDN文档我无法弄清楚到底发生了什么.基本上我正在将光盘中的PFX文件加载到a中,X509Certificate2并尝试使用公钥加密字符串并使用私钥解密.
为什么我感到困惑:加密/解密在我将引用传递给RSACryptoServiceProvider自身时起作用:
byte[] ed1 = EncryptRSA("foo1", x.PublicKey.Key as RSACryptoServiceProvider);
string foo1 = DecryptRSA(ed1, x.PrivateKey as RSACryptoServiceProvider);
Run Code Online (Sandbox Code Playgroud)
但如果出口和传递RSAParameter:
byte[] ed = EncryptRSA("foo", (x.PublicKey.Key as RSACryptoServiceProvider).ExportParameters(false));
string foo = DecryptRSA(ed, (x.PrivateKey as RSACryptoServiceProvider).ExportParameters(true));
Run Code Online (Sandbox Code Playgroud)
...它会抛出"密钥无法在指定状态下使用".尝试将私钥导出到的异常RSAParameter.请注意,生成PFX的证书标记为可导出(即我在创建证书时使用了pe标志).知道导致异常的是什么吗?
static void Main(string[] args)
{
X509Certificate2 x = new X509Certificate2(@"C:\temp\certs\1\test.pfx", "test");
x.FriendlyName = "My test Cert";
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
try
{
store.Add(x);
}
finally
{
store.Close();
}
byte[] ed1 = EncryptRSA("foo1", x.PublicKey.Key as RSACryptoServiceProvider); …Run Code Online (Sandbox Code Playgroud) 我必须加密,存储然后解密大文件.这样做的最佳方式是什么?我听说RSA加密很昂贵,建议使用RSA加密AES密钥,然后使用AES密钥加密大文件.任何带有示例的建议都会很棒.
class Parent{
public string Name{ get; set; }
}
class Child :Parent{
public string address{ get; set; }
}
[TestClass]
class TestClass{
[TestMethod]
public void TestMethod()
{
var c = new Fakes.Child();
c.addressGet = "foo"; // I can see that
c.NameGet = "bar"; // This DOES NOT exists
}
}
Run Code Online (Sandbox Code Playgroud)
如何在上面的代码示例中设置"名称"?
我正在尝试最基本的事情 - 使用公钥加密数据并使用私钥解密:
X509Certificate2 cert = new X509Certificate2(@"c:\temp\CERT\mycert.pfx", "test1");
RSACryptoServiceProvider privateKey = cert.PrivateKey as RSACryptoServiceProvider;
RSACryptoServiceProvider publicKey = cert.PublicKey.Key as RSACryptoServiceProvider;
UnicodeEncoding bytConvertor = new UnicodeEncoding();
byte[] plainData = bytConvertor.GetBytes("Sample data");
byte[] enData = publicKey.Encrypt(plainData, true);
Console.WriteLine("Encrypted Output: {0}", bytConvertor.GetString(enData));
byte[] deData = privateKey.Decrypt(enData, true);
Console.WriteLine("Decrypted Output: {0}", bytConvertor.GetString(deData));
Run Code Online (Sandbox Code Playgroud)
但是最后一行privateKey.Decrypt(...)抛出以下异常:
System.Security.Cryptography.CryptographicException未处理
Message = Bad Key.Run Code Online (Sandbox Code Playgroud)Source=mscorlib StackTrace: at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey) at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) at ConsoleApplication4.Program.Main(String[] args) in …
我正在尝试使用更大的块大小进行AES加密:
private static void EncryptFile(string inFile, RSACryptoServiceProvider rsaPublicKey)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
// Create instance of AesManaged for
// symetric encryption of the data.
aes.KeySize = 256;
// Allocating 64K
aes.BlockSize = 8 * 1024 * 64;
}
}
Run Code Online (Sandbox Code Playgroud)
并达到以下例外:
System.Security.Cryptography.CryptographicException未处理
Message =指定的块大小对此算法无效.
Source = mscorlib StackTrace:位于C:\ Projects\ConsoleApplication4\Program.cs中的ConsoleApplication4.Program.EncryptFile(String inFile,RSACryptoServiceProvider rsaPublicKey)中的System.Security.Cryptography.SymmetricAlgorithm.set_BlockSize(Int32值):第117行
我肯定错过了一些有关任何线索,任何线索?