Click here to Skip to main content
14,486,466 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am using TripleDES algoritm for encryption and decryption. When I try to decrypt the value it shows Bad data exception
Is there any way to solve this?

What I have tried:

My code:

public string EncryptTripleDES(string PlainText, string key)
            ICryptoTransform encryptor = PCIDSS.CreateDES(key).CreateEncryptor();
            byte[] bytes = Encoding.Unicode.GetBytes(PlainText);
            return Convert.ToBase64String(encryptor.TransformFinalBlock(bytes, 0, bytes.Length));

public string DecryptTripleDES(string CypherText, string key)

            byte[] inputBuffer = Convert.FromBase64String(CypherText);
            return Encoding.Unicode.GetString(PCIDSS.CreateDES(key).CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length));

Updated 25-Jul-19 20:00pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Check your key, check your IV. If they look to be the same as that used for encryption, then start by looking at your input and the code that processes it, and the output encryption. Check it's length as raw byte data (i.e. before conversion to Base64) - it should be the same or longer than the input file.
Then check the data before and after conversion from Base64. Compare that with the raw byte data right after encryption (use a binary or hex comparer).

We can't do any of this for you: we don't even have access to your key, let alone the input or encrypted data!
CPallini 26-Jul-19 1:04am
AkashDaniel 26-Jul-19 1:28am
First i created this as a class when I run it as windows application it works then i convert it to .dll file and add to the reference now it is not working.
OriginalGriff 26-Jul-19 1:46am
And that's supposed to help us fix it for you?
Start by comparing the code for the EXE version withteh DLL version: make sure you are working with the same code and that it compiles clean, not errors, no warnings.
Then check the EXE version still works, and that the same input file produces the same byte array encrypted data.
AkashDaniel 26-Jul-19 4:44am
@originaGriff It works fine but I did nothing, I don't know how its working fine now.
OriginalGriff 26-Jul-19 4:52am
"I did nothing" is unlikely - it's quite possible that you "did something" but didn't consider it relevant to the problem. But ... if you rebuilt your app, that may have cured a problem if the previous compilation failed and you were unknowingly testing against old code for example.

But if it's working, that's good. If it fails again, then you have a real problem - intermittent faults are notoriously difficult to fix! :laugh:
AkashDaniel 26-Jul-19 5:42am
Hahaha If any problem occur again,I will come to you for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100