Beispiel #1
0
        public void KeyVerify(string algorithm, HashAlgorithm hash)
        {
            MemoryStream      cache     = new MemoryStream();
            NetworkByteWriter nbr_cache = new NetworkByteWriter(cache);

            nbr_cache.WriteString(verify_v_c);
            nbr_cache.WriteString(verify_v_s);
            nbr_cache.WriteBlob(verify_i_c);
            nbr_cache.WriteBlob(verify_i_s);
            nbr_cache.WriteBlob(verify_k_s);
            nbr_cache.WriteMPInt(verify_e);
            nbr_cache.WriteMPInt(verify_f);
            nbr_cache.WriteMPInt(verify_k);
            nbr_cache.Flush();

            if (algorithm == "ssh-rsa")
            {
                verify_h = hash.ComputeHash(cache.ToArray());

                RSAParameters RSAKeyInfo = new RSAParameters();
                {
                    MemoryStream      ms_tmp  = new MemoryStream(verify_k_s);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string     type  = nbr_tmp.ReadString();
                    BigInteger rsa_e = nbr_tmp.ReadMPInt();
                    BigInteger rsa_n = nbr_tmp.ReadMPInt();
                    RSAKeyInfo.Modulus  = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_n);
                    RSAKeyInfo.Exponent = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_e);
                }
                byte[] rsa_signature_blob;
                {
                    MemoryStream      ms_tmp  = new MemoryStream(verify_sig);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string type = nbr_tmp.ReadString();
                    rsa_signature_blob = nbr_tmp.ReadBlob();
                }


                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.ImportParameters(RSAKeyInfo);
                RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
                RSADeformatter.SetHashAlgorithm("SHA1");

                byte[] xx2    = SHA1.Create().ComputeHash(verify_h);
                bool   verify = RSADeformatter.VerifySignature(xx2, rsa_signature_blob);
            }
        }
        public void PrepareCryptoTransforms()
        {
            byte[] key_cache;
            HashAlgorithm hash_key = SHA1.Create();
            //
            {
                MemoryStream ms_cache = new MemoryStream();
                NetworkByteWriter nbw_cache = new NetworkByteWriter(ms_cache);
                nbw_cache.WriteMPInt(verify_k);
                nbw_cache.WriteBytes(verify_h);
                nbw_cache.WriteByte((byte)0x41);
                nbw_cache.WriteBytes(verify_h);
                key_cache = ms_cache.ToArray();
            }
            byte[] IVc2s = hash_key.ComputeHash(key_cache);
            int j = key_cache.Length - verify_h.Length - 1;
            key_cache[j] = 0x42;
            byte[] IVs2c = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x43;
            byte[] Ec2s = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x44;
            byte[] Es2c = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x45;
            byte[] MACc2s = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x46;
            byte[] MACs2c = hash_key.ComputeHash(key_cache);

            {
                byte[] tmp = new byte[16];
                Array.Copy(Ec2s, 0, tmp, 0, tmp.Length);
                Ec2s = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(IVc2s, 0, tmp, 0, tmp.Length);
                IVc2s = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(Es2c, 0, tmp, 0, tmp.Length);
                Es2c = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(IVs2c, 0, tmp, 0, tmp.Length);
                IVs2c = tmp;
            }

            RijndaelManaged rijndael = new RijndaelManaged();
            rijndael.Mode = CipherMode.CBC;
            rijndael.Padding = PaddingMode.None;

            crypto_encryptor = rijndael.CreateEncryptor(Ec2s, IVc2s);
            crypto_decryptor = rijndael.CreateDecryptor(Es2c, IVs2c);

            crypto_mac_encryptor = HMACSHA1.Create();
            ((HMAC)crypto_mac_encryptor).Key = MACc2s;
        }
        public void KeyVerify(string algorithm, HashAlgorithm hash)
        {
            MemoryStream cache = new MemoryStream();
            NetworkByteWriter nbr_cache = new NetworkByteWriter(cache);

            nbr_cache.WriteString(verify_v_c);
            nbr_cache.WriteString(verify_v_s);
            nbr_cache.WriteBlob(verify_i_c);
            nbr_cache.WriteBlob(verify_i_s);
            nbr_cache.WriteBlob(verify_k_s);
            nbr_cache.WriteMPInt(verify_e);
            nbr_cache.WriteMPInt(verify_f);
            nbr_cache.WriteMPInt(verify_k);
            nbr_cache.Flush();

            if (algorithm == "ssh-rsa")
            {
                verify_h = hash.ComputeHash(cache.ToArray());

                RSAParameters RSAKeyInfo = new RSAParameters();
                {
                    MemoryStream ms_tmp = new MemoryStream(verify_k_s);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string type = nbr_tmp.ReadString();
                    BigInteger rsa_e = nbr_tmp.ReadMPInt();
                    BigInteger rsa_n = nbr_tmp.ReadMPInt();
                    RSAKeyInfo.Modulus = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_n);
                    RSAKeyInfo.Exponent = NetworkByteUtils.BigIntegerToUnsignedArray(rsa_e);
                }
                byte[] rsa_signature_blob;
                {
                    MemoryStream ms_tmp = new MemoryStream(verify_sig);
                    NetworkByteReader nbr_tmp = new NetworkByteReader(ms_tmp);

                    string type = nbr_tmp.ReadString();
                    rsa_signature_blob = nbr_tmp.ReadBlob();
                }

                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                RSA.ImportParameters(RSAKeyInfo);
                RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
                RSADeformatter.SetHashAlgorithm("SHA1");

                byte[] xx2 = SHA1.Create().ComputeHash(verify_h);
                bool verify = RSADeformatter.VerifySignature(xx2, rsa_signature_blob);
            }
        }
Beispiel #4
0
        public void PrepareCryptoTransforms()
        {
            byte[]        key_cache;
            HashAlgorithm hash_key = SHA1.Create();

            //
            {
                MemoryStream      ms_cache  = new MemoryStream();
                NetworkByteWriter nbw_cache = new NetworkByteWriter(ms_cache);
                nbw_cache.WriteMPInt(verify_k);
                nbw_cache.WriteBytes(verify_h);
                nbw_cache.WriteByte((byte)0x41);
                nbw_cache.WriteBytes(verify_h);
                key_cache = ms_cache.ToArray();
            }
            byte[] IVc2s = hash_key.ComputeHash(key_cache);
            int    j     = key_cache.Length - verify_h.Length - 1;

            key_cache[j] = 0x42;
            byte[] IVs2c = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x43;
            byte[] Ec2s = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x44;
            byte[] Es2c = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x45;
            byte[] MACc2s = hash_key.ComputeHash(key_cache);
            key_cache[j] = 0x46;
            byte[] MACs2c = hash_key.ComputeHash(key_cache);


            {
                byte[] tmp = new byte[16];
                Array.Copy(Ec2s, 0, tmp, 0, tmp.Length);
                Ec2s = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(IVc2s, 0, tmp, 0, tmp.Length);
                IVc2s = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(Es2c, 0, tmp, 0, tmp.Length);
                Es2c = tmp;
            }

            {
                byte[] tmp = new byte[16];
                Array.Copy(IVs2c, 0, tmp, 0, tmp.Length);
                IVs2c = tmp;
            }

            RijndaelManaged rijndael = new RijndaelManaged();

            rijndael.Mode    = CipherMode.CBC;
            rijndael.Padding = PaddingMode.None;


            crypto_encryptor = rijndael.CreateEncryptor(Ec2s, IVc2s);
            crypto_decryptor = rijndael.CreateDecryptor(Es2c, IVs2c);

            crypto_mac_encryptor             = HMACSHA1.Create();
            ((HMAC)crypto_mac_encryptor).Key = MACc2s;
        }