Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encryption/Decryption in Universal Windows Platform (UWP) has data error (cyclic redundancy check)

Tags:

c#

encryption

uwp

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?

like image 590
n.y Avatar asked Apr 01 '26 16:04

n.y


1 Answers

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.

like image 105
Andrew Au Avatar answered Apr 03 '26 15:04

Andrew Au



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!