0

私が作成した暗号化クラスに問題があります:

public static class Encryption {

    public static string EncryptToString(string TextToEncrypt, byte[] Key, byte[] IV = null)
    {
        return ByteArrToString(EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV));        
    }

    public static string EncryptToString(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null)
    {
        return ByteArrToString(EncryptStringToBytes(BytesToEncrypt, Key, IV));  
    }

    public static byte[] EncryptToBytes(string TextToEncrypt, byte[] Key, byte[] IV = null)
    {
        return EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV);
    }

    public static byte[] EncryptToBytes(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null)
    {
        return EncryptStringToBytes(BytesToEncrypt, Key, IV);
    }
    
    public static string DecryptToString(string EncryptedText, byte[] Key,byte[] IV=null) 
    {
        return ByteArrToString(DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV));            
    }

    public static string DecryptToString(byte[] EncryptedBytes, byte[] Key,byte[] IV=null) 
    {
        return ByteArrToString(DecryptStringFromBytes(EncryptedBytes, Key, IV));            
    }

    public static byte[] DecryptToBytes(string EncryptedText, byte[] Key,byte[] IV=null) 
    {
        return DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV);            
    }

    public static byte[] DecryptToBytes(byte[] EncryptedBytes, byte[] Key,byte[] IV=null) 
    {
        return DecryptStringFromBytes(EncryptedBytes, Key, IV);            
    }
            
    private static byte[] EncryptStringToBytes(byte[] TextToEncrypt, byte[] Key, byte[] IV=null)
    {
        Debug.WriteLine("Password: " + ByteArrToString(Key));
        Debug.WriteLine("IV: " + ByteArrToString(IV));                        

        byte[] encrypted;
        // Create an Rijndael object 
        // with the specified key and IV. 
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(TextToEncrypt);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }
        
        // Return the encrypted bytes from the memory stream. 
        return encrypted;
    }

    private static byte[] DecryptStringFromBytes(byte[] EncryptedText, byte[] Key, byte[] IV)
    {
        Debug.WriteLine("Password: " + ByteArrToString(Key));
        Debug.WriteLine("IV: " + ByteArrToString(IV));

        byte[] fromEncrypt = new byte[EncryptedText.Length];

        // Create a Rijndael object with the specified key and IV. 
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(EncryptedText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    //read stream into byte array
                    csDecrypt.Read(fromEncrypt,0,fromEncrypt.Length);                        
                }
            }
        }

        return fromEncrypt;
    }
    
    public static byte[] StrToByteArray(string str)
    {
        if (str.Length == 0)
            throw new Exception("Invalid string value in StrToByteArray");

        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

    public static string ByteArrToString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }

    public static byte[] GetIV()
    {
        byte[] randomArray = new byte[16];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(randomArray);
        return randomArray;
    }
}

私は次のようにテストします:

byte[] iv =  Encryption.GetIV();
byte[] password = Encryption.StrToByteArray("password");

string encrypted = Encryption.EncryptToString("Hello", password, iv);
Debug.WriteLine("Result: " + Encryption.DecryptToString(encrypted, password, iv));

これは、デバッグ ウィンドウに表示される結果です。

パスワード: パスワード

IV: 䴞ㆫ튾꛽輔

パスワード: パスワード

IV: 䴞ㆫ튾꛽輔

結果:祓瑳浥䈮瑹孥]

エラーは発生しません。ただのばかげた結果。

それが初期化ベクトル、ストリーム、または私が見逃している何かの問題なのかどうかはわかりません。

4

1 に答える 1