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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
class Program
{
static void Main(string[] args)
{
Program pr = new Program();
var test = pr.EncryptUsernamePassword("test");
var test2 = pr.DecryptUsernamePassword(test);
Console.ReadLine();
}
public string EncryptUsernamePassword(string clearText)
{
// TODO: Parameterize the Password, Salt, and Iterations. They should be encrypted with the machine key and stored in the registry
if (string.IsNullOrEmpty(clearText))
{
return clearText;
}
byte[] salt = {};
// NOTE: The keystring, salt, and iterations must be the same as what is used in the Demo java system.
PKCSKeyGenerator crypto = new PKCSKeyGenerator("MyPassword", salt, 20, 1);
ICryptoTransform cryptoTransform = crypto.Encryptor;
var cipherBytes = cryptoTransform.TransformFinalBlock(Encoding.UTF8.GetBytes(clearText), 0, clearText.Length);
return Convert.ToBase64String(cipherBytes);
}
public string DecryptUsernamePassword(string cipherText)
{
if (string.IsNullOrEmpty(cipherText))
{
return cipherText;
}
byte[] salt = {};
// NOTE: The keystring, salt, and iterations must be the same as what is used in the Demo java system.
PKCSKeyGenerator crypto = new PKCSKeyGenerator("MyPassword", salt, 20, 1);
ICryptoTransform cryptoTransform = crypto.Decryptor;
var cipherBytes = Convert.FromBase64String(cipherText);
var clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(clearBytes);
}
}
}
public class PKCSKeyGenerator
{
private byte[] key = new byte[8];
private byte[] iv = new byte[8];
private DESCryptoServiceProvider des = new DESCryptoServiceProvider();
public PKCSKeyGenerator()
{
}
public PKCSKeyGenerator(string keystring, byte[] salt, int iterationsMd5, int segments)
{
this.Generate(keystring, salt, iterationsMd5, segments);
}
public byte[] Key
{
get
{
return this.key;
}
}
public byte[] IV
{
get
{
return this.iv;
}
}
public ICryptoTransform Encryptor
{
get
{
return this.des.CreateEncryptor(this.key, this.iv);
}
}
public ICryptoTransform Decryptor
{
get
{
return des.CreateDecryptor(key, iv);
}
}
public ICryptoTransform Generate(string keystring, byte[] salt, int iterationsMd5, int segments)
{
// MD5 bytes
int hashLength = 16;
// to store contatenated Mi hashed results
byte[] keyMaterial = new byte[hashLength * segments];
// --- get secret password bytes ----
byte[] passwordBytes;
passwordBytes = Encoding.UTF8.GetBytes(keystring);
// --- contatenate salt and pswd bytes into fixed data array ---
byte[] data00 = new byte[passwordBytes.Length + salt.Length];
// copy the pswd bytes
Array.Copy(passwordBytes, data00, passwordBytes.Length);
// concatenate the salt bytes
Array.Copy(salt, 0, data00, passwordBytes.Length, salt.Length);
// ---- do multi-hashing and contatenate results D1, D2 ... into keymaterial bytes ----
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = null;
// fixed length initial hashtarget
byte[] hashtarget = new byte[hashLength + data00.Length];
for (int j = 0; j < segments; j++)
{
// ---- Now hash consecutively for iterationsMd5 times ------
if (j == 0)
{
// initialize
result = data00;
}
else
{
Array.Copy(result, hashtarget, result.Length);
Array.Copy(data00, 0, hashtarget, result.Length, data00.Length);
result = hashtarget;
}
for (int i = 0; i < iterationsMd5; i++)
{
result = md5.ComputeHash(result);
}
// contatenate to keymaterial
Array.Copy(result, 0, keyMaterial, j * hashLength, result.Length);
}
Array.Copy(keyMaterial, 0, this.key, 0, 8);
Array.Copy(keyMaterial, 8, this.iv, 0, 8);
return this.Encryptor;
} |
Partager