Help e to get simple encryption and decryption to work
From: kaushas (kaushas_at_discussions.microsoft.com)
Date: 09/17/05
- Previous message: Valery Pryamikov: "Re: Importance of salt"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Sat, 17 Sep 2005 11:36:02 -0700
I am hoping that this is the right forum to post this issue....If not my
apologies
What I am trying to do is very simple, but I am getting bad data error or I
am unable to decrypt the original string at all. I have a XML string that
needs to be encrypted and then decrypted. The password and keys are hard
coded for now so that I can validate the functionality before I refractor
code to be cleaner and efficient.
Attached is the code for main class, the only item worth noting is the size
of the encrypted string increases from 34 to 40 (ms.ToArray() line).
I suspect that the encryption it self is not working correctly but I have no
clue what could be wrong. Any help will be much appreciated.
TIA
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace EncryptDecrypt
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Crypto
{
public Crypto()
{
//
// TODO: Add constructor logic here
//
}
public StringBuilder Encrypt(StringBuilder sb, string password)
{
byte[] dataToEncode ;
byte[] encryptedData;
byte[] salt;
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
//initialize salt;
ASCIIEncoding textConverter = new ASCIIEncoding();
salt = textConverter.GetBytes("YxA1");
dataToEncode = textConverter.GetBytes(sb.ToString());
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = iv;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,salt);
rc2.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, iv);
Console.WriteLine(rc2.Key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms , rc2.CreateEncryptor(),
CryptoStreamMode.Write);
cs.Write(dataToEncode,0,dataToEncode.Length);
cs.FlushFinalBlock();
// encStream.Close();
//get encrypted array
encryptedData = ms.ToArray();
int iterator = 0;
for (iterator = 0; iterator < encryptedData.Length;iterator++)
{
if (encryptedData[iterator] == 0) break;
}
StringBuilder cryptoSb = new StringBuilder();
cryptoSb.Append(textConverter.GetString(encryptedData,0,iterator));
ms.Close();
return cryptoSb;
}
public StringBuilder Decrypt(StringBuilder sb, string password)
{
byte[] dataToDecode ;
byte[] decryptedData;
byte[] salt;
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
ASCIIEncoding textConverter = new ASCIIEncoding();
salt = textConverter.GetBytes("YxA1");
dataToDecode = textConverter.GetBytes(sb.ToString());
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
rc2.IV = iv;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,salt);
rc2.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, iv);
MemoryStream ms = new MemoryStream(dataToDecode);
try
{
CryptoStream cs = new CryptoStream(ms , rc2.CreateDecryptor (),
CryptoStreamMode.Read);
decryptedData = new byte[dataToDecode.Length];
cs.Read(decryptedData,0,decryptedData.Length);
cs.FlushFinalBlock();
}
catch (CryptographicException ex)
{
throw ex;
}
// encStream.Close();
StringBuilder decryptoSb = new StringBuilder();
decryptoSb.Append(textConverter.GetString(decryptedData).TrimEnd(Convert.ToChar(0)));
ms.Close();
return decryptoSb;
}
private static byte[] StringToByteArray(String str)
{
ASCIIEncoding ecoding = new ASCIIEncoding();
return ecoding.GetBytes(str);
}
private static byte[] StringToByteArray(StringBuilder sb)
{
ASCIIEncoding ecoding = new ASCIIEncoding();
return ecoding.GetBytes(sb.ToString());
}
private static StringBuilder ByteArrayToStringBuffer(byte [] bArray)
{
StringBuilder sb = new StringBuilder();
ASCIIEncoding ecoding = new ASCIIEncoding();
//System.Text.Encoding.UTF8.GetBytes
//System.Convert.ToBase64string
sb.Append(ecoding.GetString(bArray));
return sb;
}
}
}
Here is the test class
EncryptDecrypt.Crypto crypto;
StringBuilder cryptoSB;
String password;
StringBuilder sb;
crypto = new EncryptDecrypt.Crypto();
//Password has to be 16 or 24 bytes
password = "password1234567890123456";
sb = new StringBuilder();
sb.Append("<XML><This is a text string></XML>");
cryptoSB = new StringBuilder();
cryptoSB = crypto.Encrypt(sb,password);
Console.WriteLine ("value of cryptoSB" + cryptoSB.ToString());
StringBuilder decryptSB = new StringBuilder();
decryptSB = crypto.Decrypt(cryptoSB, password);
Console.WriteLine (decryptSB.ToString());
- Previous message: Valery Pryamikov: "Re: Importance of salt"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|