数字签名学习

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

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

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

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

相关推荐
KWMax11 天前
RSA加密原理及推导
加密·rsa
亚林瓜子12 天前
设置AWS EC2默认使用加密磁盘
云计算·磁盘·aws·加密
GettingReal14 天前
Python 构建壳来启动加密的 SpringBoot Jar 包,增加反编译难度
spring boot·python·jar·加密
weiwei2284414 天前
secp256k1算法详解一
区块链·数字签名·源码编译·elliptic curve
liulilittle19 天前
通过高级处理器硬件指令集AES-NI实现AES-256-CFB算法并通过OPENSSL加密验证算法正确性。
linux·服务器·c++·算法·安全·加密·openssl
liulilittle21 天前
OpenSSL 的 AES-NI 支持机制
linux·运维·服务器·算法·加密·openssl·解密
liulilittle22 天前
通过高级处理器硬件指令集AES-NI实现AES-256-CFB算法。
linux·服务器·c++·算法·安全·加密·openssl
佛祖让我来巡山1 个月前
【非对称加密】详解及Java实现
非对称加密·数字签名·rsa
老友@1 个月前
Spring Boot 应用中实现配置文件敏感信息加密解密方案
java·spring boot·后端·数据安全·加密·配置文件