Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用Windows平台(UWP)中的加密/解密存在数据错误(循环冗余校验)_C#_Encryption_Uwp - Fatal编程技术网

C# 通用Windows平台(UWP)中的加密/解密存在数据错误(循环冗余校验)

C# 通用Windows平台(UWP)中的加密/解密存在数据错误(循环冗余校验),c#,encryption,uwp,C#,Encryption,Uwp,我在我的通用Windows平台(UWP)应用程序中使用DES加密/解密算法。数据加密工作正常,但解密有错误: 这是我的密码: private static byte[] IV = { 12, 11, 12, 55, 0, 108, 121, 54 }; private static string stringKey = "SA/DF@#asx."; private static BinaryStringEncoding encoding; private static byte[] keyByt

我在我的通用Windows平台(UWP)应用程序中使用DES加密/解密算法。数据加密工作正常,但解密有错误:

这是我的密码:

private static byte[] IV = { 12, 11, 12, 55, 0, 108, 121, 54 };
private static string stringKey = "SA/DF@#asx.";
private static BinaryStringEncoding encoding;
private static byte[] keyByte;
private static SymmetricKeyAlgorithmProvider objAlg;
private static CryptographicKey Key;
加密:

public static string Encrypt(String strMsg)
{
     IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg,encoding);
     IBuffer buffEncrypt = CryptographicEngine.Encrypt(Key, buffMsg, IV.AsBuffer());
     return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
解密:

public static string Decrypt(String strMsg)
{
     var bb = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);
     IBuffer buffEncrypt = CryptographicEngine.Decrypt(Key, bb, IV.AsBuffer());
     return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
而解密有以下错误:

Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)

怎么了?

只要看一下代码,似乎您已经将加密结果(通常是二进制blob转换为Base64字符串,这很好)。但是,当你解密时,你没有完全撤销Base64编码,而是将其视为一个二进制blob,难怪解码会失败。

好的,我终于找到了我的答案:在定位解密步骤时出错了

正确的算法:

private static byte[] IV = { 12, 11, 12, 55, 0, 108, 121, 54 };
private static string stringKey = "SA/DF@#asx.";
private static BinaryStringEncoding encoding;
private static byte[] keyByte;
private static SymmetricKeyAlgorithmProvider objAlg;
private static CryptographicKey Key;
UWP中的DES加密:

public static string Encrypt(String strMsg)
{
     IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg,encoding);
     IBuffer buffEncrypt = CryptographicEngine.Encrypt(Key, buffMsg, IV.AsBuffer());
     return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
以及解密步骤:

public static string Decrypt(String strMsg)
{
    Byte[] bb = Convert.FromBase64String(strMsg);
    IBuffer buffEncrypt = CryptographicEngine.Decrypt(Key, bb.AsBuffer(), IV.AsBuffer());
    return CryptographicBuffer.ConvertBinaryToString(encoding, buffEncrypt);
}

什么是
IV
编码是否与输入字符串匹配?