我一直在尝试使用 Windows 8(Windows 商店应用程序)中的 Windows.security.cryptography API 对字符串进行基本对称密钥加密(目前)。
在线查看了各种示例,但在所有示例中,代码都失败了,对我来说出现了意外的密钥长度异常。
static byte[] cKey = { (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E' };
static byte[] cIV = { (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E' };
public static string Encrypt(String guidOriginal)
{
IBuffer encrypted;
IBuffer buffer;
IBuffer iv = null;
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
IBuffer keymaterial = CryptographicBuffer.CreateFromByteArray(cKey);
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
iv = CryptographicBuffer.CreateFromByteArray(cIV);
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
buffer = CryptographicBuffer.CreateFromByteArray( encoding.GetBytes(guidOriginal));
encrypted = Windows.Security.Cryptography.Core.CryptographicEngine.Encrypt(key, buffer, iv);
return CryptographicBuffer.EncodeToBase64String(encrypted);
}
^^ 以上是我尝试过的代码之一(使用了来自http://social.msdn.microsoft.com/Forums/en-ZA/winappswithcsharp/thread/b541a08a-d3cd-4e21-8d21- 7ed80749cb23),失败于
CryptographicKey key = algorithm.CreateSymmetricKey(keymaterial);
除了:ArgumentException