1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| using System.Security.Cryptography; using System.Text;
public class AES { private static string AESHead = "AESEncrypt";
public static void AESFileEncrypt(string path, string EncryptKey) { 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, 10); string headTag = Encoding.UTF8.GetString(headBuff); if (headTag == AESHead) { #if UNITY_EDITOR Debug.Log(path + "已经加密过了!"); #endif 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, 10); byte[] EncBuffer = AESEncrypt(buffer, EncryptKey); fs.Write(EncBuffer, 0, EncBuffer.Length); } } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); } }
public static void AESFileDecrypt(string path, string EncryptKey) { 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[] EncBuffer = AESDecrypt(buffer, EncryptKey); fs.Write(EncBuffer, 0, EncBuffer.Length); } } } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); } }
public static byte[] AESFileByteDecrypt(string path, string EncryptKey) { if (!File.Exists(path)) return null;
byte[] EncBuffer = 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)); EncBuffer = AESDecrypt(buffer, EncryptKey); } } } } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); }
return EncBuffer; }
public static string AESEncrypt(string EncryptString, string EncryptKey) { return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey)); }
public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey) { 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; }
public static string AESDecrypt(string DecryptString, string DecryptKey) { return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey)); }
public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey) { if (DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(DecryptKey)) { 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(DecryptKey, 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; } }
|