void CreatePublicKey() { Diff = new DiffieHellman(); khoapublic = Diff.PublicKey; keypublic = Convert.ToBase64String(khoapublic); txtPublicKey.Text = keypublic; }
static public byte SendMessage(String login, String message) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(ipPoint); BinaryWriter bw = new BinaryWriter(new NetworkStream(socket), Encoding.UTF8); bw.Write(MESSAGE_CODE); bw.Write(login); BinaryReader br = new BinaryReader(new NetworkStream(socket)); BigInteger n = BigInteger.Parse(br.ReadString()); BigInteger q = BigInteger.Parse(br.ReadString()); BigInteger y = DiffieHellman.RandomPrimeBigInteger(n); BigInteger K = BigInteger.ModPow(q, y, n); bw.Write(K.ToString()); BigInteger M = BigInteger.Parse(br.ReadString()); BigInteger key = BigInteger.ModPow(M, y, n); String encryptedMessage = DiffieHellman.encryptRS4(key.ToString(), message); bw.Write(encryptedMessage); byte[] answer = new byte[1]; socket.Receive(answer); socket.Shutdown(SocketShutdown.Both); socket.Close(); return(answer[0]); } catch (Exception ex) { return(MESSAGE_FAILED_CODE); } }
void TaoKey() { Diff = new DiffieHellman(); khoapublic = Diff.PublicKey; keypublic = Convert.ToBase64String(khoapublic); textBox3.Text = keypublic; }
public static BigInteger RandomPrimeBigInteger(BigInteger maxValue) { RandomNumberGenerator rng = RandomNumberGenerator.Create(); byte[] bytes = new byte[maxValue.ToByteArray().LongLength]; BigInteger result; do { do { rng.GetBytes(bytes); result = BigInteger.Abs(new BigInteger(bytes)); } while (result > maxValue); } while (!DiffieHellman.IsProbablePrime(result, MILLER_RABIN_CERTAINTY)); return(result); }