The Beauty of unity3d Data Encryption/Decryption

Posted by mantona on Sun, 06 Oct 2019 13:13:18 +0200

Links to the original text: https://blog.csdn.net/ldy597321444/article/details/73468574

 

using System.Collections;
using System.Text;
using System.Security.Cryptography;
using System;
using UnityEngine;
using UnityEngine.UI;
//
//                  _ooOoo_
//                 o8888888o
//                 88" . "88
//                 (| -_- |)
//                 O\  =  /O
//              ____/`---'\____
//            .'  \\|     |//  `.
//          /  \\|||  :  |||//  \
//          /  _||||| -:- |||||-  \
//          |   | \\\  -  /// |   |
//          | \_|  ''\---/''  |   |
//          \  .-\__  `-`  ___/-. /
//        ___`. .'  /--.--\  `. . __
//     ."" '<  `.___\_<|>_/___.'  >'"".
//    | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//    \  \ `-.   \_ __\ /__ _/   .-` /  /
//=====`-.____`-.___\_____/___.-`____.-'======
//                  `=---='
//
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//          Buddha Blessed Never Bug
//          Fast-growing wages do not change demand
//
public class ADDJIEMI : MonoBehaviour
{
    public InputField _input;
    //Get the value of the input box
    private string inputText;
    //Encrypted content
    private string strEncryption;
    private string strkeyValue;
    void Start()
    {
        //Encryption and decryption use the same key and can be any number, but it must be 32 bits.
        strkeyValue = "12345678901234567890198915689039";
    }
    public void encryptionClick()
    {
        inputText = _input.text;
        strEncryption = encryptionContent(inputText, strkeyValue);
        Debug.Log(strEncryption);
    }
    public void decipherClick()
    {
        inputText = decipheringContent(strEncryption, strkeyValue);
        Debug.Log(inputText);
    }
    /// <summary>
    /// Content Encryption
    /// </summary>
    /// <param name="ContentInfo">Encrypt content </param>
    /// <param name="strkey">key value </param>
    /// <returns></returns>
    public string encryptionContent(string ContentInfo, string strkey)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
        RijndaelManaged encryption = new RijndaelManaged();
        encryption.Key = keyArray;
        encryption.Mode = CipherMode.ECB;
        encryption.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = encryption.CreateEncryptor();
        byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(ContentInfo);
        byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    /// <summary>
    /// Content decryption
    /// </summary>
    /// <param name="encryptionContent">encrypted content</param>
    /// <param name="strkey">key value </param>
    /// <returns></returns>
    public string decipheringContent(string encryptionContent, string strkey)
    {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
        RijndaelManaged decipher = new RijndaelManaged();
        decipher.Key = keyArray;
        decipher.Mode = CipherMode.ECB;
        decipher.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = decipher.CreateDecryptor();
        byte[] _EncryptArray = Convert.FromBase64String(encryptionContent);
        byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
    //test
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            encryptionClick();
        }
        if(Input.GetKeyDown(KeyCode.B))
        {
           decipherClick();
        }
    }
}

In fact, the above method is not only applicable to simple string encryption/decryption, think carefully, for text, xml and other file content is also feasible yo!
For example: Encryption/decryption of xml
1. Read the xml file and get the xml content that returns a string
2. Then I wrap the content once by the way I mentioned above, which is a piece of random code in memory. It's impossible for others to change it. Hey hey.
3. When you want to use it, you can use the above decryption method to operate arbitrarily.

Topics: xml