Re: cryptography
From: DalePres (don-t-spa-m-me_at_lea-ve-me-a-lone--.com)
Date: 03/08/05
- Next message: DalePres: "Re: TreeView Tag and clone"
- Previous message: sushant.bhatia_at_gmail.com: "X509 Certificate's Signature"
- In reply to: DalePres: "Re: cryptography"
- Messages sorted by: [ date ] [ thread ]
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>
>
>
- Next message: DalePres: "Re: TreeView Tag and clone"
- Previous message: sushant.bhatia_at_gmail.com: "X509 Certificate's Signature"
- In reply to: DalePres: "Re: cryptography"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|