프로그래밍/Unity3d

[Unity3d]CryptoTutorial

프리월드 2012. 6. 15. 09:22

Crypto.unitypackage


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;

    }