经典数字签名算法---RSA
RSA数字签名算法主要可以分为MD系列和SHA系列两大类。
MD系列:MD2withRSA、MD5withRSA。
SHA系列:SHA1withRSA、SHA224withRSA、SHA256withRSA、SHA384withRSA、SHA512withRSA。
package com.antfin.jdk8.sign; import java.security.*; public class SHA256withRSA { private static final String KEY_ALGORITHM = "RSA"; private static final String SIGN_ALGORITHM = "SHA256withRSA"; private static final String ENCODING = "UTF-8"; private static final int KEY_SIZE = 2048; public static void main(String[] args) throws Exception { String data = "待签名的数据"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); Signature signature = Signature.getInstance(SIGN_ALGORITHM); signature.initSign(privateKey); signature.update(data.getBytes(ENCODING)); byte[] bytesSign = signature.sign(); //签名 signature.initVerify(publicKey); signature.update(data.getBytes(ENCODING)); boolean flag = signature.verify(bytesSign); //验签 System.out.println("flag: "+flag); } }
posted on 2019-02-27 01:15 shammgod_code 阅读(453) 评论(0) 编辑 收藏 举报