数字签名学习

1 基本概念

数字签名是一种加密技术,用于验证信息来源的身份和数据的完整性。

就是对一个东西签上自己的名;收到的人可以验证这东西是你发的;这里是用数字的方式;

对字符串也可以签名,签名以后,还是一个字符串,不过是经过签名的字符串。

密钥有公钥和私钥; 公钥是公开的,人人都知道;私钥是自己的,只有自己知道;

签上自己的名,那肯定用自己的私钥;

C#代码看一下;

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace rsademo
{
    public partial class Form1 : Form
    {
        private string publicKey;
        private string privateKey;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                publicKey = rsa.ToXmlString(false); // 公钥
                privateKey = rsa.ToXmlString(true); // 私钥
            }

        }
        public static string SignData(string data, string privateKey)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.FromXmlString(privateKey);
                byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                byte[] signatureBytes = rsa.SignData(dataBytes, CryptoConfig.MapNameToOID("SHA1"));
                return Convert.ToBase64String(signatureBytes);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = SignData(textBox1.Text, privateKey);
        }
    }
}

首先new一个RSA加密服务提供者对象;

然后可以生成公钥和私钥;

写一个签名函数,需要的参数是待签名的字符串、私钥;

第一个文本框是原来的字符串,第二个文本框是经过数字签名的字符串;

相关推荐
大犀牛牛1 天前
拆解开放签电子签系统“一核多态”的SaaS产品版本管理实战
开源·数字签名·电子合同·电子签章
利刃大大8 天前
【SpringBoot】validation参数校验 && JWT鉴权实现 && 加密/加盐
java·spring boot·jwt·加密
Irene199111 天前
文档加密加水印只读分享:WPS/PPT编辑后导出PDF(附:百度网盘分享流程)
pdf·加密·wps·pptx·只读
嵌入式-小王14 天前
证书、加密相关知识点
网络·证书·加密
csdn_aspnet16 天前
ASP.NET Core:创建并验证文档上的数字签名
.netcore·数字签名
小明记账簿21 天前
微信小程序中Crypto.js加密解密
微信小程序·小程序·加密·解密
胡玉洋23 天前
Spring Boot 项目配置文件密码加密解决方案 —— Jasypt 实战指南
java·spring boot·后端·安全·加密·配置文件·jasypt
嫂子的姐夫1 个月前
004-MD5_易车网
爬虫·python·逆向·加密
帅逼码农1 个月前
openfhe同态库安装
加密·全同态·openfhe·库安装
躺柒1 个月前
读捍卫隐私01双因素认证
信息安全·加密·隐私·隐私保护