예제 #1
0
    private void Start()
    {
        byte[] seed = System.Text.Encoding.ASCII.GetBytes("38pyb2p8yn2ybauynf874t");
        byte[] remote_public_key;
        byte[] public_key;
        byte[] key;

        DH.RandSeed(seed);

        DH.New();

        DH.GenerateKey(out remote_public_key);

        DH.Free();

        DH.New();

        DH.GenerateKey(out public_key);
        DH.ComputeKey(out key, remote_public_key);

        DH.Free();

        Debug.Log($"remote_public_key : {BitConverter.ToString(remote_public_key)}");
        Debug.Log($"public_key : {BitConverter.ToString(public_key)}");
        Debug.Log($"key : {BitConverter.ToString(key)}");
    }
        public void HandleClientKeyPacket(string publicKey, ref GameCryptography crypto)
        {
            var key = _keyExchange.ComputeKey(BigNumber.FromHexString(publicKey));

            crypto.SetKey(key);
            crypto.SetIvs(_clientIV, _serverIV);
        }
예제 #3
0
        public void Execute(string[] args)
        {
            OpenSSL.Core.Random.Seed(rnd_seed);

            BigNumber.GeneratorHandler cb = new BigNumber.GeneratorHandler(this.OnStatus);
            DH a = new DH(64, DH.Generator5, cb, Console.Out);

            DH.CheckCode check = a.Check();
            if ((check & DH.CheckCode.CheckP_NotPrime) != 0)
            {
                Console.WriteLine("p value is not prime");
            }
            if ((check & DH.CheckCode.CheckP_NotSafePrime) != 0)
            {
                Console.WriteLine("p value is not safe prime");
            }
            if ((check & DH.CheckCode.UnableToCheckGenerator) != 0)
            {
                Console.WriteLine("unable to check the generator value");
            }
            if ((check & DH.CheckCode.NotSuitableGenerator) != 0)
            {
                Console.WriteLine("the g value is not a generator");
            }

            Console.WriteLine();
            Console.WriteLine("p    ={0}", a.P);
            Console.WriteLine("g    ={0}", a.G);

            DH b = new DH(a.P, a.G);

            a.NoExpConstantTime = false;
            b.NoExpConstantTime = true;

            a.GenerateKeys();
            Console.WriteLine("pri 1={0}", a.PrivateKey);
            Console.WriteLine("pub 1={0}", a.PublicKey);

            b.GenerateKeys();
            Console.WriteLine("pri 2={0}", b.PrivateKey);
            Console.WriteLine("pub 2={0}", b.PublicKey);

            byte[] aout = a.ComputeKey(b.PublicKey);
            string astr = BitConverter.ToString(aout);

            Console.WriteLine("key1 ={0}", astr);

            byte[] bout = b.ComputeKey(a.PublicKey);
            string bstr = BitConverter.ToString(bout);

            Console.WriteLine("key2 ={0}", bstr);

            if (aout.Length < 4 || astr != bstr)
            {
                throw new Exception("Error in DH routines");
            }

            a.Dispose();
            b.Dispose();
        }
예제 #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] lpPublicKey       = bytBase64_decode(richTextBox3.Text);                //GSYCGAATBase64Decode(&lpPublicKey, m_lpPublicKeyString, m_dwPublicKeyStringLength)
            byte[] lpEncryptedSecret = bytBase64_decode(richTextBox4.Text);                //GSYCGAATBase64Decode(&lpEncryptedSecret, m_strEncryptedSecretString, iLength)
            byte[] lpPrivatekey      = CS_DH.ComputeKey(BigNumber.FromArray(lpPublicKey)); //lpPublicKeyBN = BN_bin2bn(lpPublicKey, iPublicKeyLength, NULL) + DH_compute_key(lpPrivatekey, lpPublicKeyBN, pDH)

            SHA256 sha256 = new SHA256CryptoServiceProvider();

            byte[] bytSecureHashAlgorithmCode = sha256.ComputeHash(lpPrivatekey);       //GSYCGAATSHA256(bytSecureHashAlgorithmCode, lpPrivatekey, iPrivatekeyLength);
            byte[] bytAESKey = new byte[16];
            Array.Copy(bytSecureHashAlgorithmCode, 16, bytAESKey, 0, bytAESKey.Length); //memcpy(bytAESKey, &(bytSecureHashAlgorithmCode[GSYCGAAT_SHA256_CODE_LENGTH / 2]), GSYCGAAT_AES_KEY_LENGTH);

            SyrisAES.KeySize keysize;
            keysize = SyrisAES.KeySize.Bits128;
            Array.Copy(bytAESKey, 0, SyrisAES.AESKey, 0, SyrisAES.AESKey.Length);
            SyrisAES a = new SyrisAES(keysize);

            byte[] outputByteArray = new byte[16];
            a.UnAES(lpEncryptedSecret, outputByteArray);      //GSYCGAATAES128ECBDecrypt((*lpSecret), lpSecretCipher, GSYCGAAT_AES_UNIT_LENGTH, bytAESKey)
            richTextBox5.Text = ToHexString(outputByteArray); //GSYCGAATBinaryToHEXString(strSecretCodeString, m_bytSecretCode, GSYCGAAT_SECRET_LENGTH);
        }