Java Digital Signature Algorithm--rsa

Source: Internet
Author: User

Signatures have the attributes:

    1. Security
    2. Anti-repudiation

Digital signature : message digest algorithm with key (public key, private key) (signed with private key, validated with public key)

Digital Signature Algorithm:RSA,DSA,ECDSA

Digital Signature Features:

    1. Verifying data integrity
    2. Certified Data sources
    3. Anti-repudiation

Classic Algorithms

MD,SHA two types

the execution process of digital Signature Algorithm -rsa

Code:

 PackageCom.chengxuyuanzhilu.rsa;Importjava.security.InvalidKeyException;Importjava.security.KeyFactory;ImportJava.security.KeyPair;ImportJava.security.KeyPairGenerator;Importjava.security.NoSuchAlgorithmException;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;Importjava.security.Signature;Importjava.security.SignatureException;ImportJava.security.interfaces.RSAPrivateKey;ImportJava.security.interfaces.RSAPublicKey;Importjava.security.spec.InvalidKeySpecException;ImportJava.security.spec.PKCS8EncodedKeySpec;ImportJava.security.spec.X509EncodedKeySpec;/*** <p> Public Number: Programmer's Road </p>* <p> blog:Http://www.cnblogs.com/chengxuyuanzhilu</p>* @ClassName: Cxyzl_rsa * @Description: Array Signature Algorithm--RSA *@authorShenggang * @date February 17, 2016 morning 7:23:02*/  Public classCxyzl_rsa {//signature content to be signed and validated    Private StaticString src = "chengxuyuanzhilu RSA";  Public Static voidMain (string[] args) {jdkrsa (); }         Public Static voidJdkrsa () {Cxyzl_rsa Cxyzl_rsa=NewCxyzl_rsa (); Try {            //1. Initialize the key to generate the public key pairobject[] Keypairarr =Cxyzl_rsa.initsecretkey (); Rsapublickey Rsapublickey= (Rsapublickey) keypairarr[0]; Rsaprivatekey Rsaprivatekey= (Rsaprivatekey) keypairarr[1]; //2. Execute Signature            byte[] result =cxyzl_rsa.executesignature (Rsaprivatekey); //3. Verifying the signature            BooleanBOOL =cxyzl_rsa.verifysignature (Rsapublickey,result); System.out.println ("Rsa-md5withrsa Digital Signature Algorithm operation result:" +bool); } Catch(Exception e) {e.printstacktrace (); }    }        /*** @Title: Initsecretkey * @Description: Initialize key, generate public key pair *@returnobject[] 0 public key, 1 private key *@authorPublic Number: Programmer's Road *@throwsnosuchalgorithmexception * @date February 17, 2016 morning 7:31:06*/    PrivateObject[] Initsecretkey ()throwsnosuchalgorithmexception {keypairgenerator keypairgenerator= Keypairgenerator.getinstance ("RSA"); Keypairgenerator.initialize (512); KeyPair KeyPair=Keypairgenerator.generatekeypair (); Rsapublickey Rsapublickey=(Rsapublickey) keypair.getpublic (); Rsaprivatekey Rsaprivatekey=(Rsaprivatekey) keypair.getprivate (); Object[] Keypairarr=NewObject[2]; keypairarr[0] =Rsapublickey; keypairarr[1] =Rsaprivatekey; returnKeypairarr; }        /*** @Title: Executesignature * @Description: Execute signature *@returnbyte[] Post-signature content *@authorPublic Number: Programmer's Road *@throwsInvalidKeyException *@throwsnosuchalgorithmexception *@throwsinvalidkeyspecexception *@throwssignatureexception * @date February 17, 2016 morning 7:44:49*/    Private byte[] Executesignature (Rsaprivatekey rsaprivatekey)throwsinvalidkeyexception, NoSuchAlgorithmException, invalidkeyspecexception, signatureexception{PKCS8EncodedKeySpe C Pkcs8encodedkeyspec=NewPkcs8encodedkeyspec (rsaprivatekey.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 (); returnresult; }        /*** @Title: VerifySignature * @Description: Verify signature *@paramRsapublickey Public Key *@paramresult the private key executes the signature results *@returnBoolean validation result *@authorPublic Number: Programmer's Road *@throwsnosuchalgorithmexception *@throwsinvalidkeyspecexception *@throwsInvalidKeyException *@throwssignatureexception * @date February 17, 2016 morning 7:53:37*/    Private BooleanVerifySignature (Rsapublickey Rsapublickey,byte[] result)throwsnosuchalgorithmexception, Invalidkeyspecexception, invalidkeyexception, signatureexception{X509EncodedKeySpec X509encodedkeyspec=NewX509encodedkeyspec (rsapublickey.getencoded ()); Keyfactory keyfactory= Keyfactory.getinstance ("RSA"); PublicKey PublicKey=keyfactory.generatepublic (X509ENCODEDKEYSPEC); Signature Signature= Signature.getinstance ("Md5withrsa");        Signature.initverify (PublicKey);        Signature.update (Src.getbytes ()); BooleanBOOL =signature.verify (Result); returnbool; }        }

Java Digital Signature Algorithm--rsa

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.