I2OSP() public static method

public static I2OSP ( byte x, int size ) : byte[]
x byte
size int
return byte[]
Ejemplo n.º 1
0
        public static byte[] MGF1(HashAlgorithm hash, byte[] mgfSeed, int maskLen)
        {
            if (maskLen < 0)
            {
                throw new OverflowException();
            }
            int length = mgfSeed.Length;
            int count  = hash.HashSize >> 3;
            int num    = maskLen / count;

            if (maskLen % count != 0)
            {
                ++num;
            }
            byte[] numArray1 = new byte[num * count];
            byte[] buffer    = new byte[length + 4];
            int    dstOffset = 0;

            for (int x = 0; x < num; ++x)
            {
                byte[] numArray2 = PKCS1.I2OSP(x, 4);
                Buffer.BlockCopy((Array)mgfSeed, 0, (Array)buffer, 0, length);
                Buffer.BlockCopy((Array)numArray2, 0, (Array)buffer, length, 4);
                Buffer.BlockCopy((Array)hash.ComputeHash(buffer), 0, (Array)numArray1, dstOffset, count);
                dstOffset += length;
            }
            byte[] numArray3 = new byte[maskLen];
            Buffer.BlockCopy((Array)numArray1, 0, (Array)numArray3, 0, maskLen);
            return(numArray3);
        }
Ejemplo n.º 2
0
        public static byte[] Sign_v15(RSA rsa, HashAlgorithm hash, byte[] hashValue)
        {
            int num = rsa.KeySize >> 3;

            byte[] m = PKCS1.OS2IP(PKCS1.Encode_v15(hash, hashValue, num));
            return(PKCS1.I2OSP(PKCS1.RSASP1(rsa, m), num));
        }
Ejemplo n.º 3
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.º 4
0
        public static byte[] Decrypt_v15(RSA rsa, byte[] C)
        {
            int size = rsa.KeySize >> 3;

            if (size < 11 || C.Length > size)
            {
                throw new CryptographicException("decryption error");
            }
            byte[] c         = PKCS1.OS2IP(C);
            byte[] numArray1 = PKCS1.I2OSP(PKCS1.RSADP(rsa, c), size);
            if (numArray1[0] != (byte)0 || numArray1[1] != (byte)2)
            {
                return((byte[])null);
            }
            int index = 10;

            while (numArray1[index] != (byte)0 && index < numArray1.Length)
            {
                ++index;
            }
            if (numArray1[index] != (byte)0)
            {
                return((byte[])null);
            }
            int srcOffset = index + 1;

            byte[] numArray2 = new byte[numArray1.Length - srcOffset];
            Buffer.BlockCopy((Array)numArray1, srcOffset, (Array)numArray2, 0, numArray2.Length);
            return(numArray2);
        }
Ejemplo n.º 5
0
        public static byte[] Decrypt_OAEP(RSA rsa, HashAlgorithm hash, byte[] C)
        {
            int size    = rsa.KeySize / 8;
            int maskLen = hash.HashSize / 8;

            if (size < 2 * maskLen + 2 || C.Length != size)
            {
                throw new CryptographicException("decryption error");
            }
            byte[] c         = PKCS1.OS2IP(C);
            byte[] numArray1 = PKCS1.I2OSP(PKCS1.RSADP(rsa, c), size);
            byte[] array1    = new byte[maskLen];
            Buffer.BlockCopy((Array)numArray1, 1, (Array)array1, 0, array1.Length);
            byte[] numArray2 = new byte[size - maskLen - 1];
            Buffer.BlockCopy((Array)numArray1, numArray1.Length - numArray2.Length, (Array)numArray2, 0, numArray2.Length);
            byte[] array2_1  = PKCS1.MGF1(hash, numArray2, maskLen);
            byte[] mgfSeed   = PKCS1.xor(array1, array2_1);
            byte[] array2_2  = PKCS1.MGF1(hash, mgfSeed, size - maskLen - 1);
            byte[] numArray3 = PKCS1.xor(numArray2, array2_2);
            byte[] emptyHash = PKCS1.GetEmptyHash(hash);
            byte[] array2_3  = new byte[emptyHash.Length];
            Buffer.BlockCopy((Array)numArray3, 0, (Array)array2_3, 0, array2_3.Length);
            bool flag   = PKCS1.Compare(emptyHash, array2_3);
            int  length = emptyHash.Length;

            while (numArray3[length] == (byte)0)
            {
                ++length;
            }
            int count = numArray3.Length - length - 1;

            byte[] numArray4 = new byte[count];
            Buffer.BlockCopy((Array)numArray3, length + 1, (Array)numArray4, 0, count);
            return(numArray1[0] != (byte)0 || !flag || numArray3[length] != (byte)1 ? (byte[])null : numArray4);
        }
Ejemplo n.º 6
0
        public static byte[] MGF1(HashAlgorithm hash, byte[] mgfSeed, int maskLen)
        {
            if (maskLen < 0)
            {
                throw new OverflowException();
            }
            int num  = mgfSeed.Length;
            int num2 = hash.HashSize >> 3;
            int num3 = maskLen / num2;

            if (maskLen % num2 != 0)
            {
                num3++;
            }
            byte[] array  = new byte[num3 * num2];
            byte[] array2 = new byte[num + 4];
            int    num4   = 0;

            for (int i = 0; i < num3; i++)
            {
                byte[] src = PKCS1.I2OSP(i, 4);
                Buffer.BlockCopy(mgfSeed, 0, array2, 0, num);
                Buffer.BlockCopy(src, 0, array2, num, 4);
                byte[] src2 = hash.ComputeHash(array2);
                Buffer.BlockCopy(src2, 0, array, num4, num2);
                num4 += num;
            }
            byte[] array3 = new byte[maskLen];
            Buffer.BlockCopy(array, 0, array3, 0, maskLen);
            return(array3);
        }
Ejemplo n.º 7
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[] x      = PKCS1.RSAVP1(rsa, s);
            byte[] array  = PKCS1.I2OSP(x, num);
            byte[] array2 = PKCS1.Encode_v15(hash, hashValue, num);
            bool   flag   = PKCS1.Compare(array2, array);

            if (flag || !tryNonStandardEncoding)
            {
                return(flag);
            }
            if (array[0] != 0 || array[1] != 1)
            {
                return(false);
            }
            int i;

            for (i = 2; i < array.Length - hashValue.Length - 1; i++)
            {
                if (array[i] != 255)
                {
                    return(false);
                }
            }
            if (array[i++] != 0)
            {
                return(false);
            }
            byte[] array3 = new byte[hashValue.Length];
            Buffer.BlockCopy(array, i, array3, 0, array3.Length);
            return(PKCS1.Compare(array3, hashValue));
        }
Ejemplo n.º 8
0
        public static byte[] Decrypt_v15(RSA rsa, byte[] C)
        {
            int num = rsa.KeySize >> 3;

            if (num < 11 || 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);
            if (array[0] != 0 || array[1] != 2)
            {
                return(null);
            }
            int num2 = 10;

            while (array[num2] != 0 && num2 < array.Length)
            {
                num2++;
            }
            if (array[num2] != 0)
            {
                return(null);
            }
            num2++;
            byte[] array2 = new byte[array.Length - num2];
            Buffer.BlockCopy(array, num2, array2, 0, array2.Length);
            return(array2);
        }
Ejemplo n.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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.º 14
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));
 }