Domanda

Sto provando a memorizzare una password in un file che vorrei recuperare per dopo. L'hash non è un'opzione poiché ho bisogno della password per connettermi a un server remoto per dopo.

Il seguente codice funziona bene, ma crea ogni volta un output diverso anche se la chiave è la stessa. Questo è male come quando l'applicazione si spegne e si riavvia, non sarò più in grado di recuperare la mia password. Come posso archiviare le password in un file e recuperarle in seguito?

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;
    }
}
È stato utile?

Soluzione

Credo che ciò che sta accadendo sia che il provider di criptovalute sta generando casualmente un IV. Specificare questo e non dovrebbe più differire.

Modifica: puoi farlo nel tuo 'keyProvider' impostando la proprietà IV.

Altri suggerimenti

Secondo i documenti di CreateEncryptor:

  

Se la proprietà IV corrente è nulla   riferimento (Nothing in Visual Basic),   viene chiamato il metodo GenerateIV   creare un nuovo IV casuale.

Questo renderà il testo cifrato diverso ogni volta.

Nota: un modo per aggirare questo problema è discusso qui dove ti suggerisco di poter anteporre il testo in chiaro con un mac ... quindi il primo blocco di testo cifrato è effettivamente il IV, ma è tutto ripetibile

Devi specificare un IV (vettore di inizializzazione), anche se ne generi uno casuale. Se si utilizza IV casuale, è necessario memorizzarlo insieme al testo cifrato in modo da poterlo utilizzare in seguito alla decrittografia oppure è possibile derivare un IV da alcuni altri dati (ad esempio se si sta crittografando una password, è possibile derivare il IV da il nome utente).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top