Wednesday, March 28, 2012

Putting encypted values in a database

hi guys,

Could any one tell me whats the best way to put encypted data into a table.
i have a textfield that a single integer is entered into it. It is then encrypted using DES encrption algorithim.It converts the value to byte(Convert2ByteArray method) I pass the data to a stored procedure which converts it to char before inserting it into the table. when i checked the database all it entered was System.Byte[]. Does anyone know where im goin wrong?

Mairtin

The problem lies because the .ToString method of your byte [] returns "System.Byte[]". You will need to manually convert this over to a string before placing this into your Stored Procedure.

System.Converthas a couple methods to help you with this.

byte [] encryptedBytes = GetEncryptedBytes( unencryptedData );
string encryptedString = System.Convert.ToBase64String( encryptedBytes );

To unencrypt the string you must go the other way.

byte encryptedBytes = System.Convert.FromBase64String( encryptedString );
unencryptedData = GetDecryptedString( encryptedBytes );

bill
|||

hey billrob,

i tried that out.
I dont know if it has done what is suppose to! I wrote the encrypted value out to a textfile aswell to see the difference.

in the database the value was Mw==
in the textfile the value looked like ??Xq/ ê à?*?a?]T¥ù?±@.àcl<tTàFE/.(without System.covert used on it )

Somehow i think i may be going wrong,

|||How were you viewing it in the Database? The binary data looks (from textfile) looks like valid data, post some of your code, but remove your Vector and key from the post.

ToBase64 and FromBase64 are inverse operations. If you take your binary blob, base64 it, then turn it back into binary, you will have the same thing.

How large did you make your text column in the database that stores this information.

bill

FYI, the Base64String stuff is very similiar to how .NET processes the ViewState.

No comments:

Post a Comment