Files
BeWoAdmin/DBToolControls/EncryptionHelper.cs
2016-06-27 02:24:18 +02:00

92 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Security;
namespace DBToolControls {
public sealed class EncryptionHelper {
private EncryptionHelper() { }
private static UnicodeEncoding byteConverter = new UnicodeEncoding();
public static string EncryptString(string key, string stringToEncrypt) {
if (key.Length == 0) return stringToEncrypt;
byte[] dataToEncrypt = byteConverter.GetBytes(stringToEncrypt);
return ByteArrayToHexString(AESEncrypt(dataToEncrypt, CreateCryptoProvider(key)));
}
public static string DecryptString(string key, string encryptedData) {
if (key.Length == 0) return encryptedData;
byte[] decryptedData = AESDecrypt(HexStringToByteArray(encryptedData), CreateCryptoProvider(key));
return byteConverter.GetString(decryptedData);
}
public static SecureString DecryptStringAsSecureString(string key, string encryptedData) {
SecureString secStr = new SecureString();
if (key.Length == 0) {
foreach(char c in encryptedData)
secStr.AppendChar(c);
} else {
byte[] decryptedData = AESDecrypt(HexStringToByteArray(encryptedData), CreateCryptoProvider(key));
foreach (char c in byteConverter.GetString(decryptedData).ToCharArray())
secStr.AppendChar(c);
}
return secStr;
}
private static AesCryptoServiceProvider CreateCryptoProvider(String key) {
byte[] salt = {185, 209, 196, 142, 52, 143, 231, 113,
250, 70, 74, 119, 161, 120, 251, 7,
220, 254, 173, 80, 209, 217, 253, 8,
179, 134, 239, 176, 139, 20, 47, 116,
76, 253, 164, 195, 7, 97, 87, 245,
129, 183, 30, 70, 232, 203, 86, 117};
List<byte> keyAsByteList = new List<byte>(byteConverter.GetBytes(key.ToCharArray())).FindAll(b => b != 0);
byte[] sBytes = new byte[48];
for(int i=0; i<48; i++) {
sBytes[i] = keyAsByteList[i % keyAsByteList.Count];
}
byte[] returnIV = new byte[16];
for (int i = 0; i < 16; i++) {
returnIV[i] = (byte)(salt[i] ^ sBytes[i]);
}
byte[] returnKey = new byte[32];
for (int i = 16; i < 48; i++) {
returnKey[i-16] = (byte)(salt[i] ^ sBytes[i]);
}
AesCryptoServiceProvider returnValue = new AesCryptoServiceProvider();
returnValue.IV = returnIV;
returnValue.Key = returnKey;
return returnValue;
}
private static byte[] AESEncrypt(byte[] DataToEncrypt, AesCryptoServiceProvider aes) {
return aes.CreateEncryptor().TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
private static byte[] AESDecrypt(byte[] DataToDecrypt, AesCryptoServiceProvider aes) {
return aes.CreateDecryptor().TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
}
private static string ByteArrayToHexString(byte[] data) {
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
sb.Append(String.Format("{0:X2}", b));
return sb.ToString();
}
private static byte[] HexStringToByteArray(string data) {
if (data.Length % 2 == 1) throw new CryptographicException("Invalid data");
byte[] bytes = new byte[data.Length / 2];
for (int i = 0; i < data.Length; i += 2)
bytes[i/2] = Byte.Parse(data.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
return bytes;
}
}
}