Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encrypt a string of text so that the encrypted string is different each time even though the value is the same

I want to encrypt a string of text securely and have the encrypted string different each time, but always be able to decrypt.

For example, if I encrypt the text "FooBar" at two separate times, I want the encrypted string to look different each time, how would I go about this?

Also, what is the most secure type of encryption that I can use to do this?

I want to use C# with .Net if possible but I am a total encryption noob!

Thanks

like image 212
JMK Avatar asked Jan 30 '12 12:01

JMK


People also ask

Can different encrypted strings result in same cipher text?

Most encryption schemes are designed so that encrypting the same value twice generates different encrypted values. This is a security property. Encryption schemes such that encrypting the same value twice results in the same ciphertext are said to be deterministic.

How do I encrypt a string?

Given a string s, the task is to encrypt the string in the following way: If the frequency of current character is even, then increment current character by x. If the frequency of current character is odd, then decrement current character by x.

What are the two types of methods to encrypt data?

The two types of data encryption methods are Symmetric Encryption and Asymmetric Encryption. Symmetric encryption is also known as private-key cryptography or secret key algorithm and requires both the parties of sender and receiver to have access to the same key to decrypt the data.

How do I encrypt text data?

You can encrypt the text file directly or put it in a new folder and then encrypt the folder. Right-click the file, select Properties and click the Advanced button. Select Encrypt Contents to Secure Data. Note that you can also compress the contents to save disk space.


1 Answers

I want to encrypt a string of text securely and have the encrypted string different each time, but always be able to decrypt.

This is an extremely common requirement.

Every answer so far says to use a salt. This is incorrect, as you'd know if you read the first sentence of the wikipedia page on the subject:

"In cryptography, a salt consists of random bits, creating one of the inputs to a one-way function."

Do you want a one-way function? No. You just said that you need to be able to decrypt the string, so it cannot be a one-way function.

What you want is an initialization vector.

"In cryptography, an initialization vector is a fixed-size input to a cryptographic primitive that is typically required to be random or pseudorandom. Randomization is crucial for encryption schemes to achieve semantic security, a property whereby repeated usage of the scheme under the same key does not allow an attacker to infer relationships between segments of the encrypted message."

You wisely point out:

I am a total encryption noob!

Then do not try to do this yourself; you will get it wrong. It is very easy to misuse crypto to build a system that you, the noob, cannot break. Remember, you goal is to make a system that someone who knows a lot more about crypto than you do cannot break.

Also, asking random strangers for help on the internet is a bad way to learn the truth about cryptography. Most people who can answer this question, myself included, are only slightly less noobish than you are. Just take a look at all the awful advice on this page so far. Two answers, for example, suggest to use the current time as a random salt. That is utter craziness; the whole point of a salt is that it is not predictable in advance! Get advice from real experts who know what they are talking about.

My advice: hire an expert to solve your particular problem. To learn about crypto, start with some introductory texts like "Applied Cryptography" so that you understand what problems crypto actually solves and how to use it effectively. Crypto is not magic pixie dust that you sprinkle on data to make it secure; it is only one small part of a whole strategy for securing data against attacks.

like image 91
Eric Lippert Avatar answered Sep 30 '22 02:09

Eric Lippert