Encrypt Querystring

I

Islamegy®

I give up.. I tried everything to encrypt querystring and decrypt it back
but this never success.. i use RSA encryption. I always get excption when
Convert fromBase64String so i tried HttpUtitlity.UrlEncode() but i got bad
data Exception..

Is there anyway to work around this??
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

To work around what? You got to show some code if you want help with it.
 
I

Islamegy®

Here is the code i use for encrypt/decrypt

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Configuration;
public class DataProtection
{
public static RSACryptoServiceProvider rsa;
public static void AssignParameter()
{
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "NMTechnology";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
try
{
rsa = new RSACryptoServiceProvider(cspParams);
}
catch
{
string x = "X";
}
}
public static string Encrypt(string data2Encrypt)
{
AssignParameter();
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
//read plaintext, encrypt it to ciphertext
byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
}
public static void AssignNewKey()
{
AssignParameter();
//provide public and private RSA params
StreamWriter writer = new
StreamWriter(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = rsa.ToXmlString(true);
writer.Write(publicPrivateKeyXML);
writer.Close();
//provide public only RSA params
writer = new
StreamWriter(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = rsa.ToXmlString(false);
writer.Write(publicOnlyKeyXML);
writer.Close();
}
public static string Decrypt(string data2Decrypt)
{
AssignParameter();
byte[] getpassword = Convert.FromBase64String(data2Decrypt);
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
//read ciphertext, decrypt it to plaintext
byte[] plain = rsa.Decrypt(getpassword, false);
return System.Text.Encoding.UTF8.GetString(plain);
}
}
 
A

Andrew Robinson

I have a pretty simple sample of this:

http://blog.binaryocean.com/NETSymmetricEncryption.aspx

--

Andrew Robinson

Islamegy® said:
Here is the code i use for encrypt/decrypt

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Configuration;
public class DataProtection
{
public static RSACryptoServiceProvider rsa;
public static void AssignParameter()
{
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "NMTechnology";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
try
{
rsa = new RSACryptoServiceProvider(cspParams);
}
catch
{
string x = "X";
}
}
public static string Encrypt(string data2Encrypt)
{
AssignParameter();
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
//read plaintext, encrypt it to ciphertext
byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
}
public static void AssignNewKey()
{
AssignParameter();
//provide public and private RSA params
StreamWriter writer = new
StreamWriter(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = rsa.ToXmlString(true);
writer.Write(publicPrivateKeyXML);
writer.Close();
//provide public only RSA params
writer = new
StreamWriter(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = rsa.ToXmlString(false);
writer.Write(publicOnlyKeyXML);
writer.Close();
}
public static string Decrypt(string data2Decrypt)
{
AssignParameter();
byte[] getpassword = Convert.FromBase64String(data2Decrypt);
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
//read ciphertext, decrypt it to plaintext
byte[] plain = rsa.Decrypt(getpassword, false);
return System.Text.Encoding.UTF8.GetString(plain);
}
}



Göran Andersson said:
To work around what? You got to show some code if you want help with it.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

That looks plain and simple. How do you use the code?

Islamegy® said:
Here is the code i use for encrypt/decrypt

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Configuration;
public class DataProtection
{
public static RSACryptoServiceProvider rsa;
public static void AssignParameter()
{
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "NMTechnology";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
try
{
rsa = new RSACryptoServiceProvider(cspParams);
}
catch
{
string x = "X";
}
}
public static string Encrypt(string data2Encrypt)
{
AssignParameter();
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
//read plaintext, encrypt it to ciphertext
byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
return Convert.ToBase64String(cipherbytes);
}
public static void AssignNewKey()
{
AssignParameter();
//provide public and private RSA params
StreamWriter writer = new
StreamWriter(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = rsa.ToXmlString(true);
writer.Write(publicPrivateKeyXML);
writer.Close();
//provide public only RSA params
writer = new
StreamWriter(ConfigurationManager.AppSettings["PublicKey"].ToString());
string publicOnlyKeyXML = rsa.ToXmlString(false);
writer.Write(publicOnlyKeyXML);
writer.Close();
}
public static string Decrypt(string data2Decrypt)
{
AssignParameter();
byte[] getpassword = Convert.FromBase64String(data2Decrypt);
StreamReader reader = new
StreamReader(ConfigurationManager.AppSettings["PrivateKey"].ToString());
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
//read ciphertext, decrypt it to plaintext
byte[] plain = rsa.Decrypt(getpassword, false);
return System.Text.Encoding.UTF8.GetString(plain);
}
}



Göran Andersson said:
To work around what? You got to show some code if you want help with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top