예제 #1
0
        public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, string signType, bool keyFromFile)
        {
            try
            {
                byte[] dataBytes;
                if (string.IsNullOrEmpty(charset))
                {
                    dataBytes = Encoding.UTF8.GetBytes(signContent);
                }
                else
                {
                    dataBytes = Encoding.GetEncoding(charset).GetBytes(signContent);
                }
                var rsa = RSAUtil.CreateRsaFromPublicKey(publicKeyPem);
                //var cipherBytes = System.Convert.FromBase64String(signContent);
                //var plainTextBytes = rsa.Decrypt(cipherBytes, RSAEncryptionPadding.Pkcs1);

                if ("RSA2".Equals(signType))
                {
                    return(rsa.VerifyData(dataBytes, Convert.FromBase64String(sign), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
                }
                else
                {
                    return(rsa.VerifyData(dataBytes, Convert.FromBase64String(sign), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
                }
            }
            catch
            {
                return(false);
            }
        }
예제 #2
0
        public static string RSASignCharSet(string data, string privateKeyPem, string charset, string signType)
        {
            RSA rsa = RSAUtil.CreateRsaFromPrivateKey(privateKeyPem);

            byte[] dataBytes = null;
            if (string.IsNullOrEmpty(charset))
            {
                dataBytes = Encoding.UTF8.GetBytes(data);
            }
            else
            {
                dataBytes = Encoding.GetEncoding(charset).GetBytes(data);
            }
            var cipherBytes = rsa.Encrypt(dataBytes, RSAEncryptionPadding.Pkcs1);

            if ("RSA2".Equals(signType))
            {
                cipherBytes = rsa.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            }
            else
            {
                cipherBytes = rsa.SignData(dataBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
            }
            return(Convert.ToBase64String(cipherBytes));
        }