• 大小: 45KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-12
  • 语言: C#
  • 标签: C#  SHA1  Rsa;  

资源简介

C# SHA1withRsa的各种操作:加密解密,加签验签

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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 = RSAFromPkcs8.encryptData(data publickey “UTF-8“);
            Console.WriteLine(“加密后的字符串为:“ + encrypteddata);
            Console.WriteLine(“解密后的字符串内容:“ + RSAFromPkcs8.decryptData(encrypteddata privatekey “UTF-8“));

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

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

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

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-10-14 16:13  SHA1withRsa\
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\
     文件        3751  2016-10-13 16:47  SHA1withRsa\RSA\Program.cs
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\Properties\
     文件        1356  2016-10-13 12:00  SHA1withRsa\RSA\Properties\AssemblyInfo.cs
     文件        2470  2016-10-13 12:04  SHA1withRsa\RSA\RSA.csproj
     文件       20104  2016-10-13 12:03  SHA1withRsa\RSA\RSAFromPkcs8.cs
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\bin\
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\bin\Debug\
     文件       13312  2016-10-14 14:27  SHA1withRsa\RSA\bin\Debug\RSA.exe
     文件       28160  2016-10-14 14:27  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
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\obj\
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\obj\x86\
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\obj\x86\Debug\
     文件        5895  2016-10-14 16:13  SHA1withRsa\RSA\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         696  2016-10-14 16:13  SHA1withRsa\RSA\obj\x86\Debug\RSA.csproj.FileListAbsolute.txt
     文件       13312  2016-10-14 14:27  SHA1withRsa\RSA\obj\x86\Debug\RSA.exe
     文件       28160  2016-10-14 14:27  SHA1withRsa\RSA\obj\x86\Debug\RSA.pdb
     目录           0  2016-10-14 16:13  SHA1withRsa\RSA\obj\x86\Debug\TempPE\
     文件         851  2016-10-14 16:13  SHA1withRsa\rsa.sln
     文件       14336  2016-10-14 16:13  SHA1withRsa\rsa.suo

评论

共有 条评论