How to Encrypt a String to a String?

Hi Folks,

I’m using Triple DES to encrypt a string… the output of the encryption is a byte array.
However, I want to store the encrypted result as a string in a SQL Database and not as a byte array.
(The reason I want to store it as a string, is to be able to copy & paste the result to a standalone windows form application  to decrypt it...) 

I’ve tried taking the byte array, converting it into a string…And then reconverting it into a byte array for the decryption. This causes an error upon decryption…
How can I convert the encrypted byte array into a string and back into a byte array, so that it keeps it’s integrity… in order to be able to decrypt it…

Regards,
Joel

 
"The truth is rarely pure and never simple."
- Oscar Wilde, The Importance of Being Earnest
0
langjoel
10/26/2008 6:11:29 AM
asp.net.security 27051 articles. 1 followers. Follow

3 Replies
963 Views

Similar Articles

[PageSpeed] 35

you need to use Base64String . here is an example : 

encrypt :

 

byte[] IV = new byte[8] {240, 32, 45, 29, 0, 76, 173, 59};
string cryptoKey = "All you need is Love";
string MyString = "I LOVE ASP.net"; 
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(MyString);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
des.Key = MD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(cryptoKey));
des.IV = IV;
byte[] CodedBuffer = des.CreateEncryptor().TransformFinalBlock(buffer,0,buffer.Length);
Label1.Text = System.Convert.ToBase64String(CodedBuffer,0,CodedBuffer.Length);

 decrypt :

byte[] IV = new byte[8] {240, 32, 45, 29, 0, 76, 173, 59};
string cryptoKey = "All you need is Love";
string CodedString64 = "zNvKahC6S/+8xMX3o658wQ==";
byte[] buffer = Convert.FromBase64String(CodedString64);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
des.Key = MD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(cryptoKey));
des.IV = IV;
byte[] CodedBuffer = des.CreateDecryptor().TransformFinalBlock(buffer,0,buffer.Length);
Label1.Text = System.Text.Encoding.ASCII.GetString (CodedBuffer);
 http://www.rooznamechi.com/Article/26/default.aspx  (Persian)
Don't forget to click "Mark as Answer" on the post(s) that helped you.
0
H_R
10/26/2008 9:11:58 AM

Thank you very much!

I was using the wrong method to convert a byte array back to a string...!

Regards,

- Joel


"The truth is rarely pure and never simple."
- Oscar Wilde, The Importance of Being Earnest
0
langjoel
10/26/2008 5:12:15 PM

you are welcome Smile


Don't forget to click "Mark as Answer" on the post(s) that helped you.
0
H_R
10/27/2008 7:27:19 AM
Reply:

Similar Artilces:

RSA Encryption
Here is my goal:1. Take a string2. Encrypt it 3. Pass it as a parameter in the QueryString4. Decrypt it The value starts as a string, then is converted to a byte[] and then encrypted.  The resulting byte[] is converted to a string and send as a parameter.  The recieving page decrypts the string (creates a byte[], decrypts to a new byte[], and the value is finally parsed for its values) I am using http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrsacryptoserviceproviderclasstopic.asp with a small modification t...

string and string[]
hello, whats the difference here between string and string[] thanks "string" denotes an item of type String "string[]" denotes an array of type String itemsThanks, EdMicrosoft MVP - ASP/ASP.NET Gracia mon ami...

String and string?
Hi guys, I was wondering the difference between small letter string type and a capital letter String type. Does anyone know about this? Highly appreciate for your answers! Thankz! The "proper" version is String, as it references the System.String class of the .NET Framework Class Library. The ability to use string is provided by C#, where it is an alias to String. Similarly, the "proper" version of an integer is Int32, as it references System.Int32.  The ability to use int is provided by C#, and the ability to use Integer is provided by VB.NET, and both int and Integer are aliases...

string to string[]
Hi, I've got this code : string[] Params; string SQL = "SELECT * FROM T_MANAGEMENT_PAGES"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Params_Type = myReader.GetValue(0).ToString(); } } catch { } finally { } myReader.Close(); My problem is to obtain Params_Type. But each time, it says : "impossible to convert '[object]' in 'string[]' &quo...

String and String[]
What will be the advantage of using myString: String[10] over myString: String Man wrote: > What will be the advantage of using > myString: String[10] > over > myString: String "String[10]" is a fixed-length, statically allocated, Ansi-based ShortString. "String" is a dynamic-length, dynamically allocated, reference counted, Ansi-based or Unicode-based (depening on Delphi version) Long string. Unless you absolutely need the static, fixed length nature of ShortString, such as in records that interact with external systems, you should stay ...

string() = string()
this code does not work (not instance of object) Dim Items(), Other() As String Items = New String() {"A","B","C"} Other = New String() {} Other = Items how can I get all the values ? angiras Hi, If I understand your requirement correctly, this should do it Dim Items() As String = {"A", "B", "C"} Dim Other(Items.Length - 1) As String Items.CopyTo(Other, 0) To more closely match you code above, but this will be less efficient Dim Items(), Other() As String ...

a string in a string in a string in my code behind....
I know my title talk about code-behind, but this is really a client-side problem (and surely a newby one too, sorry about that).I have a javascript function that my make a little text appear when the onmouseover event of a link is activated.So, normally, if my link was inside my html code, it would be something like this:<a href = "whocare.com"  onmouseover="poplink('I want to test something');">test</a>BUT, for some reason, I have to write this part in code behind with the LiteralControl function....and that will give something like that:this.Controls.Add(new LiteralCo...

m_AVP As IDictionary(Of String, String) = New Dictionary(Of String, String) ,not defined?
   I am creating a usercontrol (ascx,ascx.vb)   I have used Imports system.Collections Imports system.Collections.Generic     Private m_AttributeValuePair As IDictionary(Of String, String) = New Dictionary(Of String, String)     But I am gettting an error “Type Dictionary is not defined”   And also I am getting intelisense to type the line But what is the reason for my error ? How can I get escape from the error ?         this error is not showing today  ...

(string)(string)(100)
Hi, Though its stupid of me but incase I typecase a string  into string multiple times or any other datatype to itself multiple times will there be some error at some stage. (string)((string)(100)) (int)((int)("1000")) help, buddy.    thanks,Buddy Well it raises the question of what you are trying to accomplish by doing this. I can't think of any reason why you might want to case twice this way. However, typically in .Net if you want to cast something to a string you use the .ToString() method, for example: int nCount = 100; string sCount = nCount.ToString(); ...

what is the difference between String and string ?
first, i thought that String is reference-type and string is value-type, so I try: String s; s = "123"; String s2; s2 = s; s2 = "456"; MessageBox.Show(s); but the result is not as good as i thought, it show "123", not "456" so what is the difference between String and string ?MSN: unfriendlyboy@hotmail.comASP.NET fan. There is no difference. string is just an c# alias for CLR type System.String, just like int is an alias for System.Int32Leon LangleybenMCSD, ASP.NET MVPBlog...

Count no of strings in a string
dim ref as string ="<a href=""/iyp/yp/ypkeywordsearch.do?" hi i would like to know something how do i count the number of ref in a html document??you see i have another string call htmldoc(it contains a html document of a web page) and i want to extract all the ref in htmldoc how do i do that? 2) How do i include a closed inverted comma in a string?? dim a as string = "abcde"fghijk""" How do i use the mid method to seacrh from " ?? 1) You should learn about Regular Expressions 2) Double the quotes: "abcde"...

String and String[30]
Any advantage in using this style in defining a string type variable? myName: String; hisName: String[30]; "Alan Tse" <alan_NO_SPAM_pltse@yahoo.com.au> wrote in message news:177975@forums.codegear.com... > Any advantage in using this style in defining a string type variable? > > myName: String; > hisName: String[30]; String[30] is a short string type and is ANSI only. You should use simple String type (Unicode in D2009+). -- Wayne Niddery (TeamB) Alan Tse wrote: > Any advantage in using this style in defining a string type variable? ...

REPLACE STRING IN STRING
Hello! I use SQLANY 5.5.05 and loking for one function which will do following: find string in string and replace it with another string. In version ASA 6 I found function STUFF which I think do something like this. Is there anything in 5.5.05? Regards Tomaz Try this: create function ReplaceString(in str long varchar,in old_substr char(255),in new_substr char(255)) returns long varchar begin // Change all occurrences of old_substr to new_substr in str. declare start_pos integer; declare found_pos integer; declare new_str long varchar; set new_str=&#...

string to string[] #2
string[] strArray;while(dtardrUsrDet.Read())   {string strUsrEmail=dtardrUsrDet.GetValue(0).ToString(); int intDayDiff=System.Convert.ToInt16(dtardrUsrDet.GetValue(1).ToString());if(intDayDiff==45) {   strArray=strUsrEmail; }if(intDayDiff==30) {   strArray=strUsrEmail; }     how to store string values from reader into string arrayMark As Answer If my reply helped you. Hi, I am assuming that you want each string in an array, not each character as a separate string in a string array here.... The easiest way is to create an ArrayList, add each stri...

Web resources about - How to Encrypt a String to a String? - asp.net.security

Let's Encrypt - Wikipedia, the free encyclopedia
that provides free X.509 certificates for Transport Layer Security encryption (TLS) via an automated process designed to eliminate the current ...

Encrypt Your Facebook Messages With Cryptocat
Open-source chat-encryption tool Cryptocat announced its latest update, which includes full encryption for Facebook private messages , in a blog ...

Let's Encrypt (@letsencrypt) on Twitter
Log in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here Let's Encrypt @ letsencrypt ...

Encrypt A Pic for iPhone 3GS, iPhone 4, iPhone 4S, iPhone 5, iPod touch (4th generation), iPod touch ...
Get Encrypt A Pic on the App Store. See screenshots and ratings, and read customer reviews.

SurfEasy VPN - Protect, Encrypt & Unblock the Internet - YouTube
SurfEasy encrypts all Internet traffic on your smartphone, tablet and computer to protect your online privacy and security. We give you the tools ...

Google encrypts China searches, defying Great Firewall
Google has begun routinely encrypting web searches conducted in China, posing a bold new challenge to that nation's powerful system for censoring ...

Firefox first browser to encrypt Google search by default
Firefox first browser to encrypt Google search by default CSO Magazine That change meant websites would no longer receive “referrer” data that ...

How To Secure And Encrypt Your Web Browsing On Public Networks (With Hamachi And Privoxy)
When you’re browsing from a public Wi-Fi connection — like at your favourite coffee shop — anyone on that network can snoop on what you&#821... ...

Google encrypts data to block government spies
The tech giant is racing to make it more difficult for outsiders to intercept user information. Google is racing to encrypt the torrents of information ...

Police to encrypt Hunter radios
POLICE scanners across the Hunter will go quiet next month under plans to encrypt communications between officers.

Resources last updated: 12/28/2015 9:12:33 AM