| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Security.Cryptography; |
| | | 5 | | using System.IO; |
| | | 6 | | using DocumentFormat.OpenXml.Office.CustomUI; |
| | | 7 | | using System.Linq; |
| | | 8 | | |
| | | 9 | | namespace SVETA.Api.Helpers |
| | | 10 | | { |
| | | 11 | | public static class SymmetricCrypto |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Основной ключ шифрования |
| | | 15 | | /// </summary> |
| | 31 | 16 | | public static string ProtectKey { get; set; } |
| | | 17 | | |
| | | 18 | | private static SymmetricAlgorithm CreateAlgorithm() |
| | 3 | 19 | | { |
| | 3 | 20 | | if (string.IsNullOrWhiteSpace(ProtectKey)) |
| | 0 | 21 | | throw new ArgumentException("Ключ шифрования не может быть пустым"); |
| | 3 | 22 | | if (ProtectKey.Length != 16) |
| | 0 | 23 | | throw new ArgumentException("Длина ключа должна составлять 16 символов"); |
| | | 24 | | //юзаем симметричный алгоритм Rijndael. Длина ключа 128,192,256. Шаг 64 бита. Шаг по идее можно сменить чере |
| | 3 | 25 | | SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create("Rijndael"); |
| | 3 | 26 | | algorithm.Key = Encoding.UTF8.GetBytes(ProtectKey); |
| | 3 | 27 | | return algorithm; |
| | 3 | 28 | | } |
| | | 29 | | |
| | | 30 | | public static byte[] EncryptData(string data) |
| | 2 | 31 | | { |
| | 2 | 32 | | var algorithm = CreateAlgorithm(); |
| | 2 | 33 | | byte[] byteData = Encoding.UTF8.GetBytes(data); |
| | 2 | 34 | | MemoryStream tagetStream = new MemoryStream(); |
| | | 35 | | |
| | | 36 | | // Генерируем случайный вектор инициализации (IV) |
| | | 37 | | // для использования с алгоритмом. Добавляется к общему массиву , дабы массив не был все время одинааковым, |
| | 2 | 38 | | algorithm.GenerateIV(); |
| | 2 | 39 | | tagetStream.Write(algorithm.IV, 0, algorithm.IV.Length); |
| | | 40 | | |
| | | 41 | | // Шифруем данные. В итоге пишем данные в tagetStream, пропуская все через CryptoStream |
| | 2 | 42 | | CryptoStream cs = new CryptoStream(tagetStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write); |
| | 2 | 43 | | cs.Write(byteData, 0, byteData.Length); |
| | 2 | 44 | | cs.FlushFinalBlock(); |
| | | 45 | | |
| | 2 | 46 | | return tagetStream.ToArray(); |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | public static string DecryptData(byte[] data) |
| | 1 | 50 | | { |
| | 1 | 51 | | var algorithm = CreateAlgorithm(); |
| | 1 | 52 | | MemoryStream tagetStream = new MemoryStream(); |
| | | 53 | | |
| | | 54 | | // Читаем вектор инициализации (IV) |
| | 1 | 55 | | int ReadPos = 0; |
| | 1 | 56 | | byte[] IV = new byte[algorithm.IV.Length]; |
| | 1 | 57 | | Array.Copy(data, IV, IV.Length); |
| | 1 | 58 | | algorithm.IV = IV; |
| | 1 | 59 | | ReadPos += algorithm.IV.Length; |
| | | 60 | | |
| | 1 | 61 | | CryptoStream cs = new CryptoStream(tagetStream, algorithm.CreateDecryptor(), CryptoStreamMode.Write); |
| | 1 | 62 | | cs.Write(data, ReadPos, data.Length - ReadPos); |
| | 1 | 63 | | cs.FlushFinalBlock(); |
| | | 64 | | |
| | 1 | 65 | | return Encoding.UTF8.GetString(tagetStream.ToArray()); |
| | 1 | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |