Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I wanted to encrypt a string value with this method.

C#
public string EncryptString(string inputString)
       {
           MemoryStream memStream = null;
           try
           {
               byte[] key = { };
               byte[] IV = { 12, 21, 43, 17, 57, 35, 67, 27 };
               string encryptKey = "aXb2uy4z";
               key = Encoding.UTF8.GetBytes(encryptKey);
               byte[] byteInput = Encoding.UTF8.GetBytes(inputString);
               DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
               memStream = new MemoryStream();
               ICryptoTransform transform = provider.CreateEncryptor(key, IV);
               CryptoStream cryptoStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
               cryptoStream.Write(byteInput, 0, byteInput.Length);
               cryptoStream.FlushFinalBlock();

           }
           catch (Exception ex)
           {
               Response.Write(ex.Message);
           }
           return Convert.ToBase64String(memStream.ToArray());
       }




The thing I want to do is that the encrypted string will always be random. Any one know how to do it? Please help. Thanks and Best regards!:D
Posted
Updated 29-Jan-15 22:50pm
v2
Comments
Sujith Karivelil 30-Jan-15 2:25am    
That means you have to generate random string for :
string encryptKey = "aXb2uy4z"; isn't it?
Sergey Alexandrovich Kryukov 30-Jan-15 2:39am    
Random? With a hard-coded key? And how about thinking a bit? And do you really want to encrypt the data eventually?
Or maybe I'm missing something and encryptKey is not actually a key? I see you are using it as data...
What do you mean by this "random" and why?
So far, it does not seem to make any sense.
—SA
CPallini 30-Jan-15 2:59am    
How are you going to decrypt a 'random encrypted' string? :-)
Member 11237565 30-Jan-15 7:53am    
Random as in the encrypted text will always be different. Decrypt will also be same though
Member 11237565 30-Jan-15 7:58am    
Do not care about decryption!!!

You asked for encryption and do not seam to care about decryption.
In this situation there is a quite simple way to create
a random encryption key:
C#
string encryptKey = Path.GetRandomFileName().Replace(".", string.Empty)

More information can be found here:
https://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename(v=vs.110).aspx[^]
 
Share this answer
 
v2
I guess you mean that you want to be able to decrypt it, but you want the encrypted string to vary..

To do this, just append some random text as prefix to what you want to encrypt.. That way, the output will vary, but you still are able to decrypt and remove the prefixed padding... F.eks use DateTime.Now.Ticks + ":" as prefix..
Then you know that the value preceding and including the first ":" will be random data and can be discarded after decrypting
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900