rsa数字签名c语言编程,RSA数字签名 - hahahaha123的个人空间 - OSCHINA - 中文开源技术交流社区...

本文详细介绍了RSA数字签名的原理和步骤,包括使用私钥生成签名以及使用公钥验证签名的过程。通过Java代码示例展示了MD5withRSA和SHA1withRSA两种方式的签名与验证操作,强调了内容摘要在签名过程中的重要作用。
摘要由CSDN通过智能技术生成

加密算法

一、简介

数字签名用于验证消息发送者的身份,确保其他人无法伪造身份。

二、原理

数字签名基于非对称加密算法,利用只有拥有者才有私钥的特性(这可以标识身份)进行的。

1、数字签名的生成

对发送内容先生成有限长度的摘要,再使用私钥进行加密,进而生成数字签名。

2、数字签名验证

用公钥对数字签名进行解密获取加密内容(其实也就是摘要),再用与发送方相同的摘要算法对发送内空生成摘要,

再将这两者进行比较,若相等,则验证成功,否则失败。

三、代码实例

在此使用java自带的数字签名api进行演示,包括MD5withRSA和SHA1withRSA两种方式,签名使用base64编码

public class DigitalSignatureMain {

public static void main(String[] args)  throws Exception {

String content = "study hard and make progress everyday";

System.out.println("content :"+content);

KeyPair keyPair = getKeyPair();

PublicKey publicKey =  keyPair.getPublic();

PrivateKey privateKey = keyPair.getPrivate();

String md5Sign  = getMd5Sign(content,privateKey);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA算法数字签名C语言编程实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/err.h> #define KEY_LENGTH 2048 #define BLOCK_SIZE KEY_LENGTH/8-11 int main(){ RSA *keypair; unsigned char plain_text[100] = "Hello, RSA!"; unsigned char cipher_text[KEY_LENGTH/8]; unsigned char decrypted_text[KEY_LENGTH/8]; unsigned char signature[KEY_LENGTH/8]; unsigned int signature_len; int decrypted_len; // 生成 RSA 密钥对 keypair = RSA_generate_key(KEY_LENGTH, RSA_F4, NULL, NULL); // 使用私钥签名 if (RSA_sign(NID_sha256, plain_text, strlen(plain_text), signature, &signature_len, keypair) != 1) { fprintf(stderr, "RSA sign error!\n"); RSA_free(keypair); return -1; } // 使用公钥验签 if (RSA_verify(NID_sha256, plain_text, strlen(plain_text), signature, signature_len, keypair) != 1) { fprintf(stderr, "RSA verify error!\n"); RSA_free(keypair); return -1; } // 使用公钥加密 int cipher_len = RSA_public_encrypt(strlen(plain_text), plain_text, cipher_text, keypair, RSA_PKCS1_PADDING); if (cipher_len == -1) { fprintf(stderr, "RSA public encrypt error!\n"); RSA_free(keypair); return -1; } printf("Cipher text: "); for (int i = 0; i < cipher_len; i++) { printf("%02x", cipher_text[i]); } printf("\n"); // 使用私钥解密 decrypted_len = RSA_private_decrypt(cipher_len, cipher_text, decrypted_text, keypair, RSA_PKCS1_PADDING); if (decrypted_len == -1) { fprintf(stderr, "RSA private decrypt error!\n"); RSA_free(keypair); return -1; } decrypted_text[decrypted_len] = '\0'; printf("Decrypted text: %s\n", decrypted_text); RSA_free(keypair); return 0; } ``` 需要注意的是,这里用到了 OpenSSL 库,需要在编译时链接该库,同时需要在代码中包含相关的头文件。此外,为了方便,这里的签名、验签、加密、解密都是在同一个程序中实现,实际应用中可能需要拆分成不同的模块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值