문제

나중에 검색하고 싶은 파일에 비밀번호를 저장하려고 합니다.나중에 원격 서버에 연결하기 위해 비밀번호가 필요하므로 해싱은 옵션이 아닙니다.

다음 코드는 잘 작동하지만 키가 동일하더라도 매번 다른 출력을 생성합니다.애플리케이션이 종료되었다가 다시 시작되면 더 이상 비밀번호를 검색할 수 없기 때문에 이는 좋지 않습니다.비밀번호를 파일에 저장하고 나중에 검색하려면 어떻게 해야 합니까?

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;
    }
}
도움이 되었습니까?

해결책

나는 암호화폐 공급자가 무작위로 IV를 생성하고 있다고 생각합니다.이것을 지정하면 더 이상 달라지지 않습니다.

편집하다:IV 속성을 설정하여 'keyProvider'에서 이 작업을 수행할 수 있습니다.

다른 팁

CreateEncryptor의 문서에 따르면:

현재 IV 속성이 NULL 참조 (Visual Basic의 어떤 것도 없음) 인 경우 GenerateIV 메소드가 호출되어 새로운 랜덤 IV를 생성합니다.

이렇게 하면 암호문이 매번 달라집니다.

메모:이 문제를 해결하는 방법이 논의됩니다. 여기 나는 당신이 일반 텍스트 앞에 Mac을 추가할 수 있다고 제안합니다 ...그러면 암호문의 첫 번째 블록은 사실상 IV이지만 모두 반복 가능합니다.

임의의 벡터를 생성하더라도 IV(초기화 벡터)를 지정해야 합니다.무작위 IV를 사용하는 경우 나중에 해독할 때 사용할 수 있도록 암호문과 함께 저장해야 합니다. 또는 다른 데이터에서 IV를 파생할 수 있습니다(예를 들어 비밀번호를 암호화하는 경우 다음에서 IV를 파생할 수 있습니다). 사용자 이름).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top