I'm using DES Encryption/Decryption algorithm in my Universal Windows Platform (UWP) app. Data encryption work fine but decryption has error:
This is my codes:
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;
Encryption:
public static string Encrypt(String strMsg)
{
IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg,encoding);
IBuffer buffEncrypt = CryptographicEngine.Encrypt(Key, buffMsg, IV.AsBuffer());
return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
Decryption:
public static string Decrypt(String strMsg)
{
var bb = CryptographicBuffer.ConvertStringToBinary(strMsg, encoding);
IBuffer buffEncrypt = CryptographicEngine.Decrypt(Key, bb, IV.AsBuffer());
return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
And decryption has this error :
Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)
What's wrong?
Just looking at the code, it seems that you converted the encrypted result (which is in general, binary blobs into a Base64 string, which is fine). But then, when you decrypt, you didn't quite undo the Base64 encoding, but instead treating this as a binary blob, no wonder the decode will fail.
Ok, I finally found my answer: There is a mistake in locating of decryption steps!
Correct algorithm:
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;
DES Encryption in UWP:
public static string Encrypt(String strMsg)
{
IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(strMsg,encoding);
IBuffer buffEncrypt = CryptographicEngine.Encrypt(Key, buffMsg, IV.AsBuffer());
return CryptographicBuffer.EncodeToBase64String(buffEncrypt);
}
And the Decryption step:
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);
}
User contributions licensed under CC BY-SA 3.0