• 大小: 47KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: SHA1withRsa  C#  密钥  签名  

资源简介

支付宝验名验证函数可参考此代码。 SHA1withRsa的各种操作:加密解密,加签验签。 C#源码可编译。签名调用方法:SHA1WithRSA.sign(str, key, "UTF-8");

资源截图

代码片段和文件信息

using System;

namespace RSA
{
    public class Program
    {
        static void Main(string[] arg)
        {

            /**RSA加密测试RSA中的密钥对通过SSL工具生成,生成命令如下:
             * 1 生成RSA私钥:
             * openssl genrsa -out rsa_private_key.pem 1024
             *2 生成RSA公钥
             * openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
             *
             * 3 将RSA私钥转换成PKCS8格式
             * openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_pub_pk8.pem
             *
             * 直接打开rsa_private_key.pem和rsa_pub_pk8.pem文件就可以获取密钥对内容,获取密钥对内容组成字符串时,注意将换行符删除
             * */

            string publickey = “MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzOqfNunFxFtCZPlq7fO/jWwjqmTvAooVBB4y87BizSZ9dl/F7FpAxYc6MmX2TqivCvvORXgdlYdFWAhzXOnIUv9OGG///WPLe9TMs9kIwAZ/APUXauvC01oFLnYkzwPlAh0tQ1Au9arTE/OG1V1dKgf8BXHLPhKL4BmGBEUZBtQIDAQAB“;
            string privatekey = “MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAPM6p826cXEW0Jk+Wrt87+NbCOqZO8CihUEHjLzsGLNJn12X8XsWkDFhzoyZfZOqK8K+85FeB2Vh0VYCHNc6chS/04Yb//9Y8t71Myz2QjABn8A9Rdq68LTWgUudiTPA+UCHS1DUC71qtMT84bVXV0qB/wFccs+EovgGYYERRkG1AgMBAAECgYEA2PmnPdgnYKnolfvQ9tXiLaBFGPpvGk4grz0r6FB5TF7N4rErwxECunq0xioaowK4HPc40qHd2SvkkWQ7FCjYIDsnMk1oOhxNKn0J3FG0n5Cg1/dFai4eoXHs/nKn3SVZ8YZC1T2cMtN2srectLqNqhB8aQEe8xmykyUlUpg/qmECQQD9vkwjUotG5oUUrOj6etcB4WcdyyH0FtThKgyoJUDwgBv6lGGzWyFJEREvp47IgV+FgC7zeP2mL4MhgnD3tNCZAkEA9WRrjOLBNc379XZpoDsH7rZjobVvhnTrEuRDx/whqZ+vk64EPrEW81XYh647bAbJlFn2jPhY+IUHkrxFEFT/fQJBAMoLNOULXQtfkqgb5odMONeue0Ul8itB4tBHgzyALW1TFPQ6InGGJsLfbCfd67uMCFts7fXAaXhibK/KBdm3iEECQQChwVAjzlUN4nnzk9qMhFz2PcPvFGovd2J9UXpcmRaXeWuDLXIe4Rz/ydaxmWgSDWdTIvoicpIzP31+fBwKZ/0BAkEAy0bh4weKmYF29//rK0sxmY8RtqkQeFrwWbqx1daa1w0DfWlNSvy47zyW1G5/AdZU6JSpXxlxdlM/HSDw+v7kcA==“;

            //加密字符串
            string data = “yibao“;

            Console.WriteLine(“加密前字符串内容:“ + data);
            //加密
            string encrypteddata = SHA1WithRSA.encryptData(data publickey “UTF-8“);
            Console.WriteLine(“加密后的字符串为:“ + encrypteddata);
            Console.WriteLine(“解密后的字符串内容:“ + SHA1WithRSA.decryptData(encrypteddata privatekey “UTF-8“));

            Console.WriteLine(“***********“);

            //解密
            string endata = “LpnnvnfA72VnyjboX/OsCPO6FOFXeEnnsKkI7aAEQyVAPfCTfQ43ZYVZVqnADDPMW7VhBXJWyQMAGw2Fh9sS/XLHmO5XW94Yehci6JrJMynePgtIiDysjNA+UlgSTC/MlResNrBm/4MMSPvq0qLwScgpZDynhLsVZk+EQ6G8wgA=“;
            string datamw = SHA1WithRSA.decryptData(endata privatekey “UTF-8“);
            Console.WriteLine(“静态加密后的字符串为:“ + endata);
            Console.WriteLine(“解密后的字符串内容:“ + datamw);

            //签名
            string signdata = “YB010000001441234567286038508081299“;
            Console.WriteLine(“签名前的字符串内容:“ + signdata);
            string sign = SHA1WithRSA.sign(signdata privatekey “UTF-8“);
            Console.WriteLine(“签名后的字符串:“ + sign);

            //验签
            string ysigndata = “YB0100000014412

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     14336  2016-10-14 16:13  SHA1withRsa\.vs\rsa\v15\.suo

     文件      13312  2017-08-31 14:50  SHA1withRsa\RSA\bin\Debug\RSA.exe

     文件      26112  2017-08-31 14:50  SHA1withRsa\RSA\bin\Debug\RSA.pdb

     文件      11600  2016-10-14 16:13  SHA1withRsa\RSA\bin\Debug\RSA.vshost.exe

     文件        490  2012-06-06 02:06  SHA1withRsa\RSA\bin\Debug\RSA.vshost.exe.manifest

     文件         42  2017-08-31 14:49  SHA1withRsa\RSA\obj\x86\Debug\CoreCompileInputs.cache

     文件       6636  2017-08-31 14:49  SHA1withRsa\RSA\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1071  2017-08-31 14:50  SHA1withRsa\RSA\obj\x86\Debug\RSA.csproj.FileListAbsolute.txt

     文件       7052  2017-08-31 14:50  SHA1withRsa\RSA\obj\x86\Debug\RSA.csprojResolveAssemblyReference.cache

     文件      13312  2017-08-31 14:50  SHA1withRsa\RSA\obj\x86\Debug\RSA.exe

     文件      26112  2017-08-31 14:50  SHA1withRsa\RSA\obj\x86\Debug\RSA.pdb

     文件       3671  2017-08-31 14:50  SHA1withRsa\RSA\Program.cs

     文件       1356  2016-10-13 12:00  SHA1withRsa\RSA\Properties\AssemblyInfo.cs

     文件       2469  2017-08-31 14:50  SHA1withRsa\RSA\RSA.csproj

     文件      20068  2017-08-31 14:50  SHA1withRsa\RSA\SHA1WithRSA.cs

     文件        851  2016-10-14 16:13  SHA1withRsa\rsa.sln

    ..A..H.     14336  2016-10-14 16:13  SHA1withRsa\rsa.suo

    ..AD...         0  2017-08-31 14:51  SHA1withRsa\RSA\obj\x86\Debug\TempPE

     目录          0  2017-08-31 14:50  SHA1withRsa\RSA\obj\x86\Debug

     目录          0  2017-08-31 14:42  SHA1withRsa\.vs\rsa\v15

     目录          0  2017-08-31 14:41  SHA1withRsa\RSA\bin\Debug

    ..AD...         0  2017-08-31 14:51  SHA1withRsa\RSA\bin\Release

     目录          0  2017-08-31 14:41  SHA1withRsa\RSA\obj\x86

     目录          0  2017-08-31 14:42  SHA1withRsa\.vs\rsa

     目录          0  2017-08-31 14:49  SHA1withRsa\RSA\bin

     目录          0  2017-08-31 14:41  SHA1withRsa\RSA\obj

     目录          0  2017-08-31 14:41  SHA1withRsa\RSA\Properties

    ...D.H.         0  2017-08-31 14:42  SHA1withRsa\.vs

     目录          0  2017-08-31 14:50  SHA1withRsa\RSA

     目录          0  2017-08-31 14:42  SHA1withRsa

............此处省略3个文件信息

评论

共有 条评论