Click here to Skip to main content
15,916,842 members
Home / Discussions / C#
   

C#

 
Question[Help]How to get width & height of the text? Pin
xfqiu21-Aug-03 21:15
xfqiu21-Aug-03 21:15 
AnswerRe: [Help]How to get width & height of the text? Pin
Nnamdi Onyeyiri21-Aug-03 23:31
Nnamdi Onyeyiri21-Aug-03 23:31 
Questionhow to download UtilityLibrary? Pin
dongdongrojiny21-Aug-03 20:56
dongdongrojiny21-Aug-03 20:56 
AnswerRe: how to download UtilityLibrary? Pin
J. Dunlap21-Aug-03 21:11
J. Dunlap21-Aug-03 21:11 
GeneralRe: how to download UtilityLibrary? Pin
leppie22-Aug-03 13:58
leppie22-Aug-03 13:58 
GeneralEncrypt/Decrypt byte array into SQLServer image field Pin
chlock21-Aug-03 15:23
chlock21-Aug-03 15:23 
GeneralRe: Encrypt/Decrypt byte array into SQLServer image field Pin
Daniel Turini22-Aug-03 4:26
Daniel Turini22-Aug-03 4:26 
GeneralRe: Encrypt/Decrypt byte array into SQLServer image field Pin
chlock22-Aug-03 5:37
chlock22-Aug-03 5:37 
OK-

I haven't taken the time to comb through the code and find what I was doing wrong. With all the arrays flying around, I suspect I just used the wrong variable somewhere.

The following code works. So it's there for anybody who wants a simple method for encrypting/decrypting byte arrays. It's hard to find examples that aren't written for the writing-an-encrypted-file-to-disk crowd.

Bear in mind that hard coding the key and IV theoretically leaves you open to some hacker reverse engineering your code and being able to easily decrypt your files. But my personal feeling on this matter is that it's infinitely more likely that a user will compromise security by leaving their password on a sticky note stuck to their monitor. And I'm not securing highly sensitive material anyway. If you have a need for greater security, use some other method for generating and keeping track of your keys and IV's.





<br />
public byte[] EncryptStream(byte[] input)<br />
		{<br />
			RijndaelManaged rijn = new RijndaelManaged();			<br />
			byte[] encrypted;			<br />
		<br />
			byte[] key = new byte[]{0x22,0xc0,0x6d,0xcb,0x23,0xa6,0x3,0x1b,0x5a,0x1d,0xd3,<br />
                               0x9f,0x85,0xd,0xc1,0x72,0xed,0xf4,0x54,0xe6,0xba,0x65,<br />
                               0xc,0x22,0x62,0xbe,0xf3,0xec,0x14,0x81,0xa8,0xa};//32<br />
			byte[] IV = new byte[]{0x43,0xb1,0x93,0xb,0x1a,0x87,0x52,0x62,0xfb,0x8,0xd,0xc0,<br />
                              0xca,0x40,0xc2,0xdb};//16<br />
<br />
			//Get an encryptor.<br />
			ICryptoTransform encryptor = rijn.CreateEncryptor(key, IV);<br />
            <br />
			//Encrypt the data.<br />
			MemoryStream msEncrypt = new MemoryStream();<br />
			CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);<br />
			<br />
<br />
			//Write all data to the crypto stream and flush it.<br />
			csEncrypt.Write(input, 0, input.Length);<br />
			csEncrypt.FlushFinalBlock();<br />
			csEncrypt.Close();<br />
<br />
			//Get encrypted array of bytes.<br />
			encrypted = msEncrypt.ToArray();			<br />
<br />
			return encrypted;<br />
			<br />
		}<br />
		<br />
		public byte[] DecryptStream(byte[] encrypted)<br />
		{<br />
			RijndaelManaged myRijndael = new RijndaelManaged();			<br />
			byte[] fromEncrypt;			<br />
			byte[] key = new byte[]{0x22,0xc0,0x6d,0xcb,0x23,0xa6,0x3,0x1b,0x5a,0x1d,0xd3,<br />
															 0x9f,0x85,0xd,0xc1,0x72,0xed,0xf4,0x54,0xe6,0xba,0x65,<br />
															 0xc,0x22,0x62,0xbe,0xf3,0xec,0x14,0x81,0xa8,0xa};//32<br />
			byte[] IV = new byte[]{0x43,0xb1,0x93,0xb,0x1a,0x87,0x52,0x62,0xfb,0x8,0xd,0xc0,<br />
															0xca,0x40,0xc2,0xdb};//16	<br />
				<br />
			<br />
<br />
			//Get a decryptor that uses the same key and IV as the encryptor.<br />
			ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);<br />
<br />
			//Now decrypt the previously encrypted message using the decryptor<br />
			// obtained in the above step.<br />
			MemoryStream msDecrypt = new MemoryStream(encrypted);<br />
			CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);<br />
<br />
			fromEncrypt = new byte[encrypted.Length];<br />
<br />
			//Read the data out of the crypto stream.<br />
			csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);<br />
<br />
			//Convert the byte array back into a string.<br />
			//roundtrip = textConverter.GetString(fromEncrypt);<br />
			return fromEncrypt;<br />
		}<br />


enjoy-
chlock
GeneralTool bar buttons remain pressed Pin
sumeat21-Aug-03 14:56
sumeat21-Aug-03 14:56 
GeneralRe: Tool bar buttons remain pressed Pin
joan_fl22-Aug-03 7:55
joan_fl22-Aug-03 7:55 
GeneralRe: Tool bar buttons remain pressed Pin
sumeat22-Aug-03 8:27
sumeat22-Aug-03 8:27 
QuestionGet installer to create empty folders? Pin
vlusardi21-Aug-03 12:44
vlusardi21-Aug-03 12:44 
AnswerRe: Get installer to create empty folders? Pin
Martey21-Aug-03 18:34
Martey21-Aug-03 18:34 
GeneralRe: Get installer to create empty folders? Pin
vlusardi22-Aug-03 5:23
vlusardi22-Aug-03 5:23 
Generalfirst step in C# Pin
aguest21-Aug-03 12:05
aguest21-Aug-03 12:05 
GeneralRe: first step in C# Pin
MeisterBiber21-Aug-03 22:05
MeisterBiber21-Aug-03 22:05 
GeneralRe: first step in C# Pin
MeisterBiber22-Aug-03 12:05
MeisterBiber22-Aug-03 12:05 
GeneralProblem with FileLoadException Pin
adaoja21-Aug-03 9:47
adaoja21-Aug-03 9:47 
Questionwraping text in a cell of a datagrid? Pin
mikemilano21-Aug-03 9:38
mikemilano21-Aug-03 9:38 
AnswerRe: wraping text in a cell of a datagrid? Pin
Ista21-Aug-03 14:43
Ista21-Aug-03 14:43 
Questiondeploy 2 components w/ one installer? Pin
vlusardi21-Aug-03 6:25
vlusardi21-Aug-03 6:25 
GeneralVB.NET to C# converter Pin
Iftikhar Ahmad Dar21-Aug-03 5:31
Iftikhar Ahmad Dar21-Aug-03 5:31 
GeneralRe: VB.NET to C# converter Pin
Okeno Palmer21-Aug-03 7:00
Okeno Palmer21-Aug-03 7:00 
GeneralCheck Box in Datagrid with Autopostback Pin
0siris21-Aug-03 5:23
0siris21-Aug-03 5:23 
GeneralRe: Check Box in Datagrid with Autopostback Pin
Ista21-Aug-03 14:47
Ista21-Aug-03 14:47 

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.