469,582 Members | 2,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,582 developers. It's quick & easy.

Encrypt Querystring

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??
Jun 4 '06 #1
4 3964
To work around what? You got to show some code if you want help with it.

Islamegy® wrote:
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??

Jun 4 '06 #2
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" <gu***@guffa.com> wrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...
To work around what? You got to show some code if you want help with it.

Islamegy® wrote:
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??

Jun 4 '06 #3
I have a pretty simple sample of this:

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

--

Andrew Robinson

"Islamegy®" <Is******@Private.4me> wrote in message
news:u3**************@TK2MSFTNGP03.phx.gbl...
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" <gu***@guffa.com> wrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...
To work around what? You got to show some code if you want help with it.

Islamegy® wrote:
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??


Jun 4 '06 #4
That looks plain and simple. How do you use the code?

Islamegy® wrote:
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" <gu***@guffa.com> wrote in message
news:u4**************@TK2MSFTNGP03.phx.gbl...
To work around what? You got to show some code if you want help with it.

Islamegy® wrote:
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??


Jun 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jawahar Rajan | last post: by
1 post views Thread by Matt | last post: by
reply views Thread by VB Programmer | last post: by
3 posts views Thread by TJS | last post: by
1 post views Thread by Peter | last post: by
4 posts views Thread by Gary Townsend (Spatial Mapping Ltd.) | last post: by
1 post views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.