WP 8.1 和 WinRT
public static string Encrypt2(string password, string plainText)
{
IBuffer passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
IBuffer iv = WindowsRuntimeBuffer.Create(16);
SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
// create symmetric key from derived password key
CryptographicKey symmKey = symProvider.CreateSymmetricKey(passwordBuffer);
// encrypt data buffer using symmetric key and derived salt material
IBuffer encryptedBuffer = CryptographicEngine.Encrypt(symmKey, plainBuffer, iv);
string encryptedText = CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
return encryptedText;
}
public static string Decrypt2(string password, string encryptedText)
{
IBuffer passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
IBuffer encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedText);
IBuffer iv = WindowsRuntimeBuffer.Create(16);
SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
// create symmetric key from derived password material
CryptographicKey symmKey = symProvider.CreateSymmetricKey(passwordBuffer);
// encrypt data buffer using symmetric key and derived salt material
IBuffer plainBuffer = CryptographicEngine.Decrypt(symmKey, encryptedBuffer, iv);
string plainText = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, plainBuffer);
return plainText;
}
其他一切
public static string Encrypt(string password, string plainText)
{
using (var aes = new AesManaged())
{
aes.Key = Encoding.UTF8.GetBytes(password);
aes.IV = new byte[16];
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
byte[] plainBuffer = Encoding.UTF8.GetBytes(plainText);
using (MemoryStream input = new MemoryStream(plainBuffer))
using (MemoryStream output = new MemoryStream())
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (CryptoStream cs = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
{
input.CopyTo(cs);
cs.FlushFinalBlock();
string encryptedText = Convert.ToBase64String(output.GetBuffer(), 0, (int)output.Length);
return encryptedText;
}
}
}
public static string Decrypt(string password, string encryptedText)
{
using (var aes = new AesManaged())
{
aes.Key = Encoding.UTF8.GetBytes(password);
aes.IV = new byte[16];
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
using (MemoryStream input = new MemoryStream(encryptedBuffer))
using (MemoryStream output = new MemoryStream())
using (ICryptoTransform decryptor = aes.CreateDecryptor())
using (CryptoStream cs = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
{
cs.CopyTo(output);
string plainText = Encoding.UTF8.GetString(output.GetBuffer(), 0, (int)output.Length);
return plainText;
}
}
}
请注意这里有一个小问题:我使用UTF8 来编码password 和strPlainText,但是python 使用bytestring 来编码所有内容,而bytestring 编码不可知(见What is a Python bytestring?)。
使用示例:
string result = Encrypt("abcdefghijklmnopqrstuvwxyz123456", "123"); // 5pRIk9MDE3z9caf/ayilIA==
string decrypted = Decrypt("abcdefghijklmnopqrstuvwxyz123456", result); // 123
该方法返回与您的示例相同的加密结果。
这段代码的一个小问题是IV(初始化向量)是用一个空的biffer(Python中的new byte[16]和16 * '\x00')初始化的。这是一个“坏习惯”。即使使用 CBC 密码模式也被认为是“不好的”。