Click here to Skip to main content
15,896,444 members
Home / Discussions / C#
   

C#

 
AnswerRe: how we implement Slide-In Menu Bar in windows application using c#? Pin
Noble KC14-Nov-14 17:32
professionalNoble KC14-Nov-14 17:32 
Questionthere is no server Pin
reza assar26-Sep-08 13:22
reza assar26-Sep-08 13:22 
Jokethere is no spoon Pin
Nicholas Butler27-Sep-08 0:00
sitebuilderNicholas Butler27-Sep-08 0:00 
Questionimplementing wildcard match method Pin
AndrusM26-Sep-08 12:15
AndrusM26-Sep-08 12:15 
AnswerRe: implementing wildcard match method Pin
Anthony Mushrow26-Sep-08 12:29
professionalAnthony Mushrow26-Sep-08 12:29 
AnswerRe: implementing wildcard match method Pin
Paul Conrad27-Sep-08 5:05
professionalPaul Conrad27-Sep-08 5:05 
GeneralRe: implementing wildcard match method Pin
AndrusM28-Sep-08 22:44
AndrusM28-Sep-08 22:44 
QuestionSystem.Security.Cryptography for encrypting binary files (not plain text) Pin
Cyrilix26-Sep-08 9:01
Cyrilix26-Sep-08 9:01 
Is there a way to use the .NET cryptography library to encrypt binary files as opposed to plain text files? I've been having a few issues with the way that encrypted bytes are decrypted back to their original state.

What I do to initialize my algorithm for decryption is approximately the following:
UnicodeEncoding byteConverter = new UnicodeEncoding();
RijndaelManaged algorithm = new RijndaelManaged();
algorithm.IV = byteConverter->GetBytes(/*a unicode string converted to bytes for init vector*/);
algorithm.Key = byteConverter->GetBytes(/*a unicode string converted to bytes for password*/);
return alg; //returned as base class "SymmetricAlgorithm"

In order to encrypt, first, I call that stub of algorithm initialization code. Then I do approximately the following with a Byte[] data that I want to encrypt:
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
for (int i = 0; i < numBytes; i++)
{
  cs.WriteByte(data[i]);
}
cs.FlushFinalBlock();
byte encoded[] = ms.ToArray();
cs.Close();
ms.Close();
string output = Convert.ToBase64String(encoded);

In order to decrypt, first, I call that stub of algorithm initialization code. Then I do approximately the following with a string input that I want to decrypt:
byte[] encoded = Convert.FromBase64String(input);
MemoryStream ms = new MemoryStream(encoded);
CryptoStream cs = new CryptoStream(ms, algorithm.CreateDecryptor(), CryptoStreamMode.Read);
byte decoded[] = new byte[encoded.Length];
for (int i = 0; i < decoded.Length; i++)
{
  int readByte = cs.ReadByte();
  if (readByte != -1)
  {
    decoded[i] = (byte)readByte;
  }
}
cs.Close();
ms.Close();

The problem is that if I call Encrypt, then Decrypt, encryption leaves me with a nice base64 string, which decrypt easily converts back to a byte array. But when I read the CryptoStream and get check all the values of int readByte everytime I read a byte, as soon as I come to the null-terminated character (0 ''), everything else that I read after that is the same garbage symbol (-51 'Í'). More precisely, the code that I'm writing here is the C# equivalent of my Managed C++ code (however, the problem does not lie in the unmanaged part or the unmanaged/managed conversion). So, to rephrase what I mean, say that I'm checking all the values of readByte for i = 0 to the length of the decoded byte array (which hasn't yet been filled), if ReadByte happens to return me 66, 77, 54, 44, 23, 0, respectively, everything else after that is -51. If I'm decrypting binary data, I will run into this issue. If I'm decrypting text data, then I won't, since in a text string, 0 is the null-terminated character that only occurs at the very end of the string (and I've got all my bytes at that point anyway).

Any suggestions? Thanks, and I apologize for the long post.
AnswerRe: System.Security.Cryptography for encrypting binary files (not plain text) Pin
Ennis Ray Lynch, Jr.26-Sep-08 10:06
Ennis Ray Lynch, Jr.26-Sep-08 10:06 
GeneralRe: System.Security.Cryptography for encrypting binary files (not plain text) Pin
Cyrilix26-Sep-08 10:42
Cyrilix26-Sep-08 10:42 
GeneralRe: System.Security.Cryptography for encrypting binary files (not plain text) Pin
Cyrilix26-Sep-08 12:44
Cyrilix26-Sep-08 12:44 
GeneralRe: System.Security.Cryptography for encrypting binary files (not plain text) Pin
Guffa26-Sep-08 15:46
Guffa26-Sep-08 15:46 
GeneralRe: System.Security.Cryptography for encrypting binary files (not plain text) Pin
ajtunbridge26-Sep-08 15:52
ajtunbridge26-Sep-08 15:52 
QuestionCodeDom and partial method Pin
Member 346950026-Sep-08 7:56
Member 346950026-Sep-08 7:56 
AnswerRe: CodeDom and partial method Pin
Ennis Ray Lynch, Jr.26-Sep-08 8:01
Ennis Ray Lynch, Jr.26-Sep-08 8:01 
AnswerRe: CodeDom and partial method Pin
Pete O'Hanlon27-Sep-08 9:26
mvePete O'Hanlon27-Sep-08 9:26 
AnswerRe: CodeDom and partial method Pin
jatin.sabarmati23-Nov-08 16:47
jatin.sabarmati23-Nov-08 16:47 
QuestionC# vs VB6.0 Pin
Pedram Behroozi26-Sep-08 6:46
Pedram Behroozi26-Sep-08 6:46 
AnswerRe: C# vs VB6.0 Pin
Fernando A. Gomez F.26-Sep-08 7:03
Fernando A. Gomez F.26-Sep-08 7:03 
AnswerRe: C# vs VB6.0 [modified] Pin
Judah Gabriel Himango26-Sep-08 7:08
sponsorJudah Gabriel Himango26-Sep-08 7:08 
GeneralRe: C# vs VB6.0 Pin
Pedram Behroozi26-Sep-08 7:23
Pedram Behroozi26-Sep-08 7:23 
GeneralRe: C# vs VB6.0 [modified] Pin
Judah Gabriel Himango26-Sep-08 7:40
sponsorJudah Gabriel Himango26-Sep-08 7:40 
GeneralRe: C# vs VB6.0 Pin
Paul Conrad26-Sep-08 8:37
professionalPaul Conrad26-Sep-08 8:37 
AnswerRe: C# vs VB6.0 Pin
Wendelius26-Sep-08 7:39
mentorWendelius26-Sep-08 7:39 
GeneralRe: C# vs VB6.0 Pin
Pedram Behroozi26-Sep-08 8:09
Pedram Behroozi26-Sep-08 8:09 

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.