Exception error " System.Security.Cryptography.Cryptographic {"Bad Data."} running decryption using .Net Framework 4.5

in the example code, when i encrpyt a char string and decrpt that same string, the execution works fine.  But when I use the same SimpleDes class and the same encrption text, but  instead manually save that excrpted text to a text file, (eg: config.ini) and then read back that excrypted string, then the exception error fires. Here are the steps i take.. 1.

encrypt a plain text string.  2. manually copy the encrypted text to a file called config.ini.  3. Using code, retrieve the excrptyed string.  4. using code, try converting the encrpted string back to the plain text string that was used in step 1.  Note that the code I used for SimpleDesc class module, is verbatime from MSDN.  I suspect that the issue is related to encoding and I am not familair enough with encoding and encrption logic and find it hard to understand at this time.  From other blogs in researching this error, one thing noted to check was to this exception is to use .ToBase64String conversion method. That method is used in my example code for both encrpt and decrpt.  I would appreciate any thoughts on what to consider could be the cause of the error.  For running a test, pls note in the code module, Main(), i started both testing logic in there, by using val1 and va2.  The Val2 satement is one i switched on back and forth for my testing method.  Thanks
My-Expert-Exchange-question.txt
Wayne BarrtCEOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne BarrtCEOAuthor Commented:
Sorry.. i forgot to mention in my orginal post, the exception error fires when trying to execute "decStream.FlushFinalBlock()" in function DecryptData
0
Robert SchuttSoftware EngineerCommented:
Where did you get that string in config.ini from? When I execute your code
val1 = c_DES._Encr("barry")

Open in new window

then the value of val1 is:
ma9TeX80LO8j8Rxwy2e0Nw==

Open in new window

When I put that in config.ini, it works perfectly (see screen capture with pinned watches).
capture
0
Robert SchuttSoftware EngineerCommented:
Erm, oops, after running it again, I get a different output each time so something else must be going on...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Robert SchuttSoftware EngineerCommented:
Could it be that you need to call the
c_DES = New Simple3Des

Open in new window

with a key?
0
Robert SchuttSoftware EngineerCommented:
Yes, I think that's the problem, this does work consistently:
        Dim c_DES As Object
        c_DES = New Simple3Des("myPassword")

        strEnrcPwd = ReadPwdFromIniFile("C:\Users\barry\Documents\Visual Studio 2012\Projects\PasswordEncryption\bin\debug")

        val1 = c_DES._Encr("barry")
        REM val2 = c_DES._decr(val1)
        val2 = c_DES._decr(strEnrcPwd)

Open in new window

of course the new value needs to be put in the config.ini file again (password="+duzpoAz6ai0ympELP2Ffw==").

Then again, you will want to choose a password of your own...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne BarrtCEOAuthor Commented:
HI Robert,  (obviously i need to study more on encryption).. Thank you very very much!!   You the man!!
0
Wayne BarrtCEOAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Wayne Barrt's comment #a40256148

for the following reason:

Robert was able to find the issue cause quickly and communicated the answer back to me with print screens and easy to follow instructions.. I really appreciate that.
0
Wayne BarrtCEOAuthor Commented:
.. i found a real good article on technical basics of encryption.. thought to share it out .. <http://www.codeproject.com/Articles/15280/Cryptography-for-the-NET-Framework#_comments>
0
Robert SchuttSoftware EngineerCommented:
Objecting because the comments clearly suggest the solution was good but the wrong comment was selected.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.