[英]TripleDES Encryption in Windows Store app (Windows 8.1)
我想在Windows Store应用程序(Windows 8.1)中使用带有ECB密码模式的TripleDES加密来加密某些文本,但是在为对称算法创建密钥时遇到问题。
我想告诉你我目前在.NET 4.5中做什么
public static string EncryptData(string Message, string passphrase)
{
byte[] tpinBytes = System.Text.Encoding.ASCII.GetBytes(Message);
string tpinHex = ByteArrayHelper.ByteArrayToHexString(tpinBytes);
byte[] Results;
byte[] TDESKey = ByteArrayHelper.HexStringToByteArray(passphrase);
TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.Zeros;
byte[] DataToEncrypt = ByteArrayHelper.HexStringToByteArray(tpinHex);
try
{
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
TDESAlgorithm.Clear();
}
return ByteArrayHelper.ByteArrayToHexString(Results);
}
现在,我已经为Windows Store(Windows 8.1)应用程序编写了此代码段。
private static string TripleDESEncryption(string strMsg, string passphrase)
{
String strAlgName = SymmetricAlgorithmNames.TripleDesEcb;
var bytes = System.Text.Encoding.UTF8.GetBytes(strMsg);
string hex = BitConverter.ToString(bytes).Replace("-", "");
// Initialize the initialization vector
IBuffer iv = null;
// Create a buffer that contains the encoded message to be encrypted.
IBuffer DataToEncrypt = CryptographicBuffer.DecodeFromHexString(hex);
// Open a symmetric algorithm provider for the specified algorithm.
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strAlgName);
// Create a symmetric key.
IBuffer TDESKey = CryptographicBuffer.DecodeFromHexString(passphrase);
CryptographicKey key = objAlg.CreateSymmetricKey(TDESKey); // Line of problem.
// Encrypt the data and return.
IBuffer buffEncrypt = CryptographicEngine.Encrypt(key, DataToEncrypt, iv);
return CryptographicBuffer.EncodeToHexString(buffEncrypt);
}
当我匹配TDESKey和EncryptData的值时,它们是相同的。 但是,当我尝试创建对称密钥时(在TDESKey分配之后),就会出现问题。 它给我一个Value的错误,该错误不在预期范围内,并且根据MSDN论坛 ,可能不支持块大小(我无法理解),并且甚至没有该论坛中列出的那些属性(例如SupportedKeyLengths)。
谁能帮助我提供样本或指出我所犯的错误?
WinRT不支持16字节密钥。 尝试使用24字节密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.