Esempio n. 1
0
        public static byte[] generate_key_secret(byte[] my_private, byte[] another_public)
        {
            if (my_private == null || my_private.Length != DH_KEY_LENGTH)
            {
                return(null);
            }
            if (another_public == null || another_public.Length != DH_KEY_LENGTH)
            {
                return(null);
            }

            UInt128 private_k = new UInt128(my_private);
            UInt128 another_k = new UInt128(another_public);

            UInt128 secret_k = UInt128.PowModP(another_k, private_k);

            byte[] secret_key = new byte[DH_KEY_LENGTH];
            secret_k.ConvertToBytesArray(secret_key);
            return(secret_key);
        }
Esempio n. 2
0
        public static void generate_key_pair(byte[] public_key, byte[] private_key)
        {
            if (public_key == null || public_key.Length != DH_KEY_LENGTH)
            {
                return;
            }
            if (private_key == null || private_key.Length != DH_KEY_LENGTH)
            {
                return;
            }

            Random rand = new Random();

            for (int i = 0; i < DH_KEY_LENGTH; i++)
            {
                private_key[i] = (byte)(rand.Next() & 0xFF);
            }

            UInt128 private_k = new UInt128(private_key);
            UInt128 public_k  = UInt128.PowModP(G, private_k);

            public_k.ConvertToBytesArray(public_key);
        }