본문 바로가기

프로그램/유니티 스크립트 소스

[유니티 스크립트 소스] Cryptor

728x90
반응형
using UnityEngine;
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public class CryptorDemo : MonoBehaviour
{
    private static string m_Error = "Error";
    private string m_SymmetricKey = "220615!@"; // 대칭키 8자리

    void Start()
    {
        string sourceMd5 = "Source MD5";
        string sourceMd5Hash = EncryptMd5(sourceMd5, Encoding.ASCII);
        if (sourceMd5Hash != m_Error)
            Debug.LogFormat($"SourceMd5 : {sourceMd5} \nSourceMd5Hash : {sourceMd5Hash}");

        string sourceSha256 = "Source SHA256";
        string sourceSha256Hash = EncryptSha256(sourceSha256, Encoding.ASCII);
        if(sourceSha256Hash != m_Error)
            Debug.LogFormat($"SourceMSha256 : {sourceSha256} \nSourceSha256Hash : {sourceSha256Hash}");

        string sourceSha384 = "Source SHA384";
        string sourceSha384Hash = EncryptSha384(sourceSha384, Encoding.ASCII);
        if (sourceSha384Hash != m_Error)
            Debug.LogFormat($"SourceSha384 : {sourceSha384} \nSourceSha384Hash : {sourceSha384Hash}");

        string sourceSha512 = "Source SHA512";
        string sourceSha512Hash = EncryptSha512(sourceSha512, Encoding.ASCII);
        if (sourceSha256Hash != m_Error)
            Debug.LogFormat($"SourceSha512 : {sourceSha512} \nsourceSha512Hash : {sourceSha512Hash}");

        DESCryptoServiceProvider des = new DESCryptoServiceProvider();        
        
        des.Key = ASCIIEncoding.ASCII.GetBytes(m_SymmetricKey);
        des.IV = ASCIIEncoding.ASCII.GetBytes(m_SymmetricKey);

        string original = "Original";
        string desEncrypt = DESEncrypt(des, original);
        Debug.LogFormat($"DESEncrypt : {desEncrypt}");
        string desDecrypt = DESDecrypt(des, desEncrypt);
        Debug.LogFormat($"DESDecrypt : {desDecrypt}");
    }

    public static string EncryptMd5(string source, Encoding encoding)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            string hash = GetHash(md5Hash, source, encoding);

            if (VerifyHash(md5Hash, source, hash, encoding))
            {
                return hash;
            }

            else
            {
                return m_Error;
            }
        }
    }

    public static string EncryptSha256(string source, Encoding encoding)
    {
        using (SHA256 sha256Hash = SHA256.Create())
        {
            string hash = GetHash(sha256Hash, source, encoding);

            if (VerifyHash(sha256Hash, source, hash, encoding))
            {
                return hash;
            }

            else
            {
                return m_Error;
            }
        }
    }

    public static string EncryptSha384(string source, Encoding encoding)
    {
        using (SHA384 sha384Hash = SHA384.Create())
        {
            string hash = GetHash(sha384Hash, source, encoding);

            if (VerifyHash(sha384Hash, source, hash, encoding))
            {
                return hash;
            }

            else
            {
                return m_Error;
            }
        }
    }

    public static string EncryptSha512(string source, Encoding encoding)
    {
        using (SHA512 sha512Hash = SHA512.Create())
        {
            string hash = GetHash(sha512Hash, source, encoding);

            if (VerifyHash(sha512Hash, source, hash, encoding))
            {
                return hash;
            }

            else
            {
                return m_Error;
            }
        }
    }

    private static string GetHash(HashAlgorithm hashAlgorithm, string input, Encoding encoding)
    {
        byte[] data = hashAlgorithm.ComputeHash(encoding.GetBytes(input));

        var sBuilder = new StringBuilder();

        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }

    private static bool VerifyHash(HashAlgorithm hashAlgorithm, string input, string hash, Encoding encoding)
    {
        var hashOfInput = GetHash(hashAlgorithm, input, encoding);

        StringComparer comparer = StringComparer.OrdinalIgnoreCase;

        return comparer.Compare(hashOfInput, hash) == 0;
    }

    public static string DESEncrypt(DESCryptoServiceProvider des, string original)
    {
        byte[] inBlocks = UnicodeEncoding.Unicode.GetBytes(original);
        ICryptoTransform Encrypt = des.CreateEncryptor();
        byte[] outBlocks = Encrypt.TransformFinalBlock(inBlocks, 0, inBlocks.Length);

        return System.Convert.ToBase64String(outBlocks);
    }

    public static string DESDecrypt(DESCryptoServiceProvider des, string encrypted)
    {
        byte[] inBlock = System.Convert.FromBase64String(encrypted);
        ICryptoTransform Decrypt = des.CreateDecryptor();
        byte[] outBlock = Decrypt.TransformFinalBlock(inBlock, 0, inBlock.Length);

        return UnicodeEncoding.Unicode.GetString(outBlock);
    }
}
728x90
반응형