My Cryptography algorithm does not work

O

osmarjunior

I have two methods Encode(String) and Decode(String). But the Decode()
returns a different string. If I encode "123456", for example, and try
to decode the result, it returns a different value. Can anyone help to
find the error? Here's the code:

public static String Encode(String value)
{
// Create a new Rijndael object to generate a key and
initialization vector (IV).
Rijndael RijndaelAlg = Rijndael.Create();

try
{
Byte[] data = Encoding.ASCII.GetBytes(value);

MemoryStream memStream = new MemoryStream();

CryptoStream encStream = new CryptoStream(memStream,
RijndaelAlg.CreateEncryptor(RijndaelAlg.Key,
RijndaelAlg.IV),
CryptoStreamMode.Write);

encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();

//calculate the length of the encrypted data
Byte[] result = new Byte[memStream.Position];
memStream.Position = 0;
memStream.Read(result, 0, result.Length);

encStream.Close();

return Encoding.ASCII.GetString(result);
}
catch (Exception e)
{
Messages.Error("Ocorreu um erro na Criptografia: " +
e.Message);

return value;
}
}


public static String Decode(String value)
{
Byte[] data = Encoding.ASCII.GetBytes(value);

MemoryStream memStream = new MemoryStream(data.Length);

// Create a new Rijndael object to generate a key and
initialization vector (IV).
Rijndael RijndaelAlg = Rijndael.Create();

CryptoStream encStream = new CryptoStream(memStream,
RijndaelAlg.CreateEncryptor(RijndaelAlg.Key,
RijndaelAlg.IV),
CryptoStreamMode.Read);

memStream.Write(data, 0, data.Length);
memStream.Position = 0;

String strResult = new StreamReader(encStream).ReadToEnd();

encStream.Close();

Byte[] result = Encoding.ASCII.GetBytes(strResult);

return Encoding.ASCII.GetString(result);
}


Regards.

Junior.
 
L

Lasse Vågsæther Karlsen

I have two methods Encode(String) and Decode(String). But the Decode()
returns a different string. If I encode "123456", for example, and try
to decode the result, it returns a different value. Can anyone help to
find the error? Here's the code:
return Encoding.ASCII.GetString(result);

Don't re-encode it to a string using ASCII. ASCII is 7-bit, so you'll lose
every 8th bit of your encrypted data this way.

Your best option is to return it as a byte array and pass that around. In
other words, return result.
 
L

Larry Lard

Lasse said:
Don't re-encode it to a string using ASCII. ASCII is 7-bit, so you'll lose
every 8th bit of your encrypted data this way.

Your best option is to return it as a byte array and pass that around. In
other words, return result.

And if you absolutely must have your encrypted data represented as a
string (the ideal is to treat it as a collection of bytes, which is what
it is, but you might have to, I dunno, display it to the user), use
Convert.ToBase64String to obtain a string representation that's safe to
do just about anything with.
 

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

Top