Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
AnswerRe: Valid filename????????? Pin
Vasudevan Deepak Kumar1-Mar-06 22:27
Vasudevan Deepak Kumar1-Mar-06 22:27 
QuestionQuery on HttpWebRequset and Response Pin
fearless stallion1-Mar-06 18:40
fearless stallion1-Mar-06 18:40 
AnswerRe: Query on HttpWebRequset and Response Pin
Nadia Monalisa1-Mar-06 19:40
Nadia Monalisa1-Mar-06 19:40 
GeneralRe: Query on HttpWebRequset and Response Pin
fearless stallion1-Mar-06 22:56
fearless stallion1-Mar-06 22:56 
QuestionProgress Percent of SMTP Client Pin
Nadia Monalisa1-Mar-06 18:09
Nadia Monalisa1-Mar-06 18:09 
QuestionC# VS2005 - Installing deployed application to an specific path Pin
JC Carmo1-Mar-06 17:48
JC Carmo1-Mar-06 17:48 
QuestionC# VS 2005 - Crystal reports question Pin
JC Carmo1-Mar-06 17:47
JC Carmo1-Mar-06 17:47 
QuestionCryptographicException Message="Bad Data.\r\n" Pin
Dally33671-Mar-06 17:44
Dally33671-Mar-06 17:44 
Hello,

I have been trying to use this triple des encryption code to encrypt then decrypt passwords. Basically I write the password out to a file, read it in then decrypt it. Using this code works fine while the app is running. Once I exit the app and read the encrypted password in the DecryptPassword method throws the error CryptographicException Message="Bad Data.\r\n" on the FlushFinalBlock() method. I found some of this code here and the other peice from Microsoft. Both work exactly the same way, while the app is running they can encrypt/decrypt strings but if I exit the app and start again, read the data from my file they both get the same error. I have attached the class with all the code and the exception. If anyone can help it would be greatly appreciated.

code:
================
using System;
using System.Collections.Generic;
using System.Text;

namespace notify
{
class crypto
{
private static System.Security.Cryptography.SymmetricAlgorithm TripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

public void InitializeKeys()
{
//TripleDES.Key = System.Convert.FromBase64String("b4r5OegLw8Mz8wlMJ8isuSGSev1IgzMA");
//TripleDES.IV = System.Convert.FromBase64String("6qu72vdn0Fs=");
}

public string EncryptPassword(string ClearTextPassword)
{
TripleDES.Key = System.Convert.FromBase64String("b4r5OegLw8Mz8wlMJ8isuSGSev1IgzMA");
TripleDES.IV = System.Convert.FromBase64String("6qu72vdn0Fs=");
///////////////// Code Project Code /////////////////////////////////////////////////////////////////////////////////////
string encrypted;
byte[] Code = ASCIIEncoding.ASCII.GetBytes(ClearTextPassword);

encrypted = Convert.ToBase64String(TripleDES.CreateEncryptor().TransformFinalBlock(Code, 0, Code.Length));

return encrypted;
/////////////////// Microsoft Code ////////////////////////////////////////////////////////////////////////////////////////
//System.Security.Cryptography.ICryptoTransform ct;
//System.IO.MemoryStream ms;
//System.Security.Cryptography.CryptoStream cs;
//byte[] byt;
//ct = TripleDES.CreateEncryptor(TripleDES.Key, TripleDES.IV);
//byt = System.Text.Encoding.UTF8.GetBytes(ClearTextPassword);

//ms = new System.IO.MemoryStream();
//cs = new System.Security.Cryptography.CryptoStream(ms, ct, System.Security.Cryptography.CryptoStreamMode.Write);
//cs.Write(byt, 0, byt.Length);
//cs.FlushFinalBlock();
//cs.Close();
//return System.Convert.ToBase64String(ms.ToArray());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

public string DecryptPassword(string EncryptedPassword)
{
TripleDES.Key = System.Convert.FromBase64String("b4r5OegLw8Mz8wlMJ8isuSGSev1IgzMA");
TripleDES.IV = System.Convert.FromBase64String("6qu72vdn0Fs=");
///////////////// Code Project Code /////////////////////////////////////////////////////////////////////////////////////
string decrypted;
byte[] Code = Convert.FromBase64String(EncryptedPassword);

decrypted = ASCIIEncoding.ASCII.GetString(TripleDES.CreateDecryptor() .TransformFinalBlock(Code, 0, Code.Length));

return decrypted;
/////////////////// Microsoft Code ////////////////////////////////////////////////////////////////////////////////////////
//System.Security.Cryptography.ICryptoTransform ct;
//System.IO.MemoryStream ms;
//System.Security.Cryptography.CryptoStream cs;
//byte[] byt;

//ct = TripleDES.CreateDecryptor(TripleDES.Key, TripleDES.IV);

//byt = System.Convert.FromBase64String(EncryptedPassword);

//ms = new System.IO.MemoryStream();
//cs = new System.Security.Cryptography.CryptoStream(ms, ct, System.Security.Cryptography.CryptoStreamMode.Write);
//cs.Write(byt, 0, byt.Length);
//cs.FlushFinalBlock();

//cs.Close();

//return System.Text.Encoding.UTF8.GetString(ms.ToArray());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

====================================
exception:
====================================
System.Security.Cryptography.CryptographicException was unhandled
Message="Bad Data.\r\n"
Source="mscorlib"
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
at System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)
at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at notify.crypto.DecryptPassword(String EncryptedPassword) in C:\Data\Visual Studio 2005\Projects\notify\notify\crypto.cs:line 53
at notify.frmMain.frmMain_Load(Object sender, EventArgs e) in C:\Data\Visual Studio 2005\Projects\notify\notify\frmMain.cs:line 232
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at notify.Program.Main() in C:\Data\Visual Studio 2005\Projects\notify\notify\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Darryl
AnswerRe: CryptographicException Message="Bad Data.\r\n" Pin
Dally33672-Mar-06 5:34
Dally33672-Mar-06 5:34 
Questionout of memory -image Pin
leelaraj1-Mar-06 17:01
leelaraj1-Mar-06 17:01 
QuestionMouseOver NotifyIcon Pin
jgallen231-Mar-06 14:29
jgallen231-Mar-06 14:29 
QuestionWindows DataGrid Event Notification Pin
Subrahmanyam K1-Mar-06 13:43
Subrahmanyam K1-Mar-06 13:43 
AnswerRe: Windows DataGrid Event Notification Pin
microsoc1-Mar-06 14:33
microsoc1-Mar-06 14:33 
QuestionThreads Pin
BadKarma1-Mar-06 12:30
BadKarma1-Mar-06 12:30 
AnswerRe: Threads Pin
George L. Jackson1-Mar-06 12:41
George L. Jackson1-Mar-06 12:41 
GeneralRe: Threads Pin
BadKarma1-Mar-06 13:01
BadKarma1-Mar-06 13:01 
AnswerRe: Threads Pin
Guffa1-Mar-06 13:33
Guffa1-Mar-06 13:33 
GeneralRe: Threads Pin
George L. Jackson1-Mar-06 13:41
George L. Jackson1-Mar-06 13:41 
GeneralRe: Threads Pin
BadKarma1-Mar-06 20:27
BadKarma1-Mar-06 20:27 
Questionconvert c++ to C# Pin
eyalso1-Mar-06 10:44
eyalso1-Mar-06 10:44 
AnswerRe: convert c++ to C# Pin
James Gupta1-Mar-06 11:59
professionalJames Gupta1-Mar-06 11:59 
GeneralRe: convert c++ to C# Pin
soma4201-Mar-06 12:15
soma4201-Mar-06 12:15 
GeneralRe: convert c++ to C# Pin
George L. Jackson1-Mar-06 12:50
George L. Jackson1-Mar-06 12:50 
Questionreference and not copy Pin
spin vector1-Mar-06 10:14
spin vector1-Mar-06 10:14 
AnswerRe: reference and not copy Pin
Judah Gabriel Himango1-Mar-06 10:42
sponsorJudah Gabriel Himango1-Mar-06 10:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.