美文网首页
[ 加密 ] RSA: 想不到 我也可以数字签名

[ 加密 ] RSA: 想不到 我也可以数字签名

作者: 一个好汉 | 来源:发表于2021-09-13 12:50 被阅读0次

不好意思 加密签名我都要

私钥加密得到的密文实际上就是数字签名,要验证这个签名是否正确,只能用私钥持有者的公钥进行解密验证。使用数字签名的目的是为了确认某个信息确实是由某个发送方发送的,任何人都不可能伪造消息,并且,发送方也不能抵赖

防止抵赖


import javax.crypto.Cipher;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.*;

/**
 * RSA 公认目前最优秀的公钥方案之一
 */
public class RsaEncrypt {
    public static void main(String[] args) throws Exception {
        // 明文:
        byte[] plain = "Hello, encrypt use RSA".getBytes(StandardCharsets.UTF_8);
        // 创建公钥/私钥对:
        Partner alice = new Partner("Alice");
        // 用Alice的公钥加密:
        byte[] pk = alice.getPublicKey();
        System.out.println(String.format("public key: %x", new BigInteger(1, pk)));
        byte[] encrypted = alice.encrypt(plain);
        System.out.println(String.format("encrypted: %x", new BigInteger(1, encrypted)));
        // 用Alice的私钥解密:
        byte[] sk = alice.getPrivateKey();
        System.out.println(String.format("private key: %x", new BigInteger(1, sk)));
        byte[] decrypted = alice.decrypt(encrypted);
        System.out.println(new String(decrypted, StandardCharsets.UTF_8));
    }

    public static void sha1withRSA() throws GeneralSecurityException {
        // 生成RSA公钥/私钥:
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
        kpGen.initialize(1024);
        KeyPair kp = kpGen.generateKeyPair();
        PrivateKey sk = kp.getPrivate();
        PublicKey pk = kp.getPublic();

        // 待签名的消息:
        byte[] message = "Hello, I am Bob!".getBytes(StandardCharsets.UTF_8);

        // 用私钥签名:
        Signature s = Signature.getInstance("SHA1withRSA");
        s.initSign(sk);
        s.update(message);
        byte[] signed = s.sign();
        System.out.println(String.format("signature: %x", new BigInteger(1, signed)));

        // 用公钥验证:
        Signature v = Signature.getInstance("SHA1withRSA");
        v.initVerify(pk);
        v.update(message);
        boolean valid = v.verify(signed);
        System.out.println("valid? " + valid);
    }
}

class Partner {
    String name;
    // 私钥:
    PrivateKey sk;
    // 公钥:
    PublicKey pk;

    public Partner(String name) throws GeneralSecurityException {
        this.name = name;
        // 生成公钥/私钥对:
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
        kpGen.initialize(1024);
        KeyPair kp = kpGen.generateKeyPair();
        this.sk = kp.getPrivate();
        this.pk = kp.getPublic();
    }

    // 把私钥导出为字节
    public byte[] getPrivateKey() {
        return this.sk.getEncoded();
    }

    // 把公钥导出为字节
    public byte[] getPublicKey() {
        return this.pk.getEncoded();
    }

    // 用公钥加密:
    public byte[] encrypt(byte[] message) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, this.pk);
        return cipher.doFinal(message);
    }

    // 用私钥解密:
    public byte[] decrypt(byte[] input) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, this.sk);
        return cipher.doFinal(input);
    }
}

我方持有私钥
我认为
调用方调用之后 我方可用rsa进行签名加密后
返回调用方 告知调用方 调用方可做进一步验证

相关文章

  • [ 加密 ] RSA: 想不到 我也可以数字签名

    不好意思 加密签名我都要 私钥加密得到的密文实际上就是数字签名,要验证这个签名是否正确,只能用私钥持有者的公钥进行...

  • 数字证书和数字签名

    数字签名是什么?RSA算法原理(一)RSA算法原理(二)RSA数字签名与加密、解密间的关系 在网络通信中,可以通过...

  • Python RSA数字签名实践

    之前讲到RSA可以用来加密和数字签名,这里是RSA用作数字签名。Python的pycrypto库实现的数字签名有一...

  • 4.2 RSA数字签名技术

    数字签名技术 - RSA数字签名技术 RSA算法不仅是非对称加密算法,也是数字签名算法中的主力军,和MD、SHA系...

  • RSA,DES加解密

    RSA加密算法是一种非对称加密算法RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作 DES全称为...

  • iOS RSA加解密以及签名验签

    初步认识: 一、RSA 1、RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。2、RSA是被研究...

  • VC++实现信息加密聊天,未来信息安全将再提高一个档次!

    本聊天程序采用2048bit的RSA数据加密算法对数据进行加密和数字签名后发出,因为RSA加密信息需要大量的时间,...

  • iOS--RSA加密

    一、RSA 1、RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。 2、RSA是被研究得最广泛的...

  • RSA

    RSA RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。RSA是被研究得最广泛的公钥算法,从提...

  • 数字签名 数字证书 和https

    在双钥体系中,公钥用来加密信息,私钥用来数字签名。 数字签名 RSA 加密保障了信息的机密性,但无法保证信息的完整...

网友评论

      本文标题:[ 加密 ] RSA: 想不到 我也可以数字签名

      本文链接:https://www.haomeiwen.com/subject/zcrswltx.html