Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
in .net, classes for working with cryptography are in the System.Security.Cryptography namespace
/// /// Шифрует строку value
///
/// Строка которую необходимо зашифровать
/// Ключ шифрования
public static string Encrypt(string str, string keyCrypt)
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(str), keyCrypt));
}
/// /// Расшифроывает данные из строки value
///
/// Зашифрованая строка
/// Ключ шифрования
/// Возвращает null, если прочесть данные не удалось
[DebuggerNonUserCodeAttribute]
public static string Decrypt(string str, string keyCrypt)
{
string Result;
try
{
CryptoStream Cs = InternalDecrypt(Convert.FromBase64String(str), keyCrypt);
StreamReader Sr = new StreamReader(Cs);
Result = Sr.ReadToEnd();
Cs.Close();
Cs.Dispose();
Sr.Close();
Sr.Dispose();
}
catch (CryptographicException)
{
return null;
}
return Result;
}
private static byte[] Encrypt(byte[] key, string value)
{
SymmetricAlgorithm Sa = Rijndael.Create();
ICryptoTransform Ct = Sa.CreateEncryptor((new PasswordDeriveBytes(value, null)).GetBytes(16), new byte[16]);
MemoryStream Ms = new MemoryStream();
CryptoStream Cs = new CryptoStream(Ms, Ct, CryptoStreamMode.Write);
Cs.Write(key, 0, key.Length);
Cs.FlushFinalBlock();
byte[] Result = Ms.ToArray();
Ms.Close();
Ms.Dispose();
Cs.Close();
Cs.Dispose();
Ct.Dispose();
return Result;
}
private static CryptoStream InternalDecrypt(byte[] key, string value)
{
SymmetricAlgorithm sa = Rijndael.Create();
ICryptoTransform ct = sa.CreateDecryptor((new PasswordDeriveBytes(value, null)).GetBytes(16), new byte[16]);
MemoryStream ms = new MemoryStream(key);
return new CryptoStream(ms, ct, CryptoStreamMode.Read);
}
Working code from a real project.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question