Click here to Skip to main content
15,891,372 members
Home / Discussions / C#
   

C#

 
GeneralI FOUND IT ! Pin
HG24-Aug-03 23:04
HG24-Aug-03 23:04 
Generalperformance issue updating datatable Pin
troels_sorensen21-Aug-03 21:51
troels_sorensen21-Aug-03 21:51 
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 
Help?

I modified some code from an MSDN sample to try to use the RijndaelManaged object to encrypt a byte array(an uploaded file from a browser) and stuff it into an image field in SQLServer. Seems like an easy task, right?

The files are definitley scrambled. Just can't seem to un-scramble 'em. I created an Encrypt and a Decrypt function, both of which accept a byte array and return a byte array. The key and IV are hard coded at this point.

I've been through the step of encrypting and decrypting at the same time, to see if the un-encrypted file makes it into the database....no love. It's hammered. D'Oh! | :doh:
Somethin' ain't right. Here's the code for the functions:

<br />
public byte[] EncryptStream(byte[] input)<br />
		{<br />
			RijndaelManaged rijn = new RijndaelManaged();			<br />
			byte[] encrypted;			<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 />
<br />
			//Get encrypted array of bytes.<br />
			encrypted = msEncrypt.ToArray();			<br />
<br />
			return encrypted;<br />
			<br />
		}<br />


<br />
public byte[] DecryptStream(byte[] input)<br />
		{<br />
			RijndaelManaged rijn = new RijndaelManaged();			<br />
			byte[] decrypted;			<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 = rijn.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(input);<br />
			CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);<br />
<br />
			decrypted = new byte[input.Length];<br />
<br />
			//Read the data out of the crypto stream.<br />
			csDecrypt.Read(decrypted, 0, decrypted.Length);<br />
<br />
			return decrypted;<br />
		}<br />


Thanks-
chlock
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 
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 

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.