PasswordDeriveBytes. Где хранить пароль и соль ключа? - C#
Формулировка задачи:
Из справки по данному класу:
Примечание о безопасности Примечание по безопасности
Не следует жестко кодировать пароль в исходный код. Жестко запрограммированные пароли можно извлечь из сборки с помощью Ildasm.exe (дизассемблер IL) средства, или шестнадцатеричного редактора, либо открыв сборку в текстовом редакторе, например notepad.exe.
Подскажите как лучше реализовать хранение пароля и соли ключа. В защите информации я не силен.
Решение задачи: «PasswordDeriveBytes. Где хранить пароль и соль ключа?»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace Shifr
- {
- class Program
- {
- //метод шифрования строки
- public static string Shifrovka(string ishText, string pass,
- string sol = "doberman", string cryptographicAlgorithm = "SHA1",
- int passIter = 2, string initVec = "a8doSuDitOz1hZe#",
- int keySize = 256)
- {
- if (string.IsNullOrEmpty(ishText))
- return "";
- byte[] initVecB = Encoding.ASCII.GetBytes(initVec);
- byte[] solB = Encoding.ASCII.GetBytes(sol);
- byte[] ishTextB = Encoding.UTF8.GetBytes(ishText);
- PasswordDeriveBytes derivPass = new PasswordDeriveBytes(pass, solB, cryptographicAlgorithm, passIter);
- byte[] keyBytes = derivPass.GetBytes(keySize / 8);
- RijndaelManaged symmK = new RijndaelManaged();
- symmK.Mode = CipherMode.CBC;
- byte[] cipherTextBytes = null;
- using (ICryptoTransform encryptor = symmK.CreateEncryptor(keyBytes, initVecB))
- {
- using (MemoryStream memStream = new MemoryStream())
- {
- using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
- {
- cryptoStream.Write(ishTextB, 0, ishTextB.Length);
- cryptoStream.FlushFinalBlock();
- cipherTextBytes = memStream.ToArray();
- memStream.Close();
- cryptoStream.Close();
- }
- }
- }
- symmK.Clear();
- return Convert.ToBase64String(cipherTextBytes);
- }
- //метод дешифрования строки
- public static string DeShifrovka(string ciphText, string pass,
- string sol = "doberman", string cryptographicAlgorithm = "SHA1",
- int passIter = 2, string initVec = "a8doSuDitOz1hZe#",
- int keySize = 256)
- {
- if (string.IsNullOrEmpty(ciphText))
- return "";
- byte[] initVecB = Encoding.ASCII.GetBytes(initVec);
- byte[] solB = Encoding.ASCII.GetBytes(sol);
- byte[] cipherTextBytes = Convert.FromBase64String(ciphText);
- PasswordDeriveBytes derivPass = new PasswordDeriveBytes(pass, solB, cryptographicAlgorithm, passIter);
- byte[] keyBytes = derivPass.GetBytes(keySize / 8);
- RijndaelManaged symmK = new RijndaelManaged();
- symmK.Mode = CipherMode.CBC;
- byte[] plainTextBytes = new byte[cipherTextBytes.Length];
- int byteCount = 0;
- using (ICryptoTransform decryptor = symmK.CreateDecryptor(keyBytes, initVecB))
- {
- using (MemoryStream mSt = new MemoryStream(cipherTextBytes))
- {
- using (CryptoStream cryptoStream = new CryptoStream(mSt, decryptor, CryptoStreamMode.Read))
- {
- byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
- mSt.Close();
- cryptoStream.Close();
- }
- }
- }
- symmK.Clear();
- return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
- }
- static void Main(string[] args)
- {
- String gg = Shifrovka("Маша, я тебя люблю!", "пароль");
- String gg2 = DeShifrovka(gg, "пароль");
- Console.WriteLine(gg);
- Console.WriteLine(gg2);
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д