根據這篇文章,256位解密密鑰和一個512位的驗證密鑰生成和他們一起去Rijndael算法,所以你需要它的C#實現這已經是在System.Security.Cryptography
命名空間。
下面是兩個函數做的工作:
private static string EncryptString(string clearText,
string strKey, string strIv) {
byte[] plainText = Encoding.UTF8.GetBytes(clearText);
byte[] key = Encoding.UTF8.GetBytes(strKey);
// a simple initialization vector
byte[] iv = Encoding.UTF8.GetBytes(strIv);
RijndaelManaged rijndael = new RijndaelManaged();
//Define the Mode
rijndael.Mode = CipherMode.CBC;
ICryptoTransform aesEncryptor = rijndael.CreateEncryptor(key, iv);
MemoryStream ms = new MemoryStream();
// writing data to MemoryStream
CryptoStream cs = new CryptoStream(ms, aesEncryptor, CryptoStreamMode.Write);
cs.Write(plainText, 0, plainText.Length);
cs.FlushFinalBlock();
byte[] CipherBytes = ms.ToArray();
ms.Close();
cs.Close();
return Convert.ToBase64String(CipherBytes);
}
和:
public static string DecryptString(string cipherText,
string strKey, string strIv) {
byte[] cipheredData = Convert.FromBase64String(cipherText);
byte[] key = Encoding.UTF8.GetBytes(strKey);
byte[] iv = Encoding.UTF8.GetBytes(strIv);
RijndaelManaged rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv);
MemoryStream ms = new MemoryStream(cipheredData);
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
byte[] plainTextData = new byte[cipheredData.Length];
int decryptedByteCount = cs.Read(plainTextData, 0, plainTextData.Length);
ms.Close();
cs.Close();
return Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount);
}
而關於你的第二個問題,產生的machineKey是一個十六進制字符串,它是人類可讀的,但你不能圖出來。
我希望這個幫助。
這些與機器鍵匹配?字符串strKey,字符串strIv – user441365
strKey是您的機器密鑰。代表Initialization Vector字符串的strIv是一個簡單的字符串,你可以傳遞任何你想要的值。 – amrfaissal
可以硬編碼strIv嗎?另外,strKEy應該匹配validationKey或decryptionKey? – user441365