Re: cryptography

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: DalePres (don-t-spa-m-me_at_lea-ve-me-a-lone--.com)
Date: 03/08/05


Date: Mon, 7 Mar 2005 19:16:05 -0600

One more problem that is going to give you fits with your code if you
proceed from what you have as a base. The PasswordDeriveBytes.GetBytes()
method returns a psuedo-random key. Though the values will be the same for
each count in a sequence using the same password and IV, if your code ever
uses one instance at, for example, sequence 3, while the other instance is
at sequence 2, then they will not have the same key.

To get a key, use the CryptDeriveKey method instead. That will always give
you the same key for the same password and IV. You can change the IV for
randomness if you need it in your application and prepend your saved or
passed data with the IV.

DalePres

"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:eFPxQr3IFHA.2736@TK2MSFTNGP09.phx.gbl...
>
>
> Delete lines 4, 6, and 7 within your main method. Those are the lines
> that refer to the StreamReader and your declaration and initialization of
> the byteOfCipherText array. Replace those three lines with the two lines
> below:
>
> BinaryReader br = new BinaryReader(File.Open(fileName,FileMode.Open));
>
> byte[] byteOfCipherText = br.ReadBytes((int)br.BaseStream.Length);
>
> The StreamReader is reading your binary encrypted file as text and
> destroying the data in the process. The BinaryReader will read it like it
> should be read.
>
> Of course, there are a lot of other problems in your code; using a string
> literal in one line, then assigning that same literal to a variable in the
> next line and then using the variable from then on, for instance.
>
> Also, I'd recommend, and so does Microsoft, that you use the
> RijndaelManaged for your code. The Rijndael class is intended to be the
> base class for creating Cryptography services, such as RijndaelManaged.
>
>
>
> For a couple examples of simple encryption using the RijndaelManaged class
> you can read my articles at:
>
> http://www.dalepreston.com/Blog/Archives/2005_02_20_Archive.html and
>
> http://www.dalepreston.com/programming/CryptoRtfBox.aspx
>
>
> HTH
>
> DalePres
> MCAD, MCDBA, MCSE
>
> "shoeb khan via .NET 247" <anonymous@dotnet247.com> wrote in message
> news:emyl4s0IFHA.2420@TK2MSFTNGP14.phx.gbl...
>> hi,how r u guys, i m facing a problem,
>> plz see the following listing for encryption and decryption
>> static void Main(string[] args)
>> {
>> string Password="passString;
>> Encrypt("Server=srvName;uid=uids;pwd=pwds;",@"C:\LnJDServerName.crp",Password);
>> string fileName = @"C:\LnJDServerName.crp";
>> StreamReader sr=new StreamReader(fileName);
>> UnicodeEncoding uniEnc=new UnicodeEncoding();
>> string cipherText=sr.ReadToEnd();
>> byte[] byteOfCipherText=uniEnc.GetBytes(cipherText);
>> PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
>> new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65,
>> 0x64, 0x65, 0x76});
>>
>> Rijndael alg = Rijndael.Create();
>> alg.Key = pdb.GetBytes(32);
>> alg.IV = pdb.GetBytes(16);
>> alg.Padding=PaddingMode.PKCS7 ;
>>
>> byte[] cipherByte=uniEnc.GetBytes(Password);
>>
>>
>> MemoryStream ms=new MemoryStream();
>> CryptoStream cs=new
>> CryptoStream(ms,alg.CreateDecryptor(),CryptoStreamMode.Write);
>>
>>
>> cs.Write(byteOfCipherText,0,byteOfCipherText.Length);
>> ms.Seek(0,SeekOrigin.Begin);
>> byte[] DecryptedByte=ms.ToArray();
>>
>> string plainText=uniEnc.GetString(DecryptedByte);
>> Console.Write(plainText);
>> Console.Read();
>> }
>> internal static void Encrypt(string StringToCrypt, string fileOut, string
>> Password)
>> {
>>
>> FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate,
>> FileAccess.Write);
>> UnicodeEncoding uniEnc=new UnicodeEncoding();
>> PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
>> new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65,
>> 0x64, 0x65, 0x76});
>>
>> Rijndael alg = Rijndael.Create();
>> alg.Key = pdb.GetBytes(32);
>> alg.IV = pdb.GetBytes(16);
>> alg.Padding=PaddingMode.PKCS7 ;
>> // Now create a crypto stream through which we are going to be pumping
>> data.
>> // Our fileOut is going to be receiving the encrypted bytes.
>>
>> CryptoStream cs = new CryptoStream(fsOut, alg.CreateEncryptor(),
>> CryptoStreamMode.Write);
>>
>> // Now will will initialize a buffer and will be processing the input
>> file in chunks.
>>
>> // This is done to avoid reading the whole file (which can be huge) into
>> memory.
>>
>> int bufferLen = StringToCrypt.Length;
>> byte[] buffer = new byte[bufferLen];
>> //int bytesRead;
>>
>> byte[] ByteOfStringToCrypt=uniEnc.GetBytes(StringToCrypt);
>>
>> // encrypt it
>> cs.Write(ByteOfStringToCrypt, 0, ByteOfStringToCrypt.Length );
>>
>> // close everything
>>
>> cs.Close(); // this will also close the unrelying fsOut stream
>>
>> }
>>
>> ----
>> when i run the above code, encryption is smooth,however when decryption
>> part executes, it decrypts to something non readable by human, whats
>> wrong with the code, will u please point out, thanks in advance
>>
>> -----------------------
>> Posted by a user from .NET 247 (http://www.dotnet247.com/)
>>
>> <Id>RxUI697G3UeUc/p6KJFIBA==</Id>
>
>



Relevant Pages

  • Length of the data to decrypt is invalid
    ... I found this code on a site for doing string encryption/decryption. ... // Create a symmetric algorithm. ... // This is done to make encryption more secure. ... // This will tell it that we have done our decryption ...
    (microsoft.public.dotnet.framework.aspnet)
  • PHP mcrypt and VB.NET cryptoservice interop
    ... I am consuming a PHP-based web service over VB.NET but having some problems with encryption. ... but for some reason - the characters in positions 3-6 of the total length of 64 are different compared to original string at PHP side. ... The decryption code follows: ... Dim encryptedStream As MemoryStream = New MemoryStream ...
    (microsoft.public.dotnet.security)
  • RE: Bad Data using TripleDES Encryption
    ... // first we need to translate the string into an array of bytes ... // chain two crypto stream together - encrypting and base64 encoding ... // set the decryption key and IV ... Bad Data using TripleDES Encryption ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: lightweight encryption of text file
    ... Because Arcfour uses xor for the encryption step, decryption is exactly ... Arcfour is a stream cipher. ... string made up of concatenating the two strings together. ...
    (comp.lang.python)
  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)