M
M
Meliborn2011-03-03 18:11:59
C++ / C#
Meliborn, 2011-03-03 18:11:59

C# and Data Encryption

It is necessary to encrypt text files with various encryption algorithms (DES, AES, Blowfish, etc.) Can anyone tell me which libraries to use and are there any standard ones? And their documentation.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
antonlustin, 2011-03-03
@antonlustin

in .net, classes for working with cryptography are in the System.Security.Cryptography namespace

B
Bartez, 2011-03-03
@Bartez

/// /// Шифрует строку 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.

D
dmomen, 2011-03-03
@dmomen

There is nothing.

R
Rafael Osipov, 2011-03-03
@Rafael

There is a Bouncy Castle library

D
Dmitry Sidorov, 2011-03-04
@Doomsday_nxt

msdn.microsoft.com/en-us/library/92f9ye3s.aspx

I
iocderei, 2016-11-11
@iocderei

Parsing and implementation of the DES algorithm is, for example, here (there is also RSA). It seems clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question