TroubleShooting

암복호화

S_sun 2025. 11. 25. 11:47

StringCipher 사용

  • Decrypt : 암호해독 (복호화)
// 함수 호출
// ReaditEnvConstant.cipherKey = qhdkscjfwj1!
textBox1.Text = StringCipher.Decrypt(textBox2.Text, ReaditEnvConstant.cipherKey);

// 함수
public static string Decrypt(string cipherText, string passPhrase)
{
    byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
    return Decrypt(cipherTextBytes, passPhrase);
}

public static string Decrypt(byte[] cipherTextBytes, string passPhrase)
{
    try
    {
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
        {
          byte[] keyBytes = password.GetBytes(keysize / 8);
          using (RijndaelManaged symmetricKey = new RijndaelManaged())
          {
            symmetricKey.Mode = CipherMode.CBC;
            using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
            {
                using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                    }
                }
            }
          }
        }
    }
    catch (Exception e)
    {
        Log4NetLib.error(e, "StringCipher", "Decrypt");
        return "";
    }
}
  • Encrypt : 암호화
// 함수 호출
// ReaditEnvConstant.cipherKey = qhdkscjfwj1!
textBox2.Text = StringCipher.Encrypt(textBox1.Text, ReaditEnvConstant.cipherKey);

// 함수
public static string Encrypt(string plainText, string passPhrase)
{
    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
    return Encrypt(plainTextBytes, passPhrase);
}

public static string Encrypt(byte[] plainTextBytes, string passPhrase)
{
    try
    {
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            byte[] cipherTextBytes = memoryStream.ToArray();
                            return Convert.ToBase64String(cipherTextBytes);
                        }
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
    	return "Error in Encryption: Invalid Key. System Error: " + ex.Message.ToString();
    }
}

 

 

📖 Reference

 

728x90
반응형

'TroubleShooting' 카테고리의 다른 글

DB 값 SetValue를 이용해 값 Insert  (0) 2025.11.25
DB Multi-Insert  (0) 2025.11.25