Java Signing certificate

Source: Internet
Author: User

ImportJava.io.FileInputStream;ImportJava.security.KeyStore;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;Importjava.security.Signature;Importjava.security.cert.Certificate;Importjava.security.cert.CertificateFactory;Importjava.security.cert.X509Certificate;ImportJavax.crypto.Cipher; Public classCertificatecoder { Public Static FinalString cert_type= "the"; /*** Get your private key *@paramKeystorepath *@parampwd *@paramalias *@returnPrivatekey Private Key *@throwsException*/    Private StaticPrivatekey Getprivatekey (String keystorepath,string pwd,string alias)throwsexception{KeyStore ks=Getkeystore (Keystorepath, PWD); return(Privatekey) Ks.getkey (alias, Pwd.tochararray ()); }            /**     *      * @paramKeystorepath *@parampwd *@returnkeyStore Key Library *@throwsException*/    Private StaticKeyStore Getkeystore (String keystorepath,string pwd)throwsexception{KeyStore ks=keystore.getinstance (Keystore.getdefaulttype ()); FileInputStream in=NewFileInputStream (Keystorepath);        Ks.load (In,pwd.tochararray ());        In.close (); returnKS; }            /**     *      * @paramCertificatepath *@returnCertificate Certificate *@throwsException*/    Private StaticCertificate getcertificate (String certificatepath)throwsexception{certificatefactory Factory=certificatefactory.getinstance (Cert_type); FileInputStream in=NewFileInputStream (Certificatepath); Certificate Certificate=factory.generatecertificate (in);        In.close (); returncertificate; }            /*** Return the Public key by certificate *@paramCertificatepath *@returnpublickey return Public key *@throwsException*/    Private StaticPublicKey getpublickeybycertificate (String certificatepath)throwsexception{Certificate Certificate=getcertificate (Certificatepath); returnCertificate.getpublickey (); }            /**     *      * @paramKeystorepath *@paramalias *@parampwd *@returnCertificate Certificate *@throwsException*/    Private StaticCertificate getcertificate (String keystorepath,string alias,string pwd)throwsexception{KeyStore ks=Getkeystore (Keystorepath, PWD); //Get a certificate        returnKs.getcertificate (alias); }            /*** Private key encryption *@paramData *@paramKeystorepath *@paramalias *@parampwd *@returnbyte[] Data encrypted by private key *@throwsException*/     Public Static byte[] Encryptbyprivatekey (byte[] data,string keystorepath,string alias,string pwd)throwsexception{Privatekey Privatekey=Getprivatekey (Keystorepath, PWD, alias); //Encrypt the dataCipher cipher=cipher.getinstance (Privatekey.getalgorithm ());        Cipher.init (Cipher.encrypt_mode, Privatekey); returncipher.dofinal (data); }            /*** Private Key decryption *@paramData *@paramKeystorepath *@paramalias *@parampwd *@returnbyte[] Private key decrypted data *@throwsException*/     Public Static byte[] Decryptbyprivatekey (byte[] data,string keystorepath,string alias,string pwd)throwsexception{Privatekey Privatekey=Getprivatekey (Keystorepath, PWD, alias); Cipher Cipher=cipher.getinstance (Privatekey.getalgorithm ()); Cipher.init (cipher.        Decrypt_mode, Privatekey); returncipher.dofinal (data); }            /*** Public Key encryption *@paramData *@paramCerpath *@returnbyte[] Data encrypted by the public key *@throwsException*/     Public Static byte[] Encryptbypublickey (byte[] data,string Cerpath)throwsexception{//get the public keyPublicKey publickey=getpublickeybycertificate (Cerpath);        System.out.println (Publickey.getalgorithm ()); Cipher Cipher=cipher.getinstance (Publickey.getalgorithm ());        Cipher.init (Cipher.encrypt_mode, PublicKey); returncipher.dofinal (data); }        /*** Public Key decryption *@paramData *@paramCerpath *@return     * @throwsException*/     Public Static byte[] Decryptbypublickey (byte[] data,string Cerpath)throwsexception{PublicKey PublicKey=getpublickeybycertificate (Cerpath); Cipher Cipher=cipher.getinstance (Publickey.getalgorithm ());        Cipher.init (Cipher.decrypt_mode, PublicKey); returncipher.dofinal (data); }        /*** Signature *@paramSign *@paramKeystorepath *@parampwd *@paramalias *@return     * @throwsException*/     Public Static byte[] Sign (byte[] sign,string keystorepath,string pwd,string alias)throwsexception{//Get a certificateX509Certificate x509=(X509Certificate) getcertificate (Keystorepath, alias, PWD); //Building a signature, specifying the signature algorithm by certificateSignature sa=signature.getinstance (X509.getsigalgname ()); //Get private keysPrivatekey privatekey=Getprivatekey (Keystorepath, PWD, alias);        Sa.initsign (Privatekey);        Sa.update (sign); returnsa.sign (); }        /*** Verify Signature *@paramData *@paramSign *@paramCerpath *@return     * @throwsException*/     Public Static BooleanVerifybyte[] Data,byte[] sign,string Cerpath)throwsexception{x509certificate x509=(X509Certificate) getcertificate (Cerpath); Signature SA=signature.getinstance (X509.getsigalgname ());        Sa.initverify (X509);        Sa.update (data); returnsa.verify (sign); }}

Java Signing certificate

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.