Unity C# 大文件秒加密秒解密

using System.IO;
using System.Text;
using System.Security.Cryptography;
public class Encrypt
{
    /// <summary>
    /// 加密
    /// </summary>
    public static void Encryption(string key, string readfile)
    {
        StartCrypto(HashKey(key), readfile, false);
    }
    /// <summary>
    /// 解密
    /// </summary>
    public static void Decryption(string key, string readfile)
    {
        StartCrypto(HashKey(key), readfile, true);
    }
    /// <summary>
    /// 开始使用密码
    /// </summary>
    static void StartCrypto(int key, string readfile, bool recovery)
    {
        if (string.IsNullOrEmpty(readfile) == false)
        {
            CryptoStart(key, readfile, recovery);
        }
    }
    static void CryptoStart(int key, string readfile, bool recovery)
    {
        if (File.Exists(readfile) == false)
        {
            return;
        }
        //
        var size = 512;
        var buf1 = new byte[size];
        using (var fsmr = new FileStream(readfile, FileMode.Open))
            fsmr.Read(buf1, 0, size);
        //
        var mpeg = true;
        var ftyp = new char[4] { 'f', 't', 'y', 'p' };
        for (var j = 0; j < ftyp.Length; j++)
        {
            if (ftyp[j] != buf1[j + 4])
            {
                mpeg = false;
                break;
            }
        }
        //解密
        if (recovery == true && mpeg == false)
        {
            var buf2 = HaskByte(buf1, -key);
            using (var fsmw = new FileStream(readfile, FileMode.Open, FileAccess.Write, FileShare.Read))
            {
                fsmw.Write(buf2, 0, size);
            }
            return;
        }
        //加密
        if (recovery == false && mpeg == true)
        {
            var buf2 = HaskByte(buf1, key);
            using (var fsmw = new FileStream(readfile, FileMode.Open, FileAccess.Write, FileShare.Read))
            {
                fsmw.Write(buf2, 0, size);
            }
            return;
        }
    }
    static int HashKey(string key)
    {
        var tmp = 0;
        var md5 = new MD5CryptoServiceProvider();
        var bit = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
        for (var i = 0; i < bit.Length; i++)
        {
            tmp += bit[i];
        }
        return tmp;
    }
    static byte[] HaskByte(byte[] bytes, int key)
    {
        var i = 0;
        var c = bytes.Length;
        var temp = new byte[4];
        var data = new byte[c];
        for (i = 3; i < c; i += 4)
        {
            temp[0] = bytes[i - 3];
            temp[1] = bytes[i - 2];
            temp[2] = bytes[i - 1];
            temp[3] = bytes[i];
            var ints = byteToInt(temp);
            var bits = intToByte(ints + key);
            data[i - 3] = bits[0];
            data[i - 2] = bits[1];
            data[i - 1] = bits[2];
            data[i] = bits[3];
        }
        for (var j = i; j < c; j++)
        {
            data[j] = bytes[j];
        }
        return data;
    }
    static byte[] intToByte(int i)
    {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }
    static int byteToInt(byte[] bytes)
    {
        int value = 0;
        for (int i = 0; i < 4; i++)
        {
            int off = (3 - i) * 8;
            value += (bytes[i] & 0x000000FF) << off;
        }
        return value;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐教主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值