示例#1
0
        private BigInteger[] Encrypt(BigInteger biPlain, RSA_Public_Key rpkKey)
        {
            if ((rpkKey.e == 0) || (rpkKey.n == 0))
            {
                throw new System.ArgumentException("This is not a valid public key");
            }

            BigInteger biCipher = biPlain.modPow(rpkKey.e, rpkKey.n);

            BigInteger[] biOutput = new BigInteger[1];
            biOutput[0] = biCipher;

            return(biOutput);
        }
示例#2
0
        /// <summary>
        /// Public key operation. Encrypt biPlain with the keydata
        /// in the given public key packet. Result of c = m^e mod n
        /// is returned.
        /// </summary>
        /// <param name="biPlain">The plaintext that is about to
        /// be encrypted</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the encryption</param>
        /// <returns>c = m^e mod n, the encrypted plaintext. The return
        /// value is given as an array of biginteger. The length of the
        /// array is 1 and only return[0] has a value.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger[] Encrypt(BigInteger biPlain, PublicKeyPacket pkpKey)
        {
            RSA_Public_Key rpkKey = new RSA_Public_Key();

            if ((pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Only) &&
                (pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Sign))
            {
                throw new System.ArgumentException("This public key is not supposed to be used for RSA encryption.");
            }
            if (pkpKey.KeyMaterial.Length != 2)
            {
                throw new System.ArgumentException("This is not a valid RSA Key");
            }

            rpkKey.n = pkpKey.KeyMaterial[0];
            rpkKey.e = pkpKey.KeyMaterial[1];

            return(Encrypt(biPlain, rpkKey));
        }
示例#3
0
        /// <summary>
        /// Public key operation. Verifies biSignature with the keydata
        /// in the given public key packet and returns true if the signature
        /// is valid.
        /// </summary>
        /// <param name="biSignature">The signature that is about to
        /// be verified</param>
        /// <param name="biHash">The hash value of the signed message.</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the verification.</param>
        /// <returns>True if the signature is valid, otherwise
        /// false</returns>
        /// <remarks>No remarks</remarks>
        public override bool Verify(BigInteger[] biSignature, BigInteger biHash, PublicKeyPacket pkpKey)
        {
            RSA_Public_Key rpkKey = new RSA_Public_Key();

            if ((pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Sign) &&
                (pkpKey.Algorithm != AsymAlgorithms.RSA_Sign_Only))
            {
                throw new System.ArgumentException("This public key is not supposed to be used for RSA signatures.");
            }
            if (pkpKey.KeyMaterial.Length != 2)
            {
                throw new System.ArgumentException("This is not a valid RSA Key");
            }

            rpkKey.n = pkpKey.KeyMaterial[0];
            rpkKey.e = pkpKey.KeyMaterial[1];

            BigInteger biReturn = Encrypt(biSignature[0], rpkKey)[0];

            return(biReturn == biHash);
        }
示例#4
0
        private BigInteger[] Encrypt(BigInteger biPlain, RSA_Public_Key rpkKey)
        {
            if ((rpkKey.e == 0) || (rpkKey.n == 0))
                throw new System.ArgumentException("This is not a valid public key");

            BigInteger biCipher = biPlain.modPow(rpkKey.e, rpkKey.n);
            BigInteger[] biOutput = new BigInteger[1];
            biOutput[0] = biCipher;

            return biOutput;
        }
示例#5
0
        /// <summary>
        /// Public key operation. Verifies biSignature with the keydata
        /// in the given public key packet and returns true if the signature
        /// is valid.
        /// </summary>
        /// <param name="biSignature">The signature that is about to
        /// be verified</param>
        /// <param name="biHash">The hash value of the signed message.</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the verification.</param>
        /// <returns>True if the signature is valid, otherwise 
        /// false</returns>
        /// <remarks>No remarks</remarks>
        public override bool Verify(BigInteger[] biSignature, BigInteger biHash, PublicKeyPacket pkpKey)
        {
            RSA_Public_Key rpkKey = new RSA_Public_Key();

            if ((pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Sign) &&
                    (pkpKey.Algorithm != AsymAlgorithms.RSA_Sign_Only)) {
                throw new System.ArgumentException("This public key is not supposed to be used for RSA signatures.");
            }
            if (pkpKey.KeyMaterial.Length != 2) {
                throw new System.ArgumentException("This is not a valid RSA Key");
            }

            rpkKey.n = pkpKey.KeyMaterial[0];
            rpkKey.e = pkpKey.KeyMaterial[1];

            BigInteger biReturn = Encrypt(biSignature[0], rpkKey)[0];

            return biReturn == biHash;
        }
示例#6
0
        /// <summary>
        /// Public key operation. Encrypt biPlain with the keydata
        /// in the given public key packet. Result of c = m^e mod n
        /// is returned.
        /// </summary>
        /// <param name="biPlain">The plaintext that is about to
        /// be encrypted</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the encryption</param>
        /// <returns>c = m^e mod n, the encrypted plaintext. The return
        /// value is given as an array of biginteger. The length of the 
        /// array is 1 and only return[0] has a value.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger[] Encrypt(BigInteger biPlain, PublicKeyPacket pkpKey)
        {
            RSA_Public_Key rpkKey = new RSA_Public_Key();

            if ((pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Only) &&
                (pkpKey.Algorithm != AsymAlgorithms.RSA_Encrypt_Sign)) {
                throw new System.ArgumentException("This public key is not supposed to be used for RSA encryption.");
            }
            if (pkpKey.KeyMaterial.Length != 2) {
                throw new System.ArgumentException("This is not a valid RSA Key");
            }

            rpkKey.n = pkpKey.KeyMaterial[0];
            rpkKey.e = pkpKey.KeyMaterial[1];

            return Encrypt(biPlain, rpkKey);
        }