Unity

[Unity] AES 암호화/ 복호화 펌

Guk-blog 2019. 3. 26. 09:31
728x90
반응형
AES암호화
public static string Encrypt(string textToEncrypt, string key) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = Encoding.UTF8.GetBytes(key); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = keyBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(textToEncrypt); return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); }
AES복호화

public static string Decrypt(string textToDecrypt, string key)

{

RijndaelManaged rijndaelCipher = new RijndaelManaged();

rijndaelCipher.Mode = CipherMode.CBC;

rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 128;

rijndaelCipher.BlockSize = 128;

byte[] encryptedData = Convert.FromBase64String(textToDecrypt);

byte[] pwdBytes = Encoding.UTF8.GetBytes(key);

byte[] keyBytes = new byte[16];

int len = pwdBytes.Length;

if (len > keyBytes.Length)

{

len = keyBytes.Length;

}

Array.Copy(pwdBytes, keyBytes, len);

rijndaelCipher.Key = keyBytes;

rijndaelCipher.IV = keyBytes;

byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);

return Encoding.UTF8.GetString(plainText);

}


개인적으로 참조하여 사용해 기억하고 싶어서 퍼왔습니다

출처 https://mdj1234.tistory.com/41


728x90
반응형

'Unity' 카테고리의 다른 글

[Unity]VulKan vs Open GL ES  (0) 2019.03.26
[Unity]mobile 최적화 가이드  (0) 2019.03.26
[Unity]JSON 파싱하기  (0) 2019.03.26
[Unity] 모바일 개발시 쉽게 로그 찍는법  (0) 2019.03.26
[Unity] 서버에서 시간값 가져오기  (0) 2019.03.26