-5

I wanted to encrypt a string value with this method.

    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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tan Leon
  • 61
  • 8
  • How do you want to decrypt after then ? – Rohit Prakash Jan 30 '15 at 07:14
  • 2
    What do you mean by random? If you encrypt the same string twice in a row, would you get two different answers? Then what good is that? – Erik Jan 30 '15 at 07:15
  • use rijndael encryption with a Salt. it will make sure your encrypted string is always different. – Banana Jan 30 '15 at 07:16
  • You may append *random noise* to the string before encryption: `"abc"` -> `abc\01g6$...`. When decrypted just cut off the noise – Dmitry Bychenko Jan 30 '15 at 07:18
  • or you can add some random cipher key to your characters randomly, but you have to keep your cipher key with your encrypted string in a sequential manner so that you can decrypt them again. – Rohit Prakash Jan 30 '15 at 07:25

2 Answers2

0

Here is the code:

StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < r.Next(100); i++) sb.Append((char)r.Next(65, 255));
string s = sb.ToString();

Make sure you store the string or you won't be able to decrypt

Jonathan Camilleri
  • 611
  • 1
  • 6
  • 17
0

If you want some random value for your encryption (for example a salt value, to make the usage of rainbow tables impossible), i always use Guids. They are unique and "random".

Be sure to keep your salt, because you will need it later, for example, if you want to proof wether a password is correct:

PASSWORD + SALT --encryption--> Hash

If you forget the salt, you will not be able to build the same Hash again.

If you want to encrypt passwords, you algorithm is not very useful. I would take the following code-snippet from SO: sha-256 hash in C#

Just be sure, that you pass password + hash to the method, to get your encrypted value with some random "touch".

Community
  • 1
  • 1
BendEg
  • 20,098
  • 17
  • 57
  • 131