Java digital signature algorithm-RSA
Signature features:
- Security
- Anti-denial
Digital Signature: a message digest algorithm with a key (Public Key and private key). The algorithm uses the private key for signature and public key for verification)
Digital signature algorithms: RSA, DSA, and ECDSA
Digital signature features:
- Verify Data Integrity
- Authentication data source
- Anti-denial
-
Classic Algorithms
MD and SHA
Digital signature algorithm-RSA Execution Process
Code:
package com.chengxuyuanzhilu.rsa;import java.security.InvalidKeyException;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.NoSuchAlgorithmException;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.SignatureException;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;/** *
Public Account: programmer's path
*
* @ ClassName: CXYZL_RSA * @ Description: array signature algorithm -- RSA * @ author Fu Cheng Gang * @ date February 17, 2016 7:23:02 */public class CXYZL_RSA {// The signature content to be signed and verified private static String src = "chengxuyuanzhilu rsa"; public static void main (String [] args) {jdkRSA ();} public static void jdkRSA () {CXYZL_RSA cxyzl_RSA = new CXYZL_RSA (); try {// 1. initialize the key and generate a public key private key pair Object [] keyPairArr = encrypt (); RSAPublicKey rsaPublicKey = (RSAPublicKey) encrypt [0]; rs?vatekey = // 2. execute the signature byte [] result = cxyzl_RSA.executeSignature (rsw.vatekey); // 3. verify the signature boolean bool = cxyzl_RSA.verifySignature (rsaPublicKey, result); System. out. println ("RSA-MD5withRSA digital signature algorithm Calculation Result:" + bool);} catch (Exception e) {e. printStackTrace () ;}/ *** @ Title: initSecretkey * @ Description: Initialize the key and generate a public key private key pair * @ return Object [] 0 public key, 1 private key * @ author public number: programmer path * @ throws author * @ date 7:31:06 AM on April 9 */private Object [] initSecretkey () throws NoSuchAlgorithmException {KeyPairGenerator keyPairGenerator = KeyPairGenerator. getInstance ("RSA"); keyPairGenerator. initialize (512); KeyPair keyPair = keyPairGenerator. generateKeyPair (); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair. getPublic (); rsw.vatekey = (rsw.vatekey) keyPair. getPrivate (); Object [] keyPairArr = new Object [2]; keyPairArr [0] = rsaPublicKey; keyPairArr [1] = rs?vatekey; return keyPairArr;}/*** @ Title: executeSignature * @ Description: Execute the signature * @ return byte [] The signed content * @ author public number: programmer's path * @ throws InvalidKeyException * @ throws logging * @ throws SignatureException * @ date 7:44:49 on January 1, February 17, 2016 */private byte [] executeSignature, noSuchAlgorithmException, InvalidKeySpecException, SignatureException {PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec (rsw.vatekey. getEncoded (); KeyFactory keyFactory = KeyFactory. getInstance ("RSA"); PrivateKey privateKey = keyFactory. generatePrivate (pkcs8EncodedKeySpec); Signature signature = Signature. getInstance ("MD5withRSA"); signature. initSign (privateKey); signature. update (src. getBytes (); byte [] result = signature. sign (); return result;}/*** @ Title: verifySignature * @ Description: verify the signature * @ param rsaPublicKey Public Key * @ param result the private key executes the signature result * @ return boolean verification result * @ author public number: programmer's path * @ throws handler * @ throws InvalidKeySpecException * @ throws InvalidKeyException * @ throws SignatureException * @ date 7:53:37 AM on April 9 */private boolean verifySignature (RSAPublicKey rsaPublicKey, byte [] result) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec (rsaPublicKey. getEncoded (); KeyFactory keyFactory = KeyFactory. getInstance ("RSA"); PublicKey publicKey = keyFactory. generatePublic (x509EncodedKeySpec); Signature signature = Signature. getInstance ("MD5withRSA"); signature. initVerify (publicKey); signature. update (src. getBytes (); boolean bool = signature. verify (result); return bool ;}}