using System.Security.Cryptography;
public static void Main()
Console.WriteLine("Method1:" + WPAPskKey.getWpaPskKeyFromPassphrase(pwd,ssid));
Console.WriteLine("Method2:" + GetWpaMasterKey(pwd,ssid));
public static string GetWpaMasterKey(string pwd, string ssid)
byte[] key = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(pwd), Encoding.UTF8.GetBytes(ssid), 4096).GetBytes(32);
return BitConverter.ToString(key).Replace("-", string.Empty);
public static class WPAPskKey {
private static uint[] stringtowords(string s, uint padi)
char[] cs = s.ToCharArray();
for (var i = 0; i < 64; i++)
if (i == n) c = ((uint)padi >> 24) & 0xff;
else if (i == n + 1) c = (padi >> 16) & 0xff;
else if (i == n + 2) c = (padi >> 8) & 0xff;
else if (i == n + 3) c = (padi & 0xff);
else if (i == n + 4) c = 0x80;
if (padi > 0) z[15] = (uint)(8 * (64 + n + 4));
private static uint[] initsha(uint[] w, uint padbyte)
uint pw = (padbyte << 24) | (padbyte << 16) | (padbyte << 8) | padbyte;
for (int i = 0; i < 16; i++)
uint[] s = new uint[] { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
uint a = s[0], b = s[1], c = s[2], d = s[3], e = s[4];
for (var k = 16; k < 80; k++)
t = (uint)(w[k - 3] ^ w[k - 8] ^ w[k - 14] ^ w[k - 16]);
w[k] = (t << 1) | ((uint)t >> 31);
for (var k = 0; k < 20; k++)
t = (uint)(((a << 5) | ((uint)a >> 27)) + e + w[k] + 0x5A827999 + ((b & c) | ((~b) & d)));
c = (b << 30) | (b >> 2);
for (var k = 20; k < 40; k++)
t = (uint)(((a << 5) | ((uint)a >> 27)) + e + w[k] + 0x6ED9EBA1 + (b ^ c ^ d));
c = (b << 30) | ((uint)b >> 2);
for (var k = 40; k < 60; k++)
t = (uint)(((a << 5) | ((uint)a >> 27)) + e + w[k] + 0x8F1BBCDC + ((b & c) | (b & d) | (c & d)));
c = (b << 30) | ((uint)b >> 2);
for (var k = 60; k < 80; k++)
t = ((uint)((a << 5) | ((uint)a >> 27)) + e + w[k] + 0xCA62C1D6 + (b ^ c ^ d));
c = (b << 30) | ((uint)b >> 2);
s[0] = (s[0] + a) & 0xffffffff;
s[1] = (s[1] + b) & 0xffffffff;
s[2] = (s[2] + c) & 0xffffffff;
s[3] = (s[3] + d) & 0xffffffff;
s[4] = (s[4] + e) & 0xffffffff;
public static string getWpaPskKeyFromPassphrase(string pass, string salt)
var hmac_istate = initsha(stringtowords(pass, 0), 0x36);
var hmac_ostate = initsha(stringtowords(pass, 0), 0x5c);
uint[] u = new uint[] { 0, 0, 0, 0, 0 };
uint[] w = stringtowords(salt, i);
for (int j = 0; j < 2 * 4096; j++)
uint[] s = (j & 1) > 0 ? hmac_ostate : hmac_istate;
uint a = s[0], b = s[1], c = s[2], d = s[3], e = s[4];
for (var k = 16; k < 80; k++)
t = w[k - 3] ^ w[k - 8] ^ w[k - 14] ^ w[k - 16];
w[k] = (t << 1) | (t >> 31);
for (var k = 0; k < 20; k++)
t = ((a << 5) | (a >> 27)) + e + w[k] + 0x5A827999 + ((b & c) | ((~b) & d));
c = (b << 30) | (b >> 2);
for (var k = 20; k < 40; k++)
t = ((a << 5) | (a >> 27)) + e + w[k] + 0x6ED9EBA1 + (b ^ c ^ d);
c = (b << 30) | (b >> 2);
for (var k = 40; k < 60; k++)
t = ((a << 5) | (a >> 27)) + e + w[k] + 0x8F1BBCDC + ((b & c) | (b & d) | (c & d));
c = (b << 30) | (b >> 2);
for (var k = 60; k < 80; k++)
t = ((a << 5) | (a >> 27)) + e + w[k] + 0xCA62C1D6 + (b ^ c ^ d);
c = (b << 30) | (b >> 2);
w[0] = (s[0] + a) & 0xffffffff;
w[1] = (s[1] + b) & 0xffffffff;
w[2] = (s[2] + c) & 0xffffffff;
w[3] = (s[3] + d) & 0xffffffff;
w[4] = (s[4] + e) & 0xffffffff;
for (var k = 6; k < 15; k++) w[k] = 0;
for (var j = 0; j < 5; j++)
for (var k = 0; k < 8; k++)
var t = (u[j] >> (28 - 4 * k)) & 0x0f;
hash += (char)(t + (t < 10 ? 48 : 87));
return hash.Substring(0, 64);
public class Rfc2898DeriveBytes : DeriveBytes
const int BlockSize = 20;
readonly HMACSHA1 hmacsha1;
public Rfc2898DeriveBytes(string password, byte[] salt, int iterations)
: this(new UTF8Encoding(false).GetBytes(password), salt, iterations)
public Rfc2898DeriveBytes(byte[] password, byte[] salt, int iterations)
IterationCount = iterations;
hmacsha1 = new HMACSHA1(password);
return (byte[]) salt.Clone();
throw new ArgumentNullException("value");
salt = (byte[]) value.Clone();
static byte[] Int(uint i)
byte[] bytes = BitConverter.GetBytes(i);
byte[] buffer2 = new byte[] {bytes[3], bytes[2], bytes[1], bytes[0]};
if (!BitConverter.IsLittleEndian)
byte[] inputBuffer = Int(block);
hmacsha1.TransformBlock(salt, 0, salt.Length, salt, 0);
hmacsha1.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
byte[] hash = hmacsha1.Hash;
for (int i = 2; i <= iterations; i++)
hash = hmacsha1.ComputeHash(hash);
for (int j = 0; j < BlockSize; j++)
buffer3[j] = (byte) (buffer3[j] ^ hash[j]);
public override byte[] GetBytes(int bytesToGet)
throw new ArgumentOutOfRangeException("bytesToGet");
byte[] dst = new byte[bytesToGet];
int count = endIndex - startIndex;
Buffer.BlockCopy(buffer, startIndex, dst, 0, bytesToGet);
startIndex += bytesToGet;
Buffer.BlockCopy(buffer, startIndex, dst, 0, count);
startIndex = endIndex = 0;
while (dstOffset < bytesToGet)
byte[] src = DeriveKey();
int num3 = bytesToGet - dstOffset;
Buffer.BlockCopy(src, 0, dst, dstOffset, BlockSize);
Buffer.BlockCopy(src, 0, dst, dstOffset, num3);
Buffer.BlockCopy(src, num3, buffer, startIndex, BlockSize - num3);
endIndex += BlockSize - num3;
Array.Clear(buffer, 0, buffer.Length);
buffer = new byte[BlockSize];
startIndex = endIndex = 0;
public override void Reset()
public int IterationCount
throw new ArgumentOutOfRangeException("value");
iterations = (uint) value;