By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,874 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Encrypt Querystring

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.