Anybody has any idea why this simple thing is not working? I pass a text
file as input to encrypt it, then pass the encrypted version to the same
function and get some garbled data not at all resembling the input file.
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES
rijndaelAlg.Padding = PaddingMode.PKCS7;
rijndaelAlg.Mode = CipherMode.CBC;
MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();
PasswordDeriveBytes pdb = new PasswordDeriveBytes(sKey,
m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetByt es(sKey)));
//Set secret key For AES algorithm.
//rijndaelAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
rijndaelAlg.Key = pdb.GetBytes(256/8);
//Set initialization vector.
//rijndaelAlg.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
rijndaelAlg.IV = pdb.GetBytes(16);
FileStream fsIn = new FileStream(sInputFilename, FileMode.Open,
FileAccess.Read);
FileStream fsOut = new FileStream(sOutputFilename, FileMode.Create,
FileAccess.Write);
//Create an AES encryptor from the AES instance.
ICryptoTransform aesencrypt = rijndaelAlg.CreateEncryptor();
//Create crypto stream set to read and do an AES encryption transform on
incoming bytes.
CryptoStream cipherstream = new CryptoStream(fsOut, aesencrypt,
CryptoStreamMode.Write);
int data;
while ((data = fsIn.ReadByte()) != -1)
{
cipherstream.WriteByte((byte) data);
}
byte[] bytearrayinput = new byte[fsIn.Length];
fsIn.Read(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);
cipherstream.Close();
fsOut.Close();
fsIn.Close(); 5 4157
~~~ .NET Ed ~~~ <ti*********@abolishspam.now> wrote: Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all resembling the input file.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Most of the code is there, but it's a lot easier to diagnose this kind
of thing if it's already in a complete program.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
~~~ .NET Ed ~~~ wrote: Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all resembling the input file.
<snippedy-doo-dah />
Hi .NET Ed,
This is because all you're doing is encrypting the encrypted file. You need
to decrypt the file, in order to get the original version. Rijndael is not
a cyclic encryption routine, like XOR'ing every byte by an arbitrary
number.
What you need to do is use ICryptoTransform and create a decryptor, with
everything else the same:
///
ICryptoTransform aesdecrypt = rijndaelAlg.CreateDecryptor();
///
And use that to decrypt the stream.
--
Hope this helps,
Tom Spink
Jon,
It was not a complete program but just a method. The only missing things
where:
public class Test {
public void Encrypt(string sInFilename, string sOutFilename, string
sKey, string sIV)
{
the rest
}
[STAThread]
public void main(string[] args)
{ // for the purpose of testing
Test t = new Test();
t.Encrypt(args[0], args[2], args[3], args[4]);
}
}
TOM,
Thanks that was indeed the right answer. Since it was a symmetric
algorithm I was under the wrong assumption that encrypt/decrypt actually did
the same. Now I do get the original file. In short, adapt the code to use
the Decryptor method of Rijndael when decrypting the file. Had been a while
since I did crypto stuff.
Thanks!
Emilio
"Tom Spink" <ts****@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl... ~~~ .NET Ed ~~~ wrote:
Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all resembling the input file.
<snippedy-doo-dah />
Hi .NET Ed,
This is because all you're doing is encrypting the encrypted file. You need to decrypt the file, in order to get the original version. Rijndael is not a cyclic encryption routine, like XOR'ing every byte by an arbitrary number.
What you need to do is use ICryptoTransform and create a decryptor, with everything else the same:
/// ICryptoTransform aesdecrypt = rijndaelAlg.CreateDecryptor(); ///
And use that to decrypt the stream.
-- Hope this helps, Tom Spink
~~~ .NET Ed ~~~ <ti*********@abolishspam.now> wrote: It was not a complete program but just a method. The only missing things where:
<snip>
No, that's not true. Without sample data, there would be nothing to
test. It makes life a lot easier if you can post *everything* required
to demonstrate the problem. In this case, probably the code to encrypt
as well as the code to decrypt would have been a good idea. It's also a
lot easier to cut and paste a complete program than assembly it from
bits and bobs, work out the imports etc.
Thanks that was indeed the right answer. Since it was a symmetric algorithm I was under the wrong assumption that encrypt/decrypt actually did the same. Now I do get the original file. In short, adapt the code to use the Decryptor method of Rijndael when decrypting the file. Had been a while since I did crypto stuff.
I'm glad it's sorted out now.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
~~~ .NET Ed ~~~ wrote: Since it was a symmetric algorithm I was under the wrong assumption that encrypt/decrypt actually did the same.
The 'symmetric' here means only that the *keys* for encryption and
decryption are the same (or 'trivially related'). It doesn't mean the
actual processes of encryption and decryption are the same.
'Asymmetric' cryptography is where the keys themselves for encryption
and decryption differ. Wikipedia for more.
--
Larry Lard
Replies to group please This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bryan Olson |
last post by:
The current Python standard library provides two cryptographic
hash functions: MD5 and SHA-1 . The authors of MD5 originally
stated:
It is conjectured that it is computationally infeasible to...
|
by: Lauren Wilson |
last post by:
Hi Folks,
We've been using Crypto ++32 to control licensed access to our widely
distributed Access 2K app. Unfortunately, Sampson Multimedia appears
to be out of business.
Does anyone out...
|
by: William Stacey [MVP] |
last post by:
The Decypt2() method below does not work. It completes, but does not do the
right thing. The first transform request returns 0 bytes. The first
Decypt() method works as we work on a stream...
|
by: Mantorok |
last post by:
Hi
I have a couple of encryption methods but when I call decrypt I get the
string back but with a load \0 escape characters on the end?
Any idea why? It is actually causing problems in some...
|
by: Sean Kelly |
last post by:
The old one, not the .NET version. I don't see it listed, but since
it's available under .NET I thought I'd ask. Also, there are some API
calls which are supposedly not available under most...
|
by: osmarjunior |
last post by:
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...
|
by: Jens Müller |
last post by:
Hello,
I try to program a Rijndael encryption in Windows which has to be
compatible with php.
In php I use the code below to encrypt with a 256 Bit Key and a 256 Bit
block cipher.
My windows...
|
by: vermarajeev |
last post by:
Hi guys,
I have written code to encrypt and decrypt files using perl script. Please help me to port below code to crypto++ library.
//ENCRYPTION
my $cipher = Crypt::CBC->new(
-cipher =>...
|
by: KBS Developer |
last post by:
Hi,
I can encrypt without any problem but while decrypting I got junk.
I've read the other thread about getting junk but that is not my case.
Here is the sample code:
private Rijndael...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |