RSAEP() public static method

public static RSAEP ( RSA rsa, byte m ) : byte[]
rsa System.Security.Cryptography.RSA
m byte
return byte[]
Ejemplo n.º 1
0
        public static byte[] Encrypt_OAEP(
            RSA rsa,
            HashAlgorithm hash,
            RandomNumberGenerator rng,
            byte[] M)
        {
            int size    = rsa.KeySize / 8;
            int maskLen = hash.HashSize / 8;

            if (M.Length > size - 2 * maskLen - 2)
            {
                throw new CryptographicException("message too long");
            }
            byte[] emptyHash = PKCS1.GetEmptyHash(hash);
            int    num       = size - M.Length - 2 * maskLen - 2;

            byte[] array1 = new byte[emptyHash.Length + num + 1 + M.Length];
            Buffer.BlockCopy((Array)emptyHash, 0, (Array)array1, 0, emptyHash.Length);
            array1[emptyHash.Length + num] = (byte)1;
            Buffer.BlockCopy((Array)M, 0, (Array)array1, array1.Length - M.Length, M.Length);
            byte[] numArray1 = new byte[maskLen];
            rng.GetBytes(numArray1);
            byte[] array2_1  = PKCS1.MGF1(hash, numArray1, size - maskLen - 1);
            byte[] mgfSeed   = PKCS1.xor(array1, array2_1);
            byte[] array2_2  = PKCS1.MGF1(hash, mgfSeed, maskLen);
            byte[] numArray2 = PKCS1.xor(numArray1, array2_2);
            byte[] x         = new byte[numArray2.Length + mgfSeed.Length + 1];
            Buffer.BlockCopy((Array)numArray2, 0, (Array)x, 1, numArray2.Length);
            Buffer.BlockCopy((Array)mgfSeed, 0, (Array)x, numArray2.Length + 1, mgfSeed.Length);
            byte[] m = PKCS1.OS2IP(x);
            return(PKCS1.I2OSP(PKCS1.RSAEP(rsa, m), size));
        }
Ejemplo n.º 2
0
        public static byte[] Encrypt_OAEP(RSA rsa, HashAlgorithm hash, RandomNumberGenerator rng, byte[] M)
        {
            int num  = rsa.KeySize / 8;
            int num2 = hash.HashSize / 8;

            if (M.Length > num - 2 * num2 - 2)
            {
                throw new CryptographicException("message too long");
            }
            byte[] emptyHash = PKCS1.GetEmptyHash(hash);
            int    num3      = num - M.Length - 2 * num2 - 2;

            byte[] array = new byte[emptyHash.Length + num3 + 1 + M.Length];
            Buffer.BlockCopy(emptyHash, 0, array, 0, emptyHash.Length);
            array[emptyHash.Length + num3] = 1;
            Buffer.BlockCopy(M, 0, array, array.Length - M.Length, M.Length);
            byte[] array2 = new byte[num2];
            rng.GetBytes(array2);
            byte[] array3 = PKCS1.MGF1(hash, array2, num - num2 - 1);
            byte[] array4 = PKCS1.xor(array, array3);
            byte[] array5 = PKCS1.MGF1(hash, array4, num2);
            byte[] array6 = PKCS1.xor(array2, array5);
            byte[] array7 = new byte[array6.Length + array4.Length + 1];
            Buffer.BlockCopy(array6, 0, array7, 1, array6.Length);
            Buffer.BlockCopy(array4, 0, array7, array6.Length + 1, array4.Length);
            byte[] m = PKCS1.OS2IP(array7);
            byte[] x = PKCS1.RSAEP(rsa, m);
            return(PKCS1.I2OSP(x, num));
        }
Ejemplo n.º 3
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.º 4
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));
        }