473,395 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

My Cryptography algorithm does not work


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.

Jul 28 '06 #1
2 1346
"osmarjunior" <os*********@gmail.comwrote in
news:11**********************@h48g2000cwc.googlegr oups.com:
>
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:
<snip>
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jul 28 '06 #2
Lasse Vågsæther Karlsen wrote:
"osmarjunior" <os*********@gmail.comwrote in
news:11**********************@h48g2000cwc.googlegr oups.com:
>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:
<snip>
> 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.
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.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Apple | last post by:
Hi guys, I am working on my java cryptography assignment. I am looking for the popular or the best cryptography softwares either wrote in java or not to encrypt/decrypt files using DES/RSA...
0
by: Andrzej | last post by:
Hi, I have to figure out why we have a problem with special characters in encrypted usernames and passwords. Case: Username: r&bgeorge Password: tigger
10
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets...
1
by: kiran | last post by:
Hi , I need some helpon Symmetric Crptography algorithm. My friend has given a encrypted message ( encrypted using Symmetric cryptogrraphy algorithm and he does 't mention on which plat form is...
3
by: Nerd | last post by:
If I write a method using dotnet's cryptography libraries and use it for encryption in my application. How can other applications decrypt these messages. The other applications will be developed in...
1
by: Nerd | last post by:
I am trying cryptography in dotnet using the System.Security.Cryptography namespaces. I have a couple of questions 1. If I use SymmetricalAlgorithm RijndaelManaged as the algorithm and if I...
4
by: Jonathan Wood | last post by:
Does anyone know why the documentation for System.Security.Cryptography.MD5.Create() seems to omit completely any description of allowed arguments. I'm trying to convert some C++ code to C# and...
3
by: floppyzedolfin | last post by:
Hi there. I'm coding an encryption / decryption program. At this very moment, I think I should be pretty close from the end, but there's something blocking me on my way. There's a "Padding is...
0
by: johnlim20088 | last post by:
Hi, Currently I m doing a encryption with Microsoft Enterprise Enterlibrary Configuration. I does generated the encrypt and decrypt single string. I follow all the step, in creating...
0
by: Amelyan | last post by:
Why does this happen? How to fix it? Once in a while I get error in ~/ScriptResource.axd?d=... System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.