数字签名学习

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

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

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

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

相关推荐
天若有情6735 小时前
【自研实战】轻量级ASCII字符串加密算法:从设计到落地(防查岗神器版)
网络·c++·算法·安全·数据安全·加密
REDcker10 小时前
VMP 加固与 VMProtect 原理与使用
加密·反编译·vmp·vmprotect·破解·加固·梆梆加固
独断万古他化1 天前
【SSM开发实战:博客系统】(三)核心业务功能开发与安全加密实现
spring boot·spring·mybatis·博客系统·加密
深信达沙箱8 天前
源代码防泄密软件的综合对比分析
加密·源代码·沙盒
不凉帅14 天前
NO.4信息安全技术基础知识
网络安全·信息安全·软考·高项·加密
深信达沙箱15 天前
如何选择源代码加密软件?应关注哪些核心技术要素
linux·服务器·网络·加密·软件·源代码·沙盒
深信达沙箱16 天前
SDC沙箱能够满足哪些场景需求?
网络·加密·软件·源代码·沙盒
深信达沙箱16 天前
业务系统安全办公沙箱解决方案
网络·系统安全·加密·源代码·沙盒
BOB-wangbaohai17 天前
软考-系统架构师-信息安全技术基础知识(二)
网络安全·软考·加密·系统架构设计师·解密
BOB-wangbaohai18 天前
软考-系统架构师-信息安全技术基础知识(一)
安全·软考·系统架构师·加密·解密