Exemplo n.º 1
0
        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="content">待验签字符串</param>
        /// <param name="signedString">签名</param>
        /// <param name="publicKey">公钥</param>
        /// <returns>true(通过),false(不通过)</returns>
        public static bool Verify(string content, string signedString, string publicKey)
        {
            AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey);
            ISigner signer = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA

            signer.Init(false, pubKey);
            var expectedSig = Convert.FromBase64String(signedString);
            var msgBytes    = Encoding.UTF8.GetBytes(content);

            signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
            return(signer.VerifySignature(expectedSig));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 签名
        /// </summary>
        /// <param name="content">待签名字符串</param>
        /// <param name="privateKey">私钥</param>

        /// <returns>签名后字符串</returns>
        public static string Sign(string content, string privateKey)
        {
            AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey);
            ISigner sig = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA

            sig.Init(true, priKey);
            var bytes = Encoding.UTF8.GetBytes(content);

            sig.BlockUpdate(bytes, 0, bytes.Length);
            byte[] signature    = sig.GenerateSignature();
            var    signedString = Convert.ToBase64String(signature);

            return(signedString);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 私钥解密
 /// </summary>
 /// <param name="resData">加密字符串</param>
 /// <param name="privateKey">私钥</param>
 /// <param name="input_charset">编码格式</param>
 /// <returns>明文</returns>
 public static string Decrypt(string resData, string privateKey)
 {
     try
     {
         AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey);
         IBufferedCipher        cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
         cipher.Init(false, priKey);//false表示解密
         var encryptData = Convert.FromBase64String(resData);
         var decryptData = cipher.DoFinal(encryptData);
         return(Encoding.UTF8.GetString(decryptData));
     }
     catch (Exception) {
         throw new Exception("解密失败");
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 公钥加密
 /// </summary>
 /// <param name="resData">需要加密的字符串</param>
 /// <param name="publicKey">公钥</param>
 /// <returns>明文</returns>
 public static string Encrypt(string resData, string publicKey)
 {
     try
     {
         AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey);
         IBufferedCipher        cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
         cipher.Init(true, pubKey);//true表示加密
         var    data        = Encoding.UTF8.GetBytes(resData.Trim());
         byte[] encryptData = cipher.DoFinal(data);
         return(Convert.ToBase64String(encryptData));
     }
     catch (Exception)
     {
         throw new Exception("加密失败");
     }
 }