Guo Jia
Email: [Email protected]
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwell
The following demonstrates the process of creating a certificate chain and validating a certificate chain under the JCP API.
PackageCom.allenwells.jce;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;/** * Certificate Component * * @author allenwells * @version 1.0 * * Public Abstract class certificateencryptor{ /** * Type Certificate X509 * / Public Static FinalString Cert_type =" the";/** * Get private key by KeyStore * * @param keystorepath * KeyStore path * @param Alias * Alias * @param Password * password * @return privatekey private key * @thro WS Exception * / Private StaticPrivatekeyGetprivatekeybykeystore(string Keystorepath, string alias, string password)throwsException {//Get KeyStoreKeyStore ks = Getkeystore (keystorepath, password);//Get private key return(Privatekey) Ks.getkey (alias, Password.tochararray ()); }/** * obtained public key by certificate * * @param Certificatepath * Certificate path * @return P Ublickey Public Key * @throws Exception */ Private StaticPublicKeygetpublickeybycertificate(String Certificatepath)throwsException {//Get a certificateCertificate Certificate = getcertificate (Certificatepath);//Get public key returnCertificate.getpublickey (); }/** * Get certificate * * @param Certificatepath * Certificate path * @return Cert Ificate certificate * @throws Exception * / Private StaticCertificategetcertificate(String Certificatepath)throwsException {//Instantiate certificate factoryCertificatefactory certificatefactory = certificatefactory. getinstance (Cert_type);//Get certificate file streamFileInputStream in =NewFileInputStream (Certificatepath);//Generate CertificatesCertificate Certificate = Certificatefactory.generatecertificate (in);//Close certificate file streamIn.close ();returnCertificate }/** * Get certificate * * @param keystorepath * KeyStore path * @param Alias * Alias * @param Password * password * @return Certificate certificate * @th Rows Exception * / Private StaticCertificategetcertificate(string Keystorepath, string alias, string password)throwsException {//Get KeyStoreKeyStore ks = Getkeystore (keystorepath, password);//Get a certificate returnKs.getcertificate (alias); }/** * Get keystore * * @param keystorepath * KeyStore path * @param Password * Password * @return KeyStore keystore * @throws Exception */ Private StaticKeyStoreGetkeystore(string Keystorepath, string password)throwsException {//Instantiate KeyStoreKeyStore ks = Keystore.getinstance (Keystore.getdefaulttype ());//Get KeyStore file streamFileInputStream is =NewFileInputStream (Keystorepath);//Load KeyStoreKs.load (IS, Password.tochararray ());//close KeyStore file streamIs.close ();returnks }/** * Private key encryption * * @param Data * Pending encryption * @param Keystorepath * KeyStore path * @param alias * alias * @param Password * password * @ Return byte[] Encrypt data * @throws Exception */ Public Static byte[]Encryptbyprivatekey(byte[] Data, String Keystorepath, string alias, string password)throwsException {//Get the private keyPrivatekey Privatekey = Getprivatekeybykeystore (Keystorepath, alias, password);//Encryption of dataCipher Cipher = cipher.getinstance (Privatekey.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, Privatekey);returnCipher.dofinal (data); }/** * Private Key decryption * * @param data * To be decrypted * @param Keystorepath * KeyStore path * @param alias * alias * @param Password * password * @ Return byte[] Decrypt data * @throws Exception * / Public Static byte[]Decryptbyprivatekey(byte[] Data, String Keystorepath, string alias, string password)throwsException {//Get the private keyPrivatekey Privatekey = Getprivatekeybykeystore (Keystorepath, alias, password);//Encryption of dataCipher Cipher = cipher.getinstance (Privatekey.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, Privatekey);returnCipher.dofinal (data); }/** * Public Key Encryption * * @param data * To be encrypted * @param Certificatepath * Certificate path * @return byte[] Encrypt data * @throws Exception */ Public Static byte[]Encryptbypublickey(byte[] data, String Certificatepath)throwsException {//Get public keyPublicKey PublicKey = getpublickeybycertificate (Certificatepath);//Encryption of dataCipher Cipher = cipher.getinstance (Publickey.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, PublicKey);returnCipher.dofinal (data); }/** * Public Key decryption * * @param data * To be decrypted * @param Certificatepath * Certificate path * @return byte[] Decrypt data * @throws Exception */ Public Static byte[]Decryptbypublickey(byte[] data, String Certificatepath)throwsException {//Get public keyPublicKey PublicKey = getpublickeybycertificate (Certificatepath);//Encryption of dataCipher Cipher = cipher.getinstance (Publickey.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, PublicKey);returnCipher.dofinal (data); }/** * Signature * * @param keystorepath * KeyStore path * @param alias * Alias * @param Password * password * @return byte[] Signature * @throws Excepti On * / Public Static byte[] Sign(byte[] Sign, String Keystorepath, string alias, string password)throwsException {//Get a certificateX509Certificate x509certificate = (x509certificate) getcertificate (Keystorepath, alias, password);//Build signature, specify Signature algorithm by certificateSignature Signature = signature.getinstance (x509certificate. Getsigalgname ());//Get private keyPrivatekey Privatekey = Getprivatekeybykeystore (Keystorepath, alias, password);//Initialize signature, built by private keySignature.initsign (Privatekey); Signature.update (sign);returnSignature.sign (); }/** * Verify signature * * @param Data * * @param Sign * * @param Certificatepath * Certificate path * @return Boolean validation passed as True * @throws EXC Eption * * Public Static Boolean Verify(byte[] Data,byte[] sign, String Certificatepath)throwsException {//Get a certificateX509Certificate x509certificate = (x509certificate) getcertificate (Certificatepath);//Build signature by CertificateSignature Signature = signature.getinstance (x509certificate. Getsigalgname ());//signed by certificate, actually using the public key in the certificateSignature.initverify (X509Certificate); Signature.update (data);returnSignature.verify (sign); }}
"Java Security Technology Exploration Road Series: Java Extensible Security Architecture" Ten: JCP (iii): JCP programming model