Unity3d에서 암호화 하기 위한 Crypto사용 튜토리얼입니다.
C#으로 작성되었습니다.
static string KEY = "anjueolkdiwpoida";
static string IV = "45287112549354892144548565456541"; public string message; // Use this for initialization void Start () { string str_encrypt; if(message != null) str_encrypt = EncryptString(message, KEY, IV); else str_encrypt = EncryptString("Message is null", KEY, IV); Debug.Log(str_encrypt); string str_decrypt = DecryptRJ256(Decode(str_encrypt), KEY, IV); Debug.Log(str_decrypt); } public byte[] Decode(string str) { byte[] decbuff = Convert.FromBase64String(str); return decbuff; } static public String DecryptRJ256(byte[] cypher, string KeyString, string IVString) { UTF8Encoding encoding = new UTF8Encoding(); byte[] Key = encoding.GetBytes(KeyString); byte[] IV = encoding.GetBytes(IVString); string sRet = ""; RijndaelManaged rj = new RijndaelManaged(); rj.Padding = PaddingMode.Zeros; rj.Mode = CipherMode.CBC; rj.KeySize = 256; rj.BlockSize = 256; rj.Key = Key; rj.IV = IV; try { MemoryStream ms = new MemoryStream(cypher); using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader(cs)) { sRet = sr.ReadLine(); } cs.Close(); } } finally { rj.Clear(); } return sRet; } public static string EncryptString(string message, string KeyString, string IVString) { byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString); byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString); string encrypted = null; RijndaelManaged rj = new RijndaelManaged(); rj.BlockSize = 256; rj.Key = Key; rj.IV = IV; rj.Mode = CipherMode.CBC; try { MemoryStream ms = new MemoryStream(); using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(message); sw.Close(); } cs.Close(); } byte[] encoded = ms.ToArray(); encrypted = Convert.ToBase64String(encoded); ms.Close(); } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } catch (UnauthorizedAccessException e) { Console.WriteLine("A file error occurred: {0}", e.Message); return null; } catch (Exception e) { Console.WriteLine("An error occurred: {0}", e.Message); } finally { rj.Clear(); } return encrypted; }
'프로그래밍 > Unity3d' 카테고리의 다른 글
[스크랩]Unity3d 디컴파일 방지 (0) | 2012.09.21 |
---|---|
[Unity3d]Customise in-game cursor (0) | 2012.08.03 |
[Unity3d] 아이폰에서 동영상 플레이 하기. (0) | 2012.07.04 |
[Unity3d]2D스프라이트 메이커 입니다. Nolpan (0) | 2012.07.03 |
[Unity3d]한글완성형 (0) | 2012.06.07 |