Java digital signature algorithm-RSA

Source: Internet
Author: User

Java digital signature algorithm-RSA

Signature features:

  1. Security
  2. 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:

  1. Verify Data Integrity
  2. Authentication data source
  3. Anti-denial
  4.  

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 ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.