I am trying to get some very basic encryption going and I think it is half working.
I have the following
public class Encryption
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
private byte[] Output = null;
public byte[] Encrypt( byte[] Data )
{
Output = rsa.Encrypt(Data,false);
return(Output);
}
public byte[] Decrypt( byte[] Data )
{
Output = rsa.Decrypt(Data,false);
return(Output);
}
}
I then call this from my other form
I then pass a byte Array to these functions and I receive an Encrypted byte Array in return. I then save this to a file and everything seems to be working, but when I try and decrypt it I get a error saying "Bad Data"?
Can someone please let me know what I am doing wrong here?
It seems easy enough but just doesn't want to work.
My code on my form looks like this
**To Encrypt Values**
Encryption enc = new Encryption();
Encoding encode = Encoding.GetEncoding(0);
string Server = dtServer.Text;
byte[] bServer = encode.GetBytes(Server);
bServer = enc.Encrypt(bServer);
FileStream fs = new FileStream(nFile, FileMode.Truncate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("$SERVER$=$" + encode.GetString(bServer) + "$"
sw.Close();
**To Decrypt Values**
Encryption enc = new Encryption();
Encoding encode = Encoding.GetEncoding(0);
FileStream fs = new FileStream(nFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string rLine;
string[] aLine;
while( sr.Read() > 0 )
{
rLine = sr.ReadLine();
aLine = rLine.Split('$');
if( aLine.GetUpperBound(0) > 0 )
{
if( aLine[0] == "SERVER" )
{
byte[] stest = encode.GetBytes(aLine[2]);
stest = enc.Decrypt(stest);//ERROR HERE
dtServer.Text = encode.GetString(stest);
}
}
}
sr.Close();
fs.Close();
Thanks,
Gavin
I have the following
public class Encryption
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
private byte[] Output = null;
public byte[] Encrypt( byte[] Data )
{
Output = rsa.Encrypt(Data,false);
return(Output);
}
public byte[] Decrypt( byte[] Data )
{
Output = rsa.Decrypt(Data,false);
return(Output);
}
}
I then call this from my other form
I then pass a byte Array to these functions and I receive an Encrypted byte Array in return. I then save this to a file and everything seems to be working, but when I try and decrypt it I get a error saying "Bad Data"?
Can someone please let me know what I am doing wrong here?
It seems easy enough but just doesn't want to work.
My code on my form looks like this
**To Encrypt Values**
Encryption enc = new Encryption();
Encoding encode = Encoding.GetEncoding(0);
string Server = dtServer.Text;
byte[] bServer = encode.GetBytes(Server);
bServer = enc.Encrypt(bServer);
FileStream fs = new FileStream(nFile, FileMode.Truncate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("$SERVER$=$" + encode.GetString(bServer) + "$"
sw.Close();
**To Decrypt Values**
Encryption enc = new Encryption();
Encoding encode = Encoding.GetEncoding(0);
FileStream fs = new FileStream(nFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string rLine;
string[] aLine;
while( sr.Read() > 0 )
{
rLine = sr.ReadLine();
aLine = rLine.Split('$');
if( aLine.GetUpperBound(0) > 0 )
{
if( aLine[0] == "SERVER" )
{
byte[] stest = encode.GetBytes(aLine[2]);
stest = enc.Decrypt(stest);//ERROR HERE
dtServer.Text = encode.GetString(stest);
}
}
}
sr.Close();
fs.Close();
Thanks,
Gavin