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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With