Esempio n. 1
0
        public bool Verify(byte[] signature, byte[] data, uint160 nonce)
        {
            byte[]       output    = new byte[256];
            var          msg       = Utils.Combine(nonce.ToBytes(), data);
            Sha512Digest sha512    = new Sha512Digest();
            var          generator = new Mgf1BytesGenerator(sha512);

            generator.Init(new MgfParameters(msg));
            generator.GenerateBytes(output, 0, output.Length);
            var input = new BigInteger(1, output);

            if (input.CompareTo(_Key.Modulus) >= 0)
            {
                return(false);
            }
            if (signature.Length > 256)
            {
                return(false);
            }
            var signatureInt = new BigInteger(1, signature);

            if (signatureInt.CompareTo(_Key.Modulus) >= 0)
            {
                return(false);
            }
            var engine = new RsaCoreEngine();

            engine.Init(false, _Key);
            return(input.Equals(engine.ProcessBlock(signatureInt)));
        }
Esempio n. 2
0
 public virtual byte[] ProcessBlock(byte[] inBuf, int inOff, int inLen)
 {
     if (core == null)
     {
         throw new InvalidOperationException("RSA engine not initialised");
     }
     return(core.ConvertOutput(core.ProcessBlock(core.ConvertInput(inBuf, inOff, inLen))));
 }
    public virtual byte[] ProcessBlock(byte[] inBuf, int inOff, int inLen)
    {
        if (key == null)
        {
            throw new InvalidOperationException("RSA engine not initialised");
        }
        BigInteger bigInteger = core.ConvertInput(inBuf, inOff, inLen);
        BigInteger bigInteger4;

        if (key is RsaPrivateCrtKeyParameters)
        {
            RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = (RsaPrivateCrtKeyParameters)key;
            BigInteger publicExponent = rsaPrivateCrtKeyParameters.PublicExponent;
            if (publicExponent != null)
            {
                BigInteger modulus     = rsaPrivateCrtKeyParameters.Modulus;
                BigInteger bigInteger2 = BigIntegers.CreateRandomInRange(BigInteger.One, modulus.Subtract(BigInteger.One), random);
                BigInteger input       = bigInteger2.ModPow(publicExponent, modulus).Multiply(bigInteger).Mod(modulus);
                BigInteger bigInteger3 = core.ProcessBlock(input);
                BigInteger val         = bigInteger2.ModInverse(modulus);
                bigInteger4 = bigInteger3.Multiply(val).Mod(modulus);
                if (!bigInteger.Equals(bigInteger4.ModPow(publicExponent, modulus)))
                {
                    throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
                }
            }
            else
            {
                bigInteger4 = core.ProcessBlock(bigInteger);
            }
        }
        else
        {
            bigInteger4 = core.ProcessBlock(bigInteger);
        }
        return(core.ConvertOutput(bigInteger4));
    }
Esempio n. 4
0
        internal BigInteger Encrypt(BigInteger data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.CompareTo(_Key.Modulus) >= 0)
            {
                throw new ArgumentException("input too large for RSA cipher.");
            }
            RsaCoreEngine engine = new RsaCoreEngine();

            engine.Init(true, _Key);
            return(engine.ProcessBlock(data));
        }
Esempio n. 5
0
        internal BigInteger Decrypt(BigInteger encrypted)
        {
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (encrypted.CompareTo(_Key.Modulus) >= 0)
            {
                throw new DataLengthException("input too large for RSA cipher.");
            }

            RsaCoreEngine engine = new RsaCoreEngine();

            engine.Init(false, _Key);
            return(engine.ProcessBlock(encrypted));
        }
Esempio n. 6
0
 public byte[] Sign(byte[] data, out uint160 nonce)
 {
     while (true)
     {
         byte[] output = new byte[256];
         nonce = new uint160(RandomUtils.GetBytes(20));
         Sha512Digest sha512    = new Sha512Digest();
         var          msg       = Utils.Combine(nonce.ToBytes(), data);
         var          generator = new Mgf1BytesGenerator(sha512);
         generator.Init(new MgfParameters(msg));
         generator.GenerateBytes(output, 0, output.Length);
         var input = new BigInteger(1, output);
         if (input.CompareTo(_Key.Modulus) >= 0)
         {
             continue;
         }
         var engine = new RsaCoreEngine();
         engine.Init(true, _Key);
         return(engine.ConvertOutput(engine.ProcessBlock(input)));
     }
 }