Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-08-11
9
Medium Priority
?
271 Views
Last Modified: 2014-08-13
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
0
Comment
Question by:Wayne Barrt
  • 5
  • 4
9 Comments
 

Author Comment

by:Wayne Barrt
ID: 40254589
Sorry.. i forgot to mention in my orginal post, the exception error fires when trying to execute "decStream.FlushFinalBlock()" in function DecryptData
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40255761
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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40255843
Erm, oops, after running it again, I get a different output each time so something else must be going on...
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40255867
Could it be that you need to call the
c_DES = New Simple3Des

Open in new window

with a key?
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40255886
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
 

Author Comment

by:Wayne Barrt
ID: 40256148
HI Robert,  (obviously i need to study more on encryption).. Thank you very very much!!   You the man!!
0
 

Author Comment

by:Wayne Barrt
ID: 40257241
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
 

Author Comment

by:Wayne Barrt
ID: 40256193
.. 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
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40257242
Objecting because the comments clearly suggest the solution was good but the wrong comment was selected.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question