数字签名学习

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加密服务提供者对象;

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

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

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

相关推荐
MonkeyKing_sunyuhua5 天前
X-Ca-Key是什么技术
加密
秃头仔仔9 天前
【密码学】分组密码
网络·算法·密码学·加密·分组密码
江上清风山间明月16 天前
使用GPG来解密和加密文件详解
加密·解密·gpg·pgp
极客先躯16 天前
国家商用密码算法-SM4Tool.jar
java·jar·加密·加密工具
梦回阑珊17 天前
《QT从基础到进阶·七十二》基于Qt开发的文件保险柜工具并支持文件各种加密和解密
开发语言·c++·qt·加密
JackieZhengChina19 天前
PHP混淆加密以及常用的一些加密工具
后端·php·加密·混淆
太空眼睛20 天前
【Mybatis-Plus】根据自定义注解实现自动加解密
mybatis·注解·加密·mybatis-plus·拦截器·自动·解密
WineMonk23 天前
.NET C# 实现国密算法加解密
c#·.net·加密·国密
carcarrot1 个月前
.Net实现SCrypt Hash加密
算法·哈希算法·加密·scrypt·scrypt.net
闻缺陷则喜何志丹1 个月前
【哈希映射 字符串 乘法原理】2227. 加密解密字符串
c++·算法·leetcode·哈希算法·加密·解密·乘法原理