private void KeyExchange(ref Byte encryptMode) { while (keyExchanging) { Thread.Sleep(1); } if (key != null || encryptMode == 0) { return; } if ((key == null) && keyExchanged) { encryptMode = 0; return; } keyExchanging = true; Hashtable result = POST("phprpc_encrypt=true&phprpc_keylen=" + keylen); if (result.ContainsKey("phprpc_keylen")) { keylen = (UInt32)result["phprpc_keylen"]; } else { keylen = 128; } if (result.ContainsKey("phprpc_encrypt")) { AssocArray encrypt = (AssocArray)Deserialize((Byte[])result["phprpc_encrypt"]); BigInteger x = BigInteger.GenerateRandom((Int32)keylen - 1); x.SetBit(keylen - 2); BigInteger y = BigInteger.Parse(PHPConvert.ToString(encrypt["y"])); BigInteger p = BigInteger.Parse(PHPConvert.ToString(encrypt["p"])); BigInteger g = BigInteger.Parse(PHPConvert.ToString(encrypt["g"])); if (keylen == 128) { this.key = new byte[16]; Byte[] k = y.ModPow(x, p).GetBytes(); for (Int32 i = 1, n = Math.Min(k.Length, 16); i <= n; i++) { this.key[16 - i] = k[n - i]; } } else { key = MD5.Hash(encoding.GetBytes(y.ModPow(x, p).ToString())); } POST("phprpc_encrypt=" + g.ModPow(x, p).ToString()); } else { key = null; keyExchanged = true; encryptMode = 0; } keyExchanging = false; }
public BigInteger GetP() { return(BigInteger.Parse(PHPConvert.ToString(dhParams["p"]))); }
private void NextKeyExchange(Object state) { RequestState requestState = state as RequestState; Hashtable result = GetResponseBody(requestState.response, 0); #if SILVERLIGHT if (result.ContainsKey("phprpc_url")) { String phprpc_url = (String)result["phprpc_url"]; url = new Uri(phprpc_url); if (url.Query == "") { url = new Uri(phprpc_url + "?phprpc_id=" + clientID); } else { url = new Uri(phprpc_url + "&phprpc_id=" + clientID); } } #endif if (result.ContainsKey("phprpc_keylen")) { keylen = (UInt32)result["phprpc_keylen"]; } else { keylen = 128; } if (result.ContainsKey("phprpc_encrypt")) { AssocArray encrypt = (AssocArray)Deserialize((Byte[])result["phprpc_encrypt"]); BigInteger x = BigInteger.GenerateRandom((Int32)keylen - 1); x.SetBit(keylen - 2); BigInteger y = BigInteger.Parse(PHPConvert.ToString(encrypt["y"])); BigInteger p = BigInteger.Parse(PHPConvert.ToString(encrypt["p"])); BigInteger g = BigInteger.Parse(PHPConvert.ToString(encrypt["g"])); if (keylen == 128) { this.key = new byte[16]; Byte[] k = y.ModPow(x, p).GetBytes(); for (Int32 i = 1, n = Math.Min(k.Length, 16); i <= n; i++) { this.key[16 - i] = k[n - i]; } } else { key = MD5.Hash(encoding.GetBytes(y.ModPow(x, p).ToString())); } requestState = new RequestState(); requestState.bufferWrite = encoding.GetBytes("phprpc_encrypt=" + g.ModPow(x, p).ToString()); requestState.asyncCallback = new SendOrPostCallback(EndKeyExchange); POST(requestState); } else { key = null; requestState.encryptMode = 0; keyExchanged = true; EndKeyExchange(null); } }