CertificateCoder { public static String sign(byte[] sign, String keyStorePath, String alias, String password) throws Exception { X509Certificate x509Certificate = (X509Certificate) getCertificate( keyStorePath, alias, password); KeyStore ks = getKeyStore(keyStorePath, password); PrivateKey privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray()); Signature signature = Signature.getInstance(x509Certificate .getSigAlgName()); signature.initSign(privateKey); signature.update(sign); return Base64.encode(signature.sign()); }  static byte[] encryptByPrivateKey(byte[] data, String keyStorePath,
			String alias, String password); static byte[] decryptByPrivateKey(byte[] data, String keyStorePath,
			String alias, String password); static byte[] encryptByPublicKey(byte[] data, String certificatePath); static byte[] decryptByPublicKey(byte[] data, String certificatePath); static boolean verifyCertificate(String certificatePath); static boolean verifyCertificate(Date date, String certificatePath); static String sign(byte[] sign, String keyStorePath, String alias,
			String password); static boolean verify(byte[] data, String sign,
			String certificatePath); static boolean verifyCertificate(Date date, String keyStorePath,
			String alias, String password); static boolean verifyCertificate(String keyStorePath, String alias,
			String password); static final String KEY_STORE; static final String X509; }
@Test public void testSign() throws Exception { System.err.println("私钥加密——公钥解密"); String inputStr = "sign"; byte[] data = inputStr.getBytes(); byte[] encodedData = CertificateCoder.encryptByPrivateKey(data, keyStorePath, alias, password); byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData, certificatePath); String outputStr = new String(decodedData); System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr); assertEquals(inputStr, outputStr); System.err.println("私钥签名——公钥验证签名"); String sign = CertificateCoder.sign(encodedData, keyStorePath, alias, password); System.err.println("签名:\r" + sign); boolean status = CertificateCoder.verify(encodedData, sign, certificatePath); System.err.println("状态:\r" + status); assertTrue(status); }