예제 #1
0
        // PKCS #1 v.2.1, Section 9.2
        // EMSA-PKCS1-v1_5-Encode
        static byte[] Encode_v15(HashAlgorithmType algorithm, byte[] hashValue, int emLength)
        {
            var hashSize = HashAlgorithmProvider.GetHashSize(algorithm);

            if (hashValue.Length != (hashSize >> 3))
            {
                throw new CryptographicException("bad hash length for " + algorithm.ToString());
            }

            // DigestInfo ::= SEQUENCE {
            //	digestAlgorithm AlgorithmIdentifier,
            //	digest OCTET STRING
            // }

            byte[] t = null;

            string oid = HashAlgorithmProvider.GetOID(algorithm);

            if (oid != null)
            {
                ASN1 digestAlgorithm = new ASN1(0x30);
                digestAlgorithm.Add(new ASN1(CryptoConfig.EncodeOID(oid)));
                digestAlgorithm.Add(new ASN1(0x05));                            // NULL
                ASN1 digest     = new ASN1(0x04, hashValue);
                ASN1 digestInfo = new ASN1(0x30);
                digestInfo.Add(digestAlgorithm);
                digestInfo.Add(digest);

                t = digestInfo.GetBytes();
            }
            else
            {
                // There are no valid OID, in this case t = hashValue
                // This is the case of the MD5SHA hash algorithm
                t = hashValue;
            }

            Buffer.BlockCopy(hashValue, 0, t, t.Length - hashValue.Length, hashValue.Length);

            int PSLength = System.Math.Max(8, emLength - t.Length - 3);

            // PS = PSLength of 0xff

            // EM = 0x00 | 0x01 | PS | 0x00 | T
            byte[] EM = new byte [PSLength + t.Length + 3];
            EM [1] = 0x01;
            for (int i = 2; i < PSLength + 2; i++)
            {
                EM[i] = 0xff;
            }
            Buffer.BlockCopy(t, 0, EM, PSLength + 3, t.Length);

            return(EM);
        }
예제 #2
0
        public static byte[] Encode_v15(HashAlgorithm hash, byte[] hashValue, int emLength)
        {
            if (hashValue.Length != hash.HashSize >> 3)
            {
                throw new CryptographicException("bad hash length for " + hash.ToString());
            }
            string oid = CryptoConfig.MapNameToOID(hash.ToString());

            byte[] numArray1;
            if (oid != null)
            {
                ASN1 asn1_1 = new ASN1((byte)48);
                asn1_1.Add(new ASN1(CryptoConfig.EncodeOID(oid)));
                asn1_1.Add(new ASN1((byte)5));
                ASN1 asn1_2 = new ASN1((byte)4, hashValue);
                ASN1 asN1   = new ASN1((byte)48);
                asN1.Add(asn1_1);
                asN1.Add(asn1_2);
                numArray1 = asN1.GetBytes();
            }
            else
            {
                numArray1 = hashValue;
            }
            Buffer.BlockCopy((Array)hashValue, 0, (Array)numArray1, numArray1.Length - hashValue.Length, hashValue.Length);
            int num = System.Math.Max(8, emLength - numArray1.Length - 3);

            byte[] numArray2 = new byte[num + numArray1.Length + 3];
            numArray2[1] = (byte)1;
            for (int index = 2; index < num + 2; ++index)
            {
                numArray2[index] = byte.MaxValue;
            }
            Buffer.BlockCopy((Array)numArray1, 0, (Array)numArray2, num + 3, numArray1.Length);
            return(numArray2);
        }
예제 #3
0
        public static byte[] Encode_v15(HashAlgorithm hash, byte[] hashValue, int emLength)
        {
            if (hashValue.Length != hash.HashSize >> 3)
            {
                throw new CryptographicException("bad hash length for " + hash.ToString());
            }
            string text = CryptoConfig.MapNameToOID(hash.ToString());

            byte[] array;
            if (text != null)
            {
                ASN1 asn = new ASN1(48);
                asn.Add(new ASN1(CryptoConfig.EncodeOID(text)));
                asn.Add(new ASN1(5));
                ASN1 asn2 = new ASN1(4, hashValue);
                ASN1 asn3 = new ASN1(48);
                asn3.Add(asn);
                asn3.Add(asn2);
                array = asn3.GetBytes();
            }
            else
            {
                array = hashValue;
            }
            Buffer.BlockCopy(hashValue, 0, array, array.Length - hashValue.Length, hashValue.Length);
            int num = Math.Max(8, emLength - array.Length - 3);

            byte[] array2 = new byte[num + array.Length + 3];
            array2[1] = 1;
            for (int i = 2; i < num + 2; i++)
            {
                array2[i] = byte.MaxValue;
            }
            Buffer.BlockCopy(array, 0, array2, num + 3, array.Length);
            return(array2);
        }