Esempio n. 1
0
        /// <summary>
        /// Get rsa encrypt max length
        /// </summary>
        /// <param name="rsa">Rsa instance </param>
        /// <param name="padding"><see cref="RSAEncryptionPadding"/></param>
        /// <returns></returns>
        private static int GetMaxRsaEncryptLength(RSA rsa, RSAEncryptionPadding padding)
        {
            var offset = 0;

            if (padding.Mode == RSAEncryptionPaddingMode.Pkcs1)
            {
                offset = 11;
            }
            else
            {
                if (padding.Equals(RSAEncryptionPadding.OaepSHA1))
                {
                    offset = 42;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA256))
                {
                    offset = 66;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA384))
                {
                    offset = 98;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA512))
                {
                    offset = 130;
                }
            }
            var keySize   = rsa.KeySize;
            var maxLength = keySize / 8 - offset;

            return(maxLength);
        }
Esempio n. 2
0
        /// <summary>
        /// 最大分块大小
        /// </summary>
        /// <param name="rsa">RSA实例</param>
        /// <param name="padding">填充方式</param>
        /// <returns></returns>
        private int MaxBlockSize(RSA rsa, RSAEncryptionPadding padding)
        {
            int offset = 0;

            if (padding.Equals(RSAEncryptionPadding.Pkcs1))
            {
                offset = 11;
            }
            else if (padding.Equals(RSAEncryptionPadding.OaepSHA1))
            {
                offset = 42;
            }
            else if (padding.Equals(RSAEncryptionPadding.OaepSHA256))
            {
                offset = 66;
            }
            else if (padding.Equals(RSAEncryptionPadding.OaepSHA384))
            {
                offset = 98;
            }
            else if (padding.Equals(RSAEncryptionPadding.OaepSHA512))
            {
                offset = 130;
            }
            return(rsa.KeySize / 8 - offset);
        }
Esempio n. 3
0
        /// <summary>
        /// 按照补位规则计算大小
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="padding"></param>
        /// <returns></returns>
        public static int GetMaxBlockSize(RSA rsa, RSAEncryptionPadding padding)
        {
            var offset = 0;

            if (padding.Mode == RSAEncryptionPaddingMode.Pkcs1)
            {
                offset = 11;
            }
            else
            {
                if (padding.Equals(RSAEncryptionPadding.OaepSHA1))
                {
                    offset = 42;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA256))
                {
                    offset = 66;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA384))
                {
                    offset = 98;
                }

                if (padding.Equals(RSAEncryptionPadding.OaepSHA512))
                {
                    offset = 130;
                }
            }
            return(rsa.KeySize / 8 - offset);
        }
Esempio n. 4
0
        private Mechanism CreateDecryptMechanism(RSAEncryptionPadding padding)
        {
            if (padding.Equals(RSAEncryptionPadding.Pkcs1))
            {
                return(new Mechanism(CKM.CKM_RSA_PKCS));
            }

            throw new SlovakEidDecryptionException("Unsuported mechanism");

            //if (padding.Equals(RSAEncryptionPadding.OaepSHA1))
            //{
            //    return new Mechanism(CKM.CKM_RSA_PKCS_OAEP, new Net.Pkcs11Interop.HighLevelAPI.MechanismParams.CkRsaPkcsOaepParams((ulong)CKM.CKM_SHA_1, (ulong)CKG.CKG_MGF1_SHA1, (ulong)CKZ.CKZ_DATA_SPECIFIED, null));
            //}

            //if (padding.Equals(RSAEncryptionPadding.OaepSHA256))
            //{
            //    return new Mechanism(CKM.CKM_RSA_PKCS_OAEP, new Net.Pkcs11Interop.HighLevelAPI.MechanismParams.CkRsaPkcsOaepParams((ulong)CKM.CKM_SHA256, (ulong)CKG.CKG_MGF1_SHA256, (ulong)CKZ.CKZ_DATA_SPECIFIED, null));
            //}

            //if (padding.Equals(RSAEncryptionPadding.OaepSHA384))
            //{
            //    return new Mechanism(CKM.CKM_RSA_PKCS_OAEP, new Net.Pkcs11Interop.HighLevelAPI.MechanismParams.CkRsaPkcsOaepParams((ulong)CKM.CKM_SHA384, (ulong)CKG.CKG_MGF1_SHA384, (ulong)CKZ.CKZ_DATA_SPECIFIED, null));
            //}

            //if (padding.Equals(RSAEncryptionPadding.OaepSHA512))
            //{
            //    return new Mechanism(CKM.CKM_RSA_PKCS_OAEP, new Net.Pkcs11Interop.HighLevelAPI.MechanismParams.CkRsaPkcsOaepParams((ulong)CKM.CKM_SHA512, (ulong)CKG.CKG_MGF1_SHA512, (ulong)CKZ.CKZ_SALT_SPECIFIED, null));
            //}
        }