Ejemplo n.º 1
0
        /// <summary>
        /// 按PKCS#1格式生成RSA公钥
        /// </summary>
        /// <param name="biN"></param>
        /// <param name="biE"></param>
        public static byte[] PKCS1_Pub_Gen(BigInteger n, BigInteger e)
        {
            ByteString   pubKey = new ByteString();
            ASN1_Convert asn1   = new ASN1_Convert();

            pubKey += asn1.BigIntToBytes(n, 128);
            pubKey += asn1.BigIntToBytes(e);
            pubKey  = asn1.SeqToBytes(pubKey.GetBytes());
            return(pubKey.GetBytes());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 按PKCS#1格式生成RSA私钥
        /// </summary>
        public static byte[] PKCS1_Pri_Gen()
        {
            BigInteger[] RSA_Key     = GenKeyAdd();
            BigInteger   exponent1   = RSA_Key[2] % (RSA_Key[3] - 1);
            BigInteger   exponent2   = RSA_Key[2] % (RSA_Key[4] - 1);
            BigInteger   coefficient = (RSA_Key[3] - 1) % RSA_Key[4];

            ByteString   priKey = new ByteString();
            ASN1_Convert asn1   = new ASN1_Convert();

            priKey += asn1.IntToBytes(0);                           // Version
            priKey += asn1.BigIntToBytes(RSA_Key[0], 128);          // n
            priKey += asn1.BigIntToBytes(RSA_Key[1]);               // e
            priKey += asn1.BigIntToBytes(RSA_Key[2]);               // d
            priKey += asn1.BigIntToBytes(RSA_Key[3]);               // p
            priKey += asn1.BigIntToBytes(RSA_Key[4]);               // q
            priKey += asn1.BigIntToBytes(exponent1);                // exponent1
            priKey += asn1.BigIntToBytes(exponent2);                // exponent2
            priKey += asn1.BigIntToBytes(coefficient);              // coefficient
            priKey  = asn1.SeqToBytes(priKey.GetBytes());

            return(priKey.GetBytes());
        }