The encryption output is always different even with one key.

I am trying to save a password in a file that I would like to receive later. Hashing is not an option, as I need a password to connect to a remote server for later versions.

The following code works well, but a different output is generated each time, even if the key is the same. This is bad, because when the application shuts down and restarts, I can no longer get the password. How to save passwords in a file and get them later?

public class EncyptDecrypt { static System.Security.Cryptography.TripleDESCryptoServiceProvider keyProv = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); public static System.Security.Cryptography.TripleDESCryptoServiceProvider KeyProvider { get { keyProv.Key = new byte[] { /* redacted with prejudice */ }; return keyProv; } } public static string Encrypt(string text, SymmetricAlgorithm key) { if (text.Equals(string.Empty)) return text; // Create a memory stream. MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(text); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. byte[] buffer = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return System.Convert.ToBase64String(buffer); } // Decrypt the byte array. public static string Decrypt(string cypherText, SymmetricAlgorithm key) { if (cypherText.Equals(string.Empty)) return cypherText; string val; try { // Create a memory stream to the passed buffer. MemoryStream ms = new MemoryStream(System.Convert.FromBase64String(cypherText)); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream. StreamReader sr = new StreamReader(encStream); // Read the stream as a string. val = sr.ReadLine(); // Close the streams. sr.Close(); encStream.Close(); ms.Close(); } catch (System.Exception) { return string.Empty; } return val; } } 
+4
source share
3 answers

I believe that it happens that the cryptosystem provider accidentally generates IV. Indicate this and it will no longer be different.

Edit: you can do this in your "keyProvider" by setting property IV.

+8
source

According to the CreateEncryptor docs:

If the current IV property is a null reference (nothing in Visual Basic), the GenerateIV method is called to create a new random IV.

This will make the ciphertext different every time.

Note: the method was discussed here , where I suggest you add text using mac mac ... then the first block of ciphertext is actually IV, but everything is repeatable

+3
source

You need to specify IV (initialization vector), even if you produce random. If you use a random IV, you must save it along with the encrypted text in order to use it later for decryption, or you can get IV from some other data (for example, if you encrypt the password, you can get IV from the username).

+2
source

Source: https://habr.com/ru/post/1277050/


All Articles