Signatures have the attributes:
- Security
- 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:
- Verifying data integrity
- Certified Data sources
- 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