Encrypt text to only viewable characters??

  • Thread starter Thread starter Jon Hyland
  • Start date Start date
J

Jon Hyland

Hi all,

I need to take a string value encrypt it in such a way that I can view
the resulting characters, i.e. pass the encrypted value in a query
string.

My problem is the resulting encrypted string contains characters that
are not viewable (extended / localized) and I can't pass them as
parameters in a URL. What I need are alphabetic and numeric
characters only!

I'm betting there is a simple solution to this, but haven't found it
yet.

FYI .. here's my encoding class:


using System.IO;
using System.Text;
using System.Security.Cryptography;

//encrypt/decrypt strings using Rijndael style encryption
public sealed class CStrEncrypt
{


//encryption keys
private byte[] key;
private byte[] iv;

//class constructor
public CStrEncrypt()
{
//hard code some random 32-byte keys (keeps this faster)
key = new byte[] {8,3,9,2,7,3,0,1,3,93,43,34,39,12,56,43,89,24,38,15,54,32,87,94,33,55,23,78,23,23,29,12};
iv = new byte[] {8,1,6,3,3,8,3,5,2,45,23,62,78,23,73,52,39,74,42,93,74,20,22,93,23,87,55,23,54,44,15,81};
}

//encrypt the string
public string Encrypt(string s)
{
//create algorithm object
RijndaelManaged Algorithm = new RijndaelManaged();
Algorithm.BlockSize = 256;
Algorithm.KeySize = 256;

//create memory stream object
MemoryStream memStream = new MemoryStream();

//create Encryptor object
ICryptoTransform EncryptorDecryptor =
Algorithm.CreateEncryptor(key,iv);

//create CryptoStream object
CryptoStream crStream = new CryptoStream(memStream,
EncryptorDecryptor,
CryptoStreamMode.Write);

//create stream writer
StreamWriter strWriter = new StreamWriter(crStream);

//write input string to crypto stream, encrypts data
strWriter.Write(s);

//flush buffer
strWriter.Flush();
crStream.FlushFinalBlock();

//convert encrypted stream to byte array
byte[] pwd_byte = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(pwd_byte,0,(int)pwd_byte.Length);

//convert byte srray to unicode string
string pwd_str = new UnicodeEncoding().GetString(pwd_byte);

return pwd_str;
}

//decrypt the string
public string Decrypt(string s)
{
//create algorithm object
RijndaelManaged Algorithm = new RijndaelManaged();
Algorithm.BlockSize = 256;
Algorithm.KeySize = 256;

//create memory stream object
MemoryStream memStream = new MemoryStream(
new UnicodeEncoding().GetBytes(s));

//create decryptor object
ICryptoTransform EncryptorDecryptor =
Algorithm.CreateDecryptor(key,iv);

//move memory stream to beginning
memStream.Position = 0;

//create crypto stream object
CryptoStream crStream = new CryptoStream(
memStream,EncryptorDecryptor,CryptoStreamMode.Read);

//read the stream
StreamReader strReader = new StreamReader(crStream);

//return decrypted string
return strReader.ReadToEnd();
}


}


Thanks in advance!
John
 
Hi Jon, I've dealt with this very problem before. Don't pack it into a
string in this manner. As you pointed out, the encryption methods return
byte streams that may include null characters (such as 0).

Instead, use a string that represents the values of the bytes.

For example, the methods Convert.ToBase64String() and
Convert.FromBase64String() will do the trick. Then you can pass the base64
encoded string on the query string.

Phil

Jon Hyland said:
Hi all,

I need to take a string value encrypt it in such a way that I can view
the resulting characters, i.e. pass the encrypted value in a query
string.

My problem is the resulting encrypted string contains characters that
are not viewable (extended / localized) and I can't pass them as
parameters in a URL. What I need are alphabetic and numeric
characters only!

I'm betting there is a simple solution to this, but haven't found it
yet.

FYI .. here's my encoding class:


using System.IO;
using System.Text;
using System.Security.Cryptography;

//encrypt/decrypt strings using Rijndael style encryption
public sealed class CStrEncrypt
{


//encryption keys
private byte[] key;
private byte[] iv;

//class constructor
public CStrEncrypt()
{
//hard code some random 32-byte keys (keeps this faster)
key = new byte[] {8,3,9,2,7,3,0,1,3,93,43,34,39,12,56,43,89,24,38,15,54,32,87,94,33,55,23,78,23,23,29,12};
iv = new byte[] {8,1,6,3,3,8,3,5,2,45,23,62,78,23,73,52,39,74,42,93,74,20,22,93,23,87,55,23,54,44,15,81};
}

//encrypt the string
public string Encrypt(string s)
{
//create algorithm object
RijndaelManaged Algorithm = new RijndaelManaged();
Algorithm.BlockSize = 256;
Algorithm.KeySize = 256;

//create memory stream object
MemoryStream memStream = new MemoryStream();

//create Encryptor object
ICryptoTransform EncryptorDecryptor =
Algorithm.CreateEncryptor(key,iv);

//create CryptoStream object
CryptoStream crStream = new CryptoStream(memStream,
EncryptorDecryptor,
CryptoStreamMode.Write);

//create stream writer
StreamWriter strWriter = new StreamWriter(crStream);

//write input string to crypto stream, encrypts data
strWriter.Write(s);

//flush buffer
strWriter.Flush();
crStream.FlushFinalBlock();

//convert encrypted stream to byte array
byte[] pwd_byte = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(pwd_byte,0,(int)pwd_byte.Length);

//convert byte srray to unicode string
string pwd_str = new UnicodeEncoding().GetString(pwd_byte);

return pwd_str;
}

//decrypt the string
public string Decrypt(string s)
{
//create algorithm object
RijndaelManaged Algorithm = new RijndaelManaged();
Algorithm.BlockSize = 256;
Algorithm.KeySize = 256;

//create memory stream object
MemoryStream memStream = new MemoryStream(
new UnicodeEncoding().GetBytes(s));

//create decryptor object
ICryptoTransform EncryptorDecryptor =
Algorithm.CreateDecryptor(key,iv);

//move memory stream to beginning
memStream.Position = 0;

//create crypto stream object
CryptoStream crStream = new CryptoStream(
memStream,EncryptorDecryptor,CryptoStreamMode.Read);

//read the stream
StreamReader strReader = new StreamReader(crStream);

//return decrypted string
return strReader.ReadToEnd();
}


}


Thanks in advance!
John
 
Thanks Phil.. It worked like a charm!

I did have to do one more thing. Basically some base-64 characters
got screwed up in the URL translation (primarily the "+" character got
turned into a space). I had to do a Server.URLEncode on the encrypted
string prior to stuffing it into the query string. After that,
everything was great.

John
 
Back
Top