在Java加密技術(八)中
我們需要構建一個由CA機構簽發的有效證書
這裡
Shell代碼
keytool
其中
在這裡我使用的密碼為
控制台輸出
Console代碼
輸入keystore密碼
再次輸入新密碼:
所有者:CN=
簽發人:CN=
序列號:
有效期: Thu May
證書指紋:
MD
SHA
簽名算法名稱:SHA
版本:
信任這個認證? [否]
認證已添加至keystore中
OK
接下來我們將域名定位到本機上
現在
Xml代碼
<Connector
SSLEnabled=
URIEncoding=
clientAuth=
keystoreFile=
keystorePass=
maxThreads=
port=
protocol=
scheme=
secure=
sslProtocol=
注意clientAuth=
顯然
這個時候很多人開始懷疑
接著上篇內容
Java代碼
import java
import java
import java
import java
import java
import java
import java
import java
import java
import javax
import
import
import
import
import
/**
* 證書組件
*
* @author 梁棟
* @version
* @since
*/
public abstract class CertificateCoder extends Coder {
/**
* Java密鑰庫(Java Key Store
*/
public static final String KEY_STORE =
public static final String X
public static final String SunX
public static final String SSL =
/**
* 由KeyStore獲得私鑰
*
* @param keyStorePath
* @param alias
* @param password
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKey(String keyStorePath
String password) throws Exception {
KeyStore ks = getKeyStore(keyStorePath
PrivateKey key = (PrivateKey) ks
return key;
}
/**
* 由Certificate獲得公鑰
*
* @param certificatePath
* @return
* @throws Exception
*/
private static PublicKey getPublicKey(String certificatePath)
throws Exception {
Certificate certificate = getCertificate(certificatePath);
PublicKey key = certificate
return key;
}
/**
* 獲得Certificate
*
* @param certificatePath
* @return
* @throws Exception
*/
private static Certificate getCertificate(String certificatePath)
throws Exception {
CertificateFactory certificateFactory = CertificateFactory
FileInputStream in = new FileInputStream(certificatePath);
Certificate certificate = certificateFactory
in
return certificate;
}
/**
* 獲得Certificate
*
* @param keyStorePath
* @param alias
* @param password
* @return
* @throws Exception
*/
private static Certificate getCertificate(String keyStorePath
String alias
KeyStore ks = getKeyStore(keyStorePath
Certificate certificate = ks
return certificate;
}
/**
* 獲得KeyStore
*
* @param keyStorePath
* @param password
* @return
* @throws Exception
*/
private static KeyStore getKeyStore(String keyStorePath
throws Exception {
FileInputStream is = new FileInputStream(keyStorePath);
KeyStore ks = KeyStore
ks
is
return ks;
}
/**
* 私鑰加密
*
* @param data
* @param keyStorePath
* @param alias
* @param password
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data
String alias
// 取得私鑰
PrivateKey privateKey = getPrivateKey(keyStorePath
// 對數據加密
Cipher cipher = Cipher
cipher
return cipher
}
/**
* 私鑰解密
*
* @param data
* @param keyStorePath
* @param alias
* @param password
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data
String alias
// 取得私鑰
PrivateKey privateKey = getPrivateKey(keyStorePath
// 對數據加密
Cipher cipher = Cipher
cipher
return cipher
}
/**
* 公鑰加密
*
* @param data
* @param certificatePath
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data
throws Exception {
// 取得公鑰
PublicKey publicKey = getPublicKey(certificatePath);
// 對數據加密
Cipher cipher = Cipher
cipher
return cipher
}
/**
* 公鑰解密
*
* @param data
* @param certificatePath
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data
throws Exception {
// 取得公鑰
PublicKey publicKey = getPublicKey(certificatePath);
// 對數據加密
Cipher cipher = Cipher
cipher
return cipher
}
/**
* 驗證Certificate
*
* @param certificatePath
* @return
*/
public static boolean verifyCertificate(String certificatePath) {
return verifyCertificate(new Date()
}
/**
* 驗證Certificate是否過期或無效
*
* @param date
* @param certificatePath
* @return
*/
public static boolean verifyCertificate(Date date
boolean status = true;
try {
// 取得證書
Certificate certificate = getCertificate(certificatePath);
// 驗證證書是否過期或無效
status = verifyCertificate(date
} catch (Exception e) {
status = false;
}
return status;
}
/**
* 驗證證書是否過期或無效
*
* @param date
* @param certificate
* @return
*/
private static boolean verifyCertificate(Date date
boolean status = true;
try {
X
x
} catch (Exception e) {
status = false;
}
return status;
}
/**
* 簽名
*
* @param keyStorePath
* @param alias
* @param password
*
* @return
* @throws Exception
*/
public static String sign(byte[] sign
String password) throws Exception {
// 獲得證書
X
keyStorePath
// 獲取私鑰
KeyStore ks = getKeyStore(keyStorePath
// 取得私鑰
PrivateKey privateKey = (PrivateKey) ks
// 構建簽名
Signature signature = Signature
signature
signature
return encryptBASE
}
/**
* 驗證簽名
*
* @param data
* @param sign
* @param certificatePath
* @return
* @throws Exception
*/
public static boolean verify(byte[] data
String certificatePath) throws Exception {
// 獲得證書
X
// 獲得公鑰
PublicKey publicKey = x
// 構建簽名
Signature signature = Signature
signature
signature
return signature
}
/**
* 驗證Certificate
*
* @param keyStorePath
* @param alias
* @param password
* @return
*/
public static boolean verifyCertificate(Date date
String alias
boolean status = true;
try {
Certificate certificate = getCertificate(keyStorePath
password);
status = verifyCertificate(date
} catch (Exception e) {
status = false;
}
return status;
}
/**
* 驗證Certificate
*
* @param keyStorePath
* @param alias
* @param password
* @return
*/
public static boolean verifyCertificate(String keyStorePath
String password) {
return verifyCertificate(new Date()
}
/**
* 獲得SSLSocektFactory
*
* @param password
* 密碼
* @param keyStorePath
* 密鑰庫路徑
*
* @param trustKeyStorePath
* 信任庫路徑
* @return
* @throws Exception
*/
private static SSLSocketFactory getSSLSocketFactory(String password
String keyStorePath
// 初始化密鑰庫
KeyManagerFactory keyManagerFactory = KeyManagerFactory
KeyStore keyStore = getKeyStore(keyStorePath
keyManagerFactory
// 初始化信任庫
TrustManagerFactory trustManagerFactory = TrustManagerFactory
KeyStore trustkeyStore = getKeyStore(trustKeyStorePath
trustManagerFactory
// 初始化SSL上下文
SSLContext ctx = SSLContext
ctx
SSLSocketFactory sf = ctx
return sf;
}
/**
* 為HttpsURLConnection配置SSLSocketFactory
*
* @param conn
* HttpsURLConnection
* @param password
* 密碼
* @param keyStorePath
* 密鑰庫路徑
*
* @param trustKeyStorePath
* 信任庫路徑
* @throws Exception
*/
public static void configSSLSocketFactory(HttpsURLConnection conn
String password
throws Exception {
conn
trustKeyStorePath));
}
}
增加了configSSLSocketFactory方法供外界調用
給出相應測試類
Java代碼
import static org
import java
import java
import
import
import org
/**
*
* @author 梁棟
* @version
* @since
*/
public class CertificateCoderTest {
private String password =
private String alias =
private String certificatePath =
private String keyStorePath =
private String clientKeyStorePath =
private String clientPassword =
@Test
public void test() throws Exception {
System
String inputStr =
byte[] data = inputStr
byte[] encrypt = CertificateCoder
certificatePath);
byte[] decrypt = CertificateCoder
keyStorePath
String outputStr = new String(decrypt);
System
// 驗證數據一致
assertArrayEquals(data
// 驗證證書有效
assertTrue(CertificateCoder
}
@Test
public void testSign() throws Exception {
System
String inputStr =
byte[] data = inputStr
byte[] encodedData = CertificateCoder
keyStorePath
byte[] decodedData = CertificateCoder
certificatePath);
String outputStr = new String(decodedData);
System
assertEquals(inputStr
System
// 產生簽名
String sign = CertificateCoder
password);
System
// 驗證簽名
boolean status = CertificateCoder
certificatePath);
System
assertTrue(status);
}
@Test
public void testHttps() throws Exception {
URL url = new URL(
HttpsURLConnection conn = (HttpsURLConnection) url
conn
conn
nfigSSLSocketFactory(conn
clientKeyStorePath
InputStream is = conn
int length = conn
DataInputStream dis = new DataInputStream(is);
byte[] data = new byte[length];
dis
dis
System
conn
}
}
注意testHttps方法
Console代碼
<!
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements
this work for additional information regarding copyright ownership
The ASF licenses this file to You under the Apache License
(the
the License
Unless required by applicable law or agreed to in writing
distributed under the License is distributed on an
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
See the License for the specific language governing permissions and
limitations under the License
<!DOCTYPE HTML PUBLIC
<HTML><HEAD><TITLE>Apache Tomcat Examples</TITLE>
<META http
</HEAD>
<BODY>
<P>
<H
<P></P>
<ul>
<li><a >Servlets examples</a></li>
<li><a >JSP Examples</a></li>
</ul>
</BODY></HTML>
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27353.html