.net 中的混合密码系统实现。错误指定的密钥不是该算法的有效大小
Hybrid cryptosystem implementation in .net. Error Specified key is not a valid size for this algorithm
我正在尝试实现 https://en.wikipedia.org/wiki/Hybrid_cryptosystem
中提到的混合密码系统
目前我已经实现了以下算法
private void button1_Click(object sender, EventArgs e)
{
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));
string symmericKey = "Kamran12";
txtEncryptedData.Text = EncryptData(txtInputData.Text, symmericKey);
string encryptedsymmetrickey = EncryptData(symmericKey, publicKey); //error line
//string decryptsymmetrickey = encryptedsymmetrickey + privateKey;
//string decrypteddata = encryptedData + decryptsymmetrickey;
}
public string EncryptData(string data, string key)
{
string encryptedData = null;
byte[] buffer = Encoding.UTF8.GetBytes(data);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(key)
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream stmCipherText = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
encryptedData = Encoding.UTF8.GetString(stmCipherText.ToArray());
}
}
return encryptedData;
}
但是出现错误指定的密钥不是该算法的有效大小。在加密对称密钥时
您正在尝试使用(不安全的)DES 算法和 RSA public 密钥进行加密。这总是会失败,DESCryptoServiceProvider
不接受 RSA 密钥。为此你需要一个 RSACryptoServiceProvider
。
您可能需要考虑使用已经实现混合加密(PGP、CMS 或一种专有协议)的特定库。您的解决方案最终可能 运行,但 不会 是安全的。
我正在尝试实现 https://en.wikipedia.org/wiki/Hybrid_cryptosystem
中提到的混合密码系统目前我已经实现了以下算法
private void button1_Click(object sender, EventArgs e)
{
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));
string symmericKey = "Kamran12";
txtEncryptedData.Text = EncryptData(txtInputData.Text, symmericKey);
string encryptedsymmetrickey = EncryptData(symmericKey, publicKey); //error line
//string decryptsymmetrickey = encryptedsymmetrickey + privateKey;
//string decrypteddata = encryptedData + decryptsymmetrickey;
}
public string EncryptData(string data, string key)
{
string encryptedData = null;
byte[] buffer = Encoding.UTF8.GetBytes(data);
DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
{
Key = new UTF8Encoding().GetBytes(key)
};
desCryptSrvckey.IV = desCryptSrvckey.Key;
using (MemoryStream stmCipherText = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(buffer, 0, buffer.Length);
cs.FlushFinalBlock();
encryptedData = Encoding.UTF8.GetString(stmCipherText.ToArray());
}
}
return encryptedData;
}
但是出现错误指定的密钥不是该算法的有效大小。在加密对称密钥时
您正在尝试使用(不安全的)DES 算法和 RSA public 密钥进行加密。这总是会失败,DESCryptoServiceProvider
不接受 RSA 密钥。为此你需要一个 RSACryptoServiceProvider
。
您可能需要考虑使用已经实现混合加密(PGP、CMS 或一种专有协议)的特定库。您的解决方案最终可能 运行,但 不会 是安全的。