Ejemplo n.º 1
0
        public static byte[] Encrypt_v15(RSA rsa, RandomNumberGenerator rng, byte[] M)
        {
            int size = rsa.KeySize / 8;

            if (M.Length > size - 11)
            {
                throw new CryptographicException("message too long");
            }
            int count = System.Math.Max(8, size - M.Length - 3);

            byte[] data = new byte[count];
            rng.GetNonZeroBytes(data);
            byte[] x = new byte[size];
            x[1] = (byte)2;
            Buffer.BlockCopy((Array)data, 0, (Array)x, 2, count);
            Buffer.BlockCopy((Array)M, 0, (Array)x, size - M.Length, M.Length);
            byte[] m = PKCS1.OS2IP(x);
            return(PKCS1.I2OSP(PKCS1.RSAEP(rsa, m), size));
        }
Ejemplo n.º 2
0
        public static bool Verify_v15(
            RSA rsa,
            HashAlgorithm hash,
            byte[] hashValue,
            byte[] signature,
            bool tryNonStandardEncoding)
        {
            int num = rsa.KeySize >> 3;

            byte[] s      = PKCS1.OS2IP(signature);
            byte[] array2 = PKCS1.I2OSP(PKCS1.RSAVP1(rsa, s), num);
            bool   flag   = PKCS1.Compare(PKCS1.Encode_v15(hash, hashValue, num), array2);

            if (flag || !tryNonStandardEncoding)
            {
                return(flag);
            }
            if (array2[0] != (byte)0 || array2[1] != (byte)1)
            {
                return(false);
            }
            int index1;

            for (index1 = 2; index1 < array2.Length - hashValue.Length - 1; ++index1)
            {
                if (array2[index1] != byte.MaxValue)
                {
                    return(false);
                }
            }
            byte[] numArray  = array2;
            int    index2    = index1;
            int    srcOffset = index2 + 1;

            if (numArray[index2] != (byte)0)
            {
                return(false);
            }
            byte[] array1 = new byte[hashValue.Length];
            Buffer.BlockCopy((Array)array2, srcOffset, (Array)array1, 0, array1.Length);
            return(PKCS1.Compare(array1, hashValue));
        }
Ejemplo n.º 3
0
        public static byte[] Encrypt_v15(RSA rsa, RandomNumberGenerator rng, byte[] M)
        {
            int num = rsa.KeySize / 8;

            if (M.Length > num - 11)
            {
                throw new CryptographicException("message too long");
            }
            int num2 = Math.Max(8, num - M.Length - 3);

            byte[] array = new byte[num2];
            rng.GetNonZeroBytes(array);
            byte[] array2 = new byte[num];
            array2[1] = 2;
            Buffer.BlockCopy(array, 0, array2, 2, num2);
            Buffer.BlockCopy(M, 0, array2, num - M.Length, M.Length);
            byte[] m = PKCS1.OS2IP(array2);
            byte[] x = PKCS1.RSAEP(rsa, m);
            return(PKCS1.I2OSP(x, num));
        }
Ejemplo n.º 4
0
        public override byte[] CreateSignature(byte[] rgbHash)
        {
            if (this.key == null)
            {
                throw new CryptographicUnexpectedOperationException("The key is a null reference");
            }
            if (hash == null)
            {
                throw new CryptographicUnexpectedOperationException("The hash algorithm is a null reference.");
            }
            if (rgbHash == null)
            {
                throw new ArgumentNullException("The rgbHash parameter is a null reference.");
            }

                        #pragma warning disable 436
            return(PKCS1.Sign_v15(key, hash.Algorithm, rgbHash));

                        #pragma warning restore 436
        }
Ejemplo n.º 5
0
        public static byte[] Decrypt_OAEP(RSA rsa, HashAlgorithm hash, byte[] C)
        {
            int num  = rsa.KeySize / 8;
            int num2 = hash.HashSize / 8;

            if (num < 2 * num2 + 2 || C.Length != num)
            {
                throw new CryptographicException("decryption error");
            }
            byte[] c      = PKCS1.OS2IP(C);
            byte[] x      = PKCS1.RSADP(rsa, c);
            byte[] array  = PKCS1.I2OSP(x, num);
            byte[] array2 = new byte[num2];
            Buffer.BlockCopy(array, 1, array2, 0, array2.Length);
            byte[] array3 = new byte[num - num2 - 1];
            Buffer.BlockCopy(array, array.Length - array3.Length, array3, 0, array3.Length);
            byte[] array4    = PKCS1.MGF1(hash, array3, num2);
            byte[] mgfSeed   = PKCS1.xor(array2, array4);
            byte[] array5    = PKCS1.MGF1(hash, mgfSeed, num - num2 - 1);
            byte[] array6    = PKCS1.xor(array3, array5);
            byte[] emptyHash = PKCS1.GetEmptyHash(hash);
            byte[] array7    = new byte[emptyHash.Length];
            Buffer.BlockCopy(array6, 0, array7, 0, array7.Length);
            bool flag = PKCS1.Compare(emptyHash, array7);
            int  num3 = emptyHash.Length;

            while (array6[num3] == 0)
            {
                num3++;
            }
            int num4 = array6.Length - num3 - 1;

            byte[] array8 = new byte[num4];
            Buffer.BlockCopy(array6, num3 + 1, array8, 0, num4);
            if (array[0] != 0 || !flag || array6[num3] != 1)
            {
                return(null);
            }
            return(array8);
        }
Ejemplo n.º 6
0
 public static bool Verify_v15(RSA rsa, HashAlgorithm hash, byte[] hashValue, byte[] signature)
 {
     return(PKCS1.Verify_v15(rsa, hash, hashValue, signature, false));
 }
Ejemplo n.º 7
0
 public static byte[] I2OSP(int x, int size)
 {
     byte[] bytes = BitConverterLE.GetBytes(x);
     Array.Reverse((Array)bytes, 0, bytes.Length);
     return(PKCS1.I2OSP(bytes, size));
 }