上篇介绍了AES算法,本篇介绍类似原理的另外一种对称算法。关于该算法的详解,请移看他人博客https://blog.csdn.net/z784561257/article/details/78174106
DES算法封装实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Security.Cryptography;
using System.Text;
using System.IO;
/*
* Autohr:W
* Des算法加密工具
*/
namespace W.GameFramework.HotUpdate
{
public class DesEncryptHelper : EncryptionHelper
{
/// <summary>
/// 文件解密
/// </summary>
/// <param name="encryptedFile"></param>
/// <param name="key"></param>
/// <returns></returns>
public byte[] DeEncrypthFile(byte[] encryptedFile, string key)
{
byte[] originalFile = null;
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = keyBytes;
provider.IV = keyBytes;
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,provider.CreateDecryptor(),CryptoStreamMode.Write);
cryptoStream.Write(encryptedFile, 0, encryptedFile.Length);
cryptoStream.FlushFinalBlock();
originalFile = memoryStream.ToArray();
return originalFile;
}
/// <summary>
/// 文件加密
/// </summary>
/// <param name="targetFile"></param>
/// <param name="key"></param>
/// <returns></returns>
public byte[] EncryptFile(byte[] targetFile, string key)
{
byte[] encryptedFile = null;
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = keyBytes;
provider.IV = keyBytes;
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,provider.CreateEncryptor(),CryptoStreamMode.Write);
cryptoStream.Write(targetFile, 0, targetFile.Length);
cryptoStream.FlushFinalBlock();
encryptedFile = memoryStream.ToArray();
return encryptedFile;
}
}
}
|