[c#] 뮤터블과 이뮤터블의 데이터 암호화와 해독 방법

뮤터블(Mutable) 데이터의 암호화 및 해독

뮤터블 데이터는 변경 가능한 데이터를 나타내며, 이러한 데이터에 대한 암호화 및 해독은 주의를 요합니다. 데이터가 수정될 수 있기 때문에 암호화된 데이터가 보호되지 않을 수 있습니다. 또한, 데이터를 암호화하기 전에 변경되었을 경우 암호해제시 데이터 유효성 문제가 발생할 수 있습니다. 이를 해결하기 위해서는 데이터를 암호화하기 전에 해시를 계산하고, 암호화된 데이터에 해시를 포함시켜 데이터의 무결성을 보장할 수 있습니다.

using System;
using System.Security.Cryptography;

public class MutableDataEncryption
{
    public string EncryptData(string data, string key)
    {
        byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(data);
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = System.Text.Encoding.UTF8.GetBytes(key);
            aesAlg.IV = new byte[16]; // Initialization Vector
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            byte[] encryptedData = encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
            byte[] hash = SHA256.Create().ComputeHash(dataBytes);            
            byte[] result = new byte[hash.Length + encryptedData.Length];
            Buffer.BlockCopy(hash, 0, result, 0, hash.Length);
            Buffer.BlockCopy(encryptedData, 0, result, hash.Length, encryptedData.Length);
            return Convert.ToBase64String(result);
        }
    }

    public string DecryptData(string encryptedData, string key)
    {
        byte[] encryptedDataBytes = Convert.FromBase64String(encryptedData);
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = System.Text.Encoding.UTF8.GetBytes(key);
            aesAlg.IV = new byte[16]; // Initialization Vector
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
            byte[] decryptedHash = new byte[32];
            byte[] decryptedData = new byte[encryptedDataBytes.Length - 32];
            Buffer.BlockCopy(encryptedDataBytes, 0, decryptedHash, 0, 32);
            Buffer.BlockCopy(encryptedDataBytes, 32, decryptedData, 0, encryptedDataBytes.Length - 32);
            byte[] computedHash = SHA256.Create().ComputeHash(decryptedData);
            if (!computedHash.SequenceEqual(decryptedHash))
            {
                throw new CryptographicException("Data integrity compromised.");
            }
            return System.Text.Encoding.UTF8.GetString(decryptedData);
        }
    }
}

위의 예제 코드는 뮤터블 데이터를 암호화 및 해독하기 위한 C#의 간단한 예제입니다. 데이터를 암호화할 때에는 먼저 데이터에 대한 해시를 계산하고, 이 해시를 암호화한 데이터와 함께 저장하여 데이터의 무결성을 확인하는 방법을 사용합니다.

이뮤터블(Immutable) 데이터의 암호화 및 해독

이뮤터블 데이터는 변경 불가능한 데이터를 나타내며, 뮤터블 데이터와 달리 이뮤터블 데이터를 암호화하고 해독하는 것은 더욱 간단합니다. 이뮤터블 데이터이므로 데이터의 무결성을 확인할 필요가 없으며, 데이터를 한 번 암호화하면 데이터가 변경될 우려가 없습니다.

using System;
using System.Security.Cryptography;

public class ImmutableDataEncryption
{
    public byte[] EncryptData(byte[] data, string key)
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = System.Text.Encoding.UTF8.GetBytes(key);
            aesAlg.IV = new byte[16]; // Initialization Vector
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            return encryptor.TransformFinalBlock(data, 0, data.Length);
        }
    }

    public byte[] DecryptData(byte[] encryptedData, string key)
    {
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = System.Text.Encoding.UTF8.GetBytes(key);
            aesAlg.IV = new byte[16]; // Initialization Vector
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
            return decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
        }
    }
}

이뮤터블 데이터의 경우에는 무결성 문제를 확인할 필요가 없으며, 위의 예제 코드처럼 데이터를 간단히 암호화 및 해독할 수 있습니다.

결론

데이터의 무결성이 중요한 경우에는 뮤터블 데이터를 암호화하기 전에 데이터의 무결성을 확인하는 추가적인 작업이 필요합니다. 반면에 이뮤터블 데이터는 데이터가 변경될 가능성이 없으므로 이러한 추가적인 작업이 필요하지 않습니다. 데이터를 안전하게 암호화하고 해독하기 위해서는 데이터의 특성에 따라 적절한 방법을 선택하는 것이 중요합니다.