java簽署憑證

來源:互聯網
上載者:User

標籤:

import java.io.FileInputStream;import java.security.KeyStore;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.cert.Certificate;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import javax.crypto.Cipher;public class CertificateCoder {    public static final String CERT_TYPE="X.509";                /**     * 擷取私匙     * @param keyStorePath     * @param pwd     * @param alias     * @return PrivateKey 私匙     * @throws Exception     */    private static PrivateKey getPrivateKey(String keyStorePath,String pwd,String alias) throws Exception{        KeyStore ks=getKeyStore(keyStorePath, pwd);        return (PrivateKey)ks.getKey(alias, pwd.toCharArray());            }            /**     *      * @param keyStorePath     * @param pwd     * @return keyStore 密匙庫     * @throws Exception     */    private static KeyStore getKeyStore(String keyStorePath,String pwd) throws Exception{        KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());        FileInputStream in=new FileInputStream(keyStorePath);        ks.load(in,pwd.toCharArray());        in.close();        return ks;    }            /**     *      * @param certificatePath     * @return Certificate 認證     * @throws Exception     */    private static Certificate getCertificate(String certificatePath) throws Exception{        CertificateFactory factory=CertificateFactory.getInstance(CERT_TYPE);        FileInputStream in=new FileInputStream(certificatePath);        Certificate certificate=factory.generateCertificate(in);        in.close();        return certificate;            }            /**     * 通過認證返回公匙     * @param certificatePath     * @return Publickey 返回公匙     * @throws Exception     */    private static PublicKey getPublicKeyByCertificate(String certificatePath) throws Exception{        Certificate certificate=getCertificate(certificatePath);        return certificate.getPublicKey();    }            /**     *      * @param keyStorePath     * @param alias     * @param pwd     * @return Certificate 認證     * @throws Exception     */    private static Certificate getCertificate(String keyStorePath,String alias,String pwd) throws Exception{        KeyStore ks=getKeyStore(keyStorePath, pwd);        //擷取認證        return ks.getCertificate(alias);    }            /**     * 私匙加密      * @param data     * @param keyStorePath     * @param alias     * @param pwd     * @return byte[] 被私匙加密的資料     * @throws Exception     */    public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String pwd) throws Exception{        PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);        //對資料進行加密        Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());        cipher.init(Cipher.ENCRYPT_MODE, privateKey);        return cipher.doFinal(data);                    }            /**     * 私匙解密     * @param data     * @param keyStorePath     * @param alias     * @param pwd     * @return byte[] 私匙解密的資料     * @throws Exception     */    public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String pwd) throws Exception{        PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);        Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());        cipher.init(cipher.DECRYPT_MODE, privateKey);        return cipher.doFinal(data);    }            /**     * 公匙加密     * @param data     * @param cerPath     * @return byte[] 被公匙加密的資料     * @throws Exception     */    public static byte[] encryptByPublicKey(byte[] data,String cerPath) throws Exception{        //擷取公匙        PublicKey publicKey=getPublicKeyByCertificate(cerPath);        System.out.println(publicKey.getAlgorithm());        Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());        cipher.init(Cipher.ENCRYPT_MODE, publicKey);        return cipher.doFinal(data);    }        /**     * 公匙解密     * @param data     * @param cerPath     * @return     * @throws Exception     */    public static byte[] decryptByPublicKey(byte[] data,String cerPath) throws Exception{        PublicKey publicKey=getPublicKeyByCertificate(cerPath);        Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());        cipher.init(Cipher.DECRYPT_MODE, publicKey);        return cipher.doFinal(data);    }        /**     * 簽名     * @param sign     * @param keyStorePath     * @param pwd     * @param alias     * @return     * @throws Exception     */    public static byte[] sign(byte[] sign,String keyStorePath,String pwd,String alias) throws Exception{        //擷取認證        X509Certificate x509=(X509Certificate)getCertificate(keyStorePath, alias, pwd);        //構建簽名,由認證指定簽名演算法        Signature sa=Signature.getInstance(x509.getSigAlgName());        //擷取私匙        PrivateKey privateKey=getPrivateKey(keyStorePath, pwd, alias);        sa.initSign(privateKey);        sa.update(sign);        return sa.sign();    }        /**     * 驗證簽名     * @param data     * @param sign     * @param cerPath     * @return     * @throws Exception     */    public static boolean verify(byte[] data,byte[] sign,String cerPath) throws Exception{        X509Certificate x509=(X509Certificate)getCertificate(cerPath);        Signature sa=Signature.getInstance(x509.getSigAlgName());        sa.initVerify(x509);        sa.update(data);        return sa.verify(sign);    }}

 

java簽署憑證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.