IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity MD5工具(加密/解密) -> 正文阅读

[游戏开发]Unity MD5工具(加密/解密)

using UnityEngine;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary> 功能:MD5工具 </summary>
public class MD5Tool : Singleton<MD5Tool> {

? ? /// <summary> 根据路径获取文件的MD5 </summary>
? ? public string BuildFileMd5(string fliePath) {
? ? ? ? string filemd5 = null;
? ? ? ? try {
? ? ? ? ? ? using (var fileStream = File.OpenRead(fliePath)) {
? ? ? ? ? ? ? ? var md5 = MD5.Create();
? ? ? ? ? ? ? ? var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? filemd5 = FormatMD5(fileMD5Bytes);
? ? ? ? ? ? }
? ? ? ? } catch (System.Exception ex) {
? ? ? ? ? ? Debug.LogError(ex);
? ? ? ? }
? ? ? ? return filemd5;
? ? }

? ? private byte[] ReadFile(string fileName) {
? ? ? ? FileStream pFileStream = null;
? ? ? ? byte[] pReadByte = new byte[0];
? ? ? ? try {
? ? ? ? ? ? pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
? ? ? ? ? ? BinaryReader r = new BinaryReader(pFileStream);
? ? ? ? ? ? r.BaseStream.Seek(0, SeekOrigin.Begin); ? ?//将文件指针设置到文件开
? ? ? ? ? ? pReadByte = r.ReadBytes((int)r.BaseStream.Length);
? ? ? ? ? ? return pReadByte;
? ? ? ? } catch {
? ? ? ? ? ? return pReadByte;
? ? ? ? } finally {
? ? ? ? ? ? if (pFileStream != null)
? ? ? ? ? ? ? ? pFileStream.Close();
? ? ? ? }
? ? }
? ? /// <summary> 根据路径获取文件解密后的MD5 </summary>
? ? public string BuildFileMd5_AES(string fliePath) {
? ? ? ? string filemd5 = null;
? ? ? ? try {
? ? ? ? ? ? byte[] bytes = ReadFile(fliePath);
? ? ? ? ? ? byte[] bytesAES = AESTool.AESDecrypt(bytes);
? ? ? ? ? ? var md5 = MD5.Create();
? ? ? ? ? ? var fileMD5Bytes = md5.ComputeHash(bytesAES);//计算指定Stream 对象的哈希值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? filemd5 = FormatMD5(fileMD5Bytes);
? ? ? ? } catch (System.Exception ex) {
? ? ? ? ? ? Debug.LogError(ex);
? ? ? ? }
? ? ? ? return filemd5;
? ? }


? ? /// <summary> 将byte[]装换成字符串 </summary>
? ? private string FormatMD5(byte[] data) {
? ? ? ? return System.BitConverter.ToString(data).Replace("-", "").ToLower();
? ? }

? ? /// <summary> 根据字符串生成MD5 </summary>
? ? public string EncryptWithMD5(string con) {
? ? ? ? byte[] sor = Encoding.UTF8.GetBytes(con);
? ? ? ? MD5 md5 = MD5.Create();
? ? ? ? byte[] result = md5.ComputeHash(sor);
? ? ? ? StringBuilder strbul = new StringBuilder(40);
? ? ? ? for (int i = 0; i < result.Length; i++) {
? ? ? ? ? ? strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位

? ? ? ? }
? ? ? ? return strbul.ToString();
? ? }
}
?

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
//功能:AES加解密工具
public class AESTool {

? ? #region AES带头 加、解密
? ? private static string AESHead = "AESEncrypt";

? ? /// <summary>
? ? /// 文件加密,传入文件路径
? ? /// </summary>
? ? /// <param name="path"></param>
? ? /// <param name="EncrptyKey"></param>
? ? private static void AESFileEncrypt(string path) {
? ? ? ? if(!File.Exists(path))
? ? ? ? ? ? return;

? ? ? ? try {
? ? ? ? ? ? using(FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite)) {
? ? ? ? ? ? ? ? if(fs != null) {
? ? ? ? ? ? ? ? ? ? //读取字节头,判断是否已经加密过了
? ? ? ? ? ? ? ? ? ? byte[] headBuff = new byte[10];
? ? ? ? ? ? ? ? ? ? fs.Read(headBuff,0,headBuff.Length);
? ? ? ? ? ? ? ? ? ? string headTag = Encoding.UTF8.GetString(headBuff);
? ? ? ? ? ? ? ? ? ? if(headTag == AESHead) {
? ? ? ? ? ? ? ? ? ? ? ? Debug.Log(path + "已经加密过了!");
? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //加密并且写入字节头
? ? ? ? ? ? ? ? ? ? fs.Seek(0,SeekOrigin.Begin);
? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[fs.Length];
? ? ? ? ? ? ? ? ? ? fs.Read(buffer,0,Convert.ToInt32(fs.Length));
? ? ? ? ? ? ? ? ? ? fs.Seek(0,SeekOrigin.Begin);
? ? ? ? ? ? ? ? ? ? fs.SetLength(0);
? ? ? ? ? ? ? ? ? ? byte[] headBuffer = Encoding.UTF8.GetBytes(AESHead);
? ? ? ? ? ? ? ? ? ? fs.Write(headBuffer,0,headBuffer.Length);
? ? ? ? ? ? ? ? ? ? byte[] EncBuffer = AESEncrypt(buffer);
? ? ? ? ? ? ? ? ? ? fs.Write(EncBuffer,0,EncBuffer.Length);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? Debug.LogError(e);
? ? ? ? }
? ? }

? ? /// <summary>
? ? /// 文件解密,传入文件路径(会改动加密文件,不适合运行时)
? ? /// </summary>
? ? /// <param name="path"></param>
? ? /// <param name="EncrptyKey"></param>
? ? private static void AESFileDecrypt(string path) {
? ? ? ? if(!File.Exists(path)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? using(FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite)) {
? ? ? ? ? ? ? ? if(fs != null) {
? ? ? ? ? ? ? ? ? ? byte[] headBuff = new byte[10];
? ? ? ? ? ? ? ? ? ? fs.Read(headBuff,0,headBuff.Length);
? ? ? ? ? ? ? ? ? ? string headTag = Encoding.UTF8.GetString(headBuff);
? ? ? ? ? ? ? ? ? ? if(headTag == AESHead) {
? ? ? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[fs.Length - headBuff.Length];
? ? ? ? ? ? ? ? ? ? ? ? fs.Read(buffer,0,Convert.ToInt32(fs.Length - headBuff.Length));
? ? ? ? ? ? ? ? ? ? ? ? fs.Seek(0,SeekOrigin.Begin);
? ? ? ? ? ? ? ? ? ? ? ? fs.SetLength(0);
? ? ? ? ? ? ? ? ? ? ? ? byte[] DecBuffer = AESDecrypt(buffer);
? ? ? ? ? ? ? ? ? ? ? ? fs.Write(DecBuffer,0,DecBuffer.Length);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? Debug.LogError(e);
? ? ? ? }
? ? }

? ? /// <summary>
? ? /// 文件界面,传入文件路径,返回字节
? ? /// </summary>
? ? /// <returns></returns>
? ? private static byte[] AESFileByteDecrypt(string path) {
? ? ? ? if(!File.Exists(path)) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? byte[] DecBuffer = null;
? ? ? ? try {
? ? ? ? ? ? using(FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read)) {
? ? ? ? ? ? ? ? if(fs != null) {
? ? ? ? ? ? ? ? ? ? byte[] headBuff = new byte[10];
? ? ? ? ? ? ? ? ? ? fs.Read(headBuff,0,headBuff.Length);
? ? ? ? ? ? ? ? ? ? string headTag = Encoding.UTF8.GetString(headBuff);
? ? ? ? ? ? ? ? ? ? if(headTag == AESHead) {
? ? ? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[fs.Length - headBuff.Length];
? ? ? ? ? ? ? ? ? ? ? ? fs.Read(buffer,0,Convert.ToInt32(fs.Length - headBuff.Length));
? ? ? ? ? ? ? ? ? ? ? ? DecBuffer = AESDecrypt(buffer);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? Debug.LogError(e);
? ? ? ? }

? ? ? ? return DecBuffer;
? ? }
? ? #endregion

? ? #region AES加、解密
? ? private static string encryptKey = "QIYUAN";//密钥

? ? /// <summary>
? ? /// 根据路径加密
? ? /// </summary>
? ? /// <param name="path"></param>
? ? /// <returns></returns>
? ? public static void AESEncryptByPath(string path) {
? ? ? ? if(!File.Exists(path))
? ? ? ? ? ? return;
? ? ? ? try {
? ? ? ? ? ? using(FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite)) {
? ? ? ? ? ? ? ? if(fs != null) {
? ? ? ? ? ? ? ? ? ? //加密
? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[fs.Length];
? ? ? ? ? ? ? ? ? ? fs.Read(buffer,0,Convert.ToInt32(fs.Length));
? ? ? ? ? ? ? ? ? ? fs.Seek(0,SeekOrigin.Begin);
? ? ? ? ? ? ? ? ? ? fs.SetLength(0);
? ? ? ? ? ? ? ? ? ? byte[] EncBuffer = AESEncrypt(buffer);
? ? ? ? ? ? ? ? ? ? fs.Write(EncBuffer,0,EncBuffer.Length);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? Debug.LogError(e);
? ? ? ? }
? ? }


? ? /// <summary>
? ? /// 字符串加密 ?返回加密后的字符串
? ? /// </summary>
? ? /// <param name="EncryptString"></param>
? ? /// <returns></returns>
? ? public static string AESEncryptToString(string EncryptString) {
? ? ? ? return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString)));
? ? }

? ? /// <summary>
? ? /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
? ? /// </summary>
? ? /// <param name="EncryptString">待加密密文</param>
? ? public static byte[] AESEncrypt(byte[] EncryptByte) {
? ? ? ? if(EncryptByte.Length == 0) { throw (new Exception("明文不得为空")); }
? ? ? ? if(string.IsNullOrEmpty(encryptKey)) { throw (new Exception("密钥不得为空")); }
? ? ? ? byte[] m_strEncrypt;
? ? ? ? byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
? ? ? ? byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
? ? ? ? Rijndael m_AESProvider = Rijndael.Create();
? ? ? ? try {
? ? ? ? ? ? MemoryStream m_stream = new MemoryStream();
? ? ? ? ? ? PasswordDeriveBytes pdb = new PasswordDeriveBytes(encryptKey,m_salt);
? ? ? ? ? ? ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32),m_btIV);
? ? ? ? ? ? CryptoStream m_csstream = new CryptoStream(m_stream,transform,CryptoStreamMode.Write);
? ? ? ? ? ? m_csstream.Write(EncryptByte,0,EncryptByte.Length);
? ? ? ? ? ? m_csstream.FlushFinalBlock();
? ? ? ? ? ? m_strEncrypt = m_stream.ToArray();
? ? ? ? ? ? m_stream.Close();
? ? ? ? ? ? m_stream.Dispose();
? ? ? ? ? ? m_csstream.Close();
? ? ? ? ? ? m_csstream.Dispose();
? ? ? ? } catch(IOException ex) { throw ex; } catch(CryptographicException ex) { throw ex; } catch(ArgumentException ex) { throw ex; } catch(Exception ex) { throw ex; } finally { m_AESProvider.Clear(); }
? ? ? ? return m_strEncrypt;
? ? }

? ? /// <summary>
? ? /// ?根据路径解密 返回解密后的Byte
? ? /// </summary>
? ? /// <returns></returns>
? ? public static byte[] AESDecryptToByte(string path) {
? ? ? ? if(!File.Exists(path)) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? byte[] DecBuffer = null;
? ? ? ? try {
? ? ? ? ? ? using(FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read)) {
? ? ? ? ? ? ? ? if(fs != null) {
? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[fs.Length];
? ? ? ? ? ? ? ? ? ? fs.Read(buffer,0,Convert.ToInt32(fs.Length));
? ? ? ? ? ? ? ? ? ? DecBuffer = AESDecrypt(buffer);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? ? ? Debug.LogError(e);
? ? ? ? }

? ? ? ? return DecBuffer;
? ? }

? ? /// <summary>
? ? /// 字符串解密 ?返回解密后的字符串
? ? /// </summary>
? ? /// <param name="DecryptString"></param>
? ? /// <returns></returns>
? ? public static string AESDecryptToString(string DecryptString) {
? ? ? ? return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString)));
? ? }

? ? /// <summary>
? ? /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
? ? /// </summary>
? ? /// <param name="DecryptString">待解密密文</param>
? ? public static byte[] AESDecrypt(byte[] DecryptByte) {
? ? ? ? if(DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); }
? ? ? ? if(string.IsNullOrEmpty(encryptKey)) { throw (new Exception("密钥不得为空")); }
? ? ? ? byte[] m_strDecrypt;
? ? ? ? byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
? ? ? ? byte[] m_salt = Convert.FromBase64String("gsf4jvkyhye5/d7k8OrLgM==");
? ? ? ? Rijndael m_AESProvider = Rijndael.Create();
? ? ? ? try {
? ? ? ? ? ? MemoryStream m_stream = new MemoryStream();
? ? ? ? ? ? PasswordDeriveBytes pdb = new PasswordDeriveBytes(encryptKey,m_salt);
? ? ? ? ? ? ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(32),m_btIV);
? ? ? ? ? ? CryptoStream m_csstream = new CryptoStream(m_stream,transform,CryptoStreamMode.Write);
? ? ? ? ? ? m_csstream.Write(DecryptByte,0,DecryptByte.Length);
? ? ? ? ? ? m_csstream.FlushFinalBlock();
? ? ? ? ? ? m_strDecrypt = m_stream.ToArray();
? ? ? ? ? ? m_stream.Close();
? ? ? ? ? ? m_stream.Dispose();
? ? ? ? ? ? m_csstream.Close();
? ? ? ? ? ? m_csstream.Dispose();
? ? ? ? } catch(IOException ex) { throw ex; } catch(CryptographicException ex) { throw ex; } catch(ArgumentException ex) { throw ex; } catch(Exception ex) { throw ex; } finally { m_AESProvider.Clear(); }
? ? ? ? return m_strDecrypt;
? ? }

? ? #endregion
}

/// <summary>功能:实现单例</summary>
public class Singleton<T> where T : class, new() {
? ? private static T instance;
? ? public static T Instance {
? ? ? ? get {
? ? ? ? ? ? if(instance == null) {
? ? ? ? ? ? ? ? new T();
? ? ? ? ? ? }
? ? ? ? ? ? return instance;
? ? ? ? }
? ? }

? ? public Singleton() {
? ? ? ? instance = this as T;
? ? }
}
?

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-20 15:26:37  更:2021-08-20 15:26:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 23:18:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码