C#: Decryption und Encryption

Für die Verschlüsselung eines Passwortes brauchte ich diese 2 Methoden…

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
#region Encryption / Decryption
public string EncryptMessage(string plainMessage, string password)
{
	TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
	des.IV = new byte[8];
	PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
	des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
	MemoryStream ms = new MemoryStream(plainMessage.Length * 2);
	CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
	byte[] plainBytes = Encoding.UTF8.GetBytes(plainMessage);
	encStream.Write(plainBytes, 0, plainBytes.Length);
	encStream.FlushFinalBlock();
	byte[] encryptedBytes = new byte[ms.Length];
	ms.Position = 0;
	ms.Read(encryptedBytes, 0, (int)ms.Length);
	encStream.Close();
	return Convert.ToBase64String(encryptedBytes);
}
 
public string DecryptMessage(string encryptedBase64, string password)
{
	TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
	des.IV = new byte[8];
	PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
	des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
	byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);
	MemoryStream ms = new MemoryStream(encryptedBase64.Length);
	CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
	decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
	decStream.FlushFinalBlock();
	byte[] plainBytes = new byte[ms.Length];
	ms.Position = 0;
	ms.Read(plainBytes, 0, (int)ms.Length);
	decStream.Close();
	return Encoding.UTF8.GetString(plainBytes);
}
#endregion

Gefunden auf http://www.tutorials.de/forum/net-archiv/150182-verschluesselung-mit-c.html

leave your comment

*

Unterstütze den Frickelblog!