BLOG main image
for our next (37)
Dev (32)
Mac (1)
Windows (2)
FreeTalk (1)
Shell (1)
Private (0)
Visitors up to today!
Today hit, Yesterday hit
daisy rss
tistory 티스토리 가입하기!
2011. 4. 22. 22:19
1. Encrypt
     //-----------------------------------------------------------------------------
    public static void SerializeEncryptObject(string path, object obj, System.Type type, SymmetricAlgorithm key)
    {
        using(FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using(CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
            {
                using(StreamWriter sw = new StreamWriter(cs, Encoding.UTF8))
                {               
                    try
                    {
                        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                        ns.Add("", "");
                       
                        XmlSerializer serializer = new XmlSerializer(type);
                        serializer.Serialize(sw, obj, ns);
                    }
                    catch (System.Exception ex)
                    {
                        Debug.LogWarning(ex.ToString());   
                    }
                    finally
                    {
                        sw.Close();
                        cs.Close();
                        fs.Close();
                    }
                }
            }
        }
    }   

2. Decrypt
    //-----------------------------------------------------------------------------
    public static string LoadXmlFromEncryptFile(string _filePath, SymmetricAlgorithm key)
    {
        if (File.Exists(_filePath) == false)
        {
            _deserializeErrorString = System.String.Format("File({0}) does not exists", _filePath);
            return "";
        }
       
        using(FileStream fs = File.Open(_filePath, FileMode.Open))
        {
            using(CryptoStream cs = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode.Read))
            {
                using(StreamReader sr = new StreamReader(cs, Encoding.UTF8))
                {
                    try
                    {
                        return sr.ReadToEnd();
                    }
                    catch(System.Exception ex)
                    {
                        Debug.LogWarning(ex.ToString());
                        _deserializeErrorString = ex.ToString();
                    }
                    finally
                    {
                        sr.Close();
                        cs.Close();
                        fs.Close();
                    }
                }
            }
        }
       
        return "";
    }

3. Usage
-
private readonly string SECRET_KEY = "12345678";    // must be 64bit, 8bytes
-
private System.Security.Cryptography.DESCryptoServiceProvider mCryptoProvider = null;
-
mCryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
mCryptoProvider.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(SECRET_KEY);        mCryptoProvider.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(SECRET_KEY);

-
SerializeEncryptObject("FilePath", Type, mCryptoProvider);
string xmlString = LoadXmlFromEncryptFile("FilePath", mCryptoProvider);

'Dev > Unity3D' 카테고리의 다른 글

Unity3D Mesh Create Code  (0) 2011.10.20
Unity3D Inspector object 선택후 일괄 처리  (0) 2011.07.02
FindObjectOfType fail.  (0) 2010.12.14
Unity Script 와 Obejct-C 연동.  (0) 2010.11.09
Unity3D 3.0 과 OpenFeint 2.7.4 연동  (4) 2010.11.02