Click here to Skip to main content
15,899,679 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: connection string Pin
saberbladez21-Jan-09 21:39
saberbladez21-Jan-09 21:39 
QuestionAccess Control from another form Pin
Ola E21-Jan-09 19:46
Ola E21-Jan-09 19:46 
AnswerRe: Access Control from another form Pin
Ashutosh Phoujdar22-Jan-09 0:35
Ashutosh Phoujdar22-Jan-09 0:35 
AnswerRe: Access Control from another form Pin
Ola E22-Jan-09 1:17
Ola E22-Jan-09 1:17 
GeneralRe: Access Control from another form Pin
Ben Fair22-Jan-09 6:21
Ben Fair22-Jan-09 6:21 
AnswerRe: Access Control from another form Pin
Ola E22-Jan-09 19:06
Ola E22-Jan-09 19:06 
Questionbinary file encryption Pin
Member 455503121-Jan-09 4:22
Member 455503121-Jan-09 4:22 
AnswerRe: binary file encryption Pin
Ben Fair22-Jan-09 5:59
Ben Fair22-Jan-09 5:59 
The problem may be these two lines in the Decipher function:

byte[] PlainTextBytes = new byte[CipherText.Length];
int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);


The Read() will fill PlainTextBytes up through the number of bytes read, the rest will be 0's. I find that very often the encrypted data is longer than the decrypted data. So it's very possible that PlainTextBytes will be created as a byte[] larger than the actual decrypted data. I've also come across code like this on the internet and won't use it myself; it just doesn't look right to me. For what arbitrary reason would you determine that the length of the decrypted data is exactly the same as the length of the encrypted data? Which is essentially what the code is assuming. I prefer to play it safe and not make that assumption.

I find it easier to use the CryptoStream in Write mode for both encrypting and decrypting, I find it helps avoid the aforementioned assumption. If you supply the CryptoStream an empty MemoryStream and the proper ICryptoTransform (encrypt or decrypt), then all you have to do is write the byte[] to the CryptoStream and when you Read from the MemoryStream you will get the transformed data. Doing that in the Decipher function would look like this:

MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream, Decryptor, CryptoStreamMode.Write);
cryptoStream.Write(CipherText, 0, CipherText.Length);
PlainTextBytes = memStream.ToArray();


This is pretty much how you did the encryption in the Cipher function. The cool thing about this is that the actions for encrypting and decrypted are exactly the same, the only thing different is the data and the ICryptoTransform used. So, you can put this functionality in a single function in which you only need to pass the proper ICryptoTransform and the proper byte[]. When you are encrypting pass in the unencrypted data as a byte[] and the Encryptor ICryptoTransform and when decrypting pass in the encrypted data as a byte[] and the Decryptor ICryptoTransform, and you'd have this (which is almost the same as code I've used):

private byte[] PerformTransform(byte[] data, ICryptoTransform tran)
{
    // create a memory stream to hold data and a cryptostream to do the transformation
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, tran, CryptoStreamMode.Write);
    // input the data to the crypto stream so it can transform it
    cs.Write(data, 0, data.Length);
    // close the crypto stream and let it flush and finalize
    cs.Close();
    // return the transformed data from the memory stream
    return ms.ToArray();
}


One last note, save yourself a lot of unnecessary text and put in the using statements for the System.IO and System.Security.Cryptography namespaces. Wink | ;)

Keep It Simple Stupid! (KISS)

QuestionTimed GTalk Status Updater Pin
itsravie21-Jan-09 0:25
itsravie21-Jan-09 0:25 
AnswerRe: Timed GTalk Status Updater Pin
Dave Kreskowiak21-Jan-09 2:02
mveDave Kreskowiak21-Jan-09 2:02 
AnswerRe: Timed GTalk Status Updater Pin
Eddy Vluggen21-Jan-09 2:34
professionalEddy Vluggen21-Jan-09 2:34 
GeneralRe: Timed GTalk Status Updater Pin
Mohammad Dayyan21-Jan-09 8:52
Mohammad Dayyan21-Jan-09 8:52 
AnswerRe: Timed GTalk Status Updater Pin
itsravie25-Jan-09 12:07
itsravie25-Jan-09 12:07 
QuestionDeserialisation Problem with Generic Methods and Delegates Pin
mi220-Jan-09 22:43
mi220-Jan-09 22:43 
QuestionBCP process does not throw proper exceptions incase of any failures Pin
indian14320-Jan-09 19:52
indian14320-Jan-09 19:52 
AnswerSorry the message became too big as I am unable to edit it, again I am writing the question here [modified] Pin
indian14320-Jan-09 19:57
indian14320-Jan-09 19:57 
GeneralRe: Sorry the message became too big as I am unable to edit it, again I am writing the question here Pin
Dave Kreskowiak21-Jan-09 2:00
mveDave Kreskowiak21-Jan-09 2:00 
AnswerRe: BCP process does not throw proper exceptions incase of any failures Pin
Wendelius21-Jan-09 5:19
mentorWendelius21-Jan-09 5:19 
Question.Net Pin
Smtnri20-Jan-09 8:50
Smtnri20-Jan-09 8:50 
AnswerRe: .Net Pin
Not Active20-Jan-09 9:01
mentorNot Active20-Jan-09 9:01 
AnswerWow. Pin
leckey20-Jan-09 9:47
leckey20-Jan-09 9:47 
AnswerRe: .Net Pin
Pete O'Hanlon20-Jan-09 10:38
mvePete O'Hanlon20-Jan-09 10:38 
JokeRe: .Net Pin
0x3c020-Jan-09 10:52
0x3c020-Jan-09 10:52 
GeneralRe: .Net Pin
Mohammad Dayyan20-Jan-09 20:05
Mohammad Dayyan20-Jan-09 20:05 
AnswerRe: .Net Pin
Mohammad Dayyan20-Jan-09 20:06
Mohammad Dayyan20-Jan-09 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.