問題描述
我正在嘗試實(shí)現(xiàn) https://en.wikipedia.org/wiki 中提到的混合密碼系統(tǒng)/Hybrid_cryptosystem
目前我已經(jīng)實(shí)現(xiàn)了以下算法
At the moment I have implemented following algorithm
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;
}
但收到錯(cuò)誤指定的密鑰不是此算法的有效大小.在加密對(duì)稱密鑰時(shí)
But getting error Specified key is not a valid size for this algorithm. at the time of encrypting the symmetric key
推薦答案
您正在嘗試使用帶有 RSA 公鑰的(不安全的)DES 算法進(jìn)行加密.這總是會(huì)失敗,DESCryptoServiceProvider
不接受 RSA 密鑰.為此,您需要一個(gè) RSACryptoServiceProvider
.
You are trying to encrypt using the (insecure) DES algorithm with an RSA public key. That's always going to fail, DESCryptoServiceProvider
doesn't accept RSA keys. You'd need an RSACryptoServiceProvider
for that.
您可能需要考慮使用已經(jīng)實(shí)現(xiàn)混合加密(PGP、CMS 或其中一種專有協(xié)議)的特定庫.您的解決方案最終可能會(huì)運(yùn)行,但它不是安全的.
You may want to consider using a specific library that already implements hybrid cryptography (PGP, CMS or one of the proprietary protocols). The way you are going at it your solution may run in the end, but it will not be secure.
這篇關(guān)于.net 中的混合密碼系統(tǒng)實(shí)現(xiàn).錯(cuò)誤 指定的密鑰不是此算法的有效大小的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!