AES 256加密:公钥和私钥我怎么能生成和使用它。NET [英] AES 256 Encryption: public and private key how can I generate and use it .net
本文介绍了AES 256加密:公钥和私钥我怎么能生成和使用它。NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于AES 256加密:
Regarding AES 256 Encryption:
- 什么是公钥和私钥
- 如何我可以生成这两个键?
- 如何使用公众对数据进行加密?
- 我如何使用私人解密数据?
- What is the public and private key?
- How can I generate these two keys?
- How can I use the public to encrypt the data?
- How can I use the private to decrypt the data?
推荐答案
在.NET中,可以创建你的密钥对这样的:
In .Net, you can create your key pair like this:
public static Tuple<string, string> CreateKeyPair()
{
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
return new Tuple<string, string>(privateKey, publicKey);
}
您就可以使用您的公钥来,像这样加密的消息:
You can then use your public key to encrypt a message like so:
public static byte[] Encrypt(string publicKey, string data)
{
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));
byte[] plainBytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
return encryptedBytes;
}
和使用您的私钥来解密这样的:
And use your private key to decrypt like this:
public static string Decrypt(string privateKey, byte[] encryptedBytes)
{
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey));
byte[] plainBytes = rsaProvider.Decrypt(encryptedBytes, false);
string plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);
return plainText;
}
这篇关于AES 256加密:公钥和私钥我怎么能生成和使用它。NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文