67,513
社区成员
发帖
与我相关
我的任务
分享
/**
* 加密。注意密钥是可以获取到它适用的算法的。
*
* @param plainText
* @param privateKey
* @return
*/
public static byte[] encrypt(PrivateKey privateKey, byte[] data) {
try {
Cipher cipher=Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
//数据长度
int inputLen=data.length;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
//
int offSet=0;
byte[] cache;
int i=0;
while(inputLen-offSet>0){
if(inputLen-offSet>117){
cache=cipher.doFinal(data, offSet, 117);
}else{
cache=cipher.doFinal(data, offSet, inputLen-offSet);
}
baos.write(cache,0,cache.length);
i++;
offSet=i*117;
}
byte[] encryptedData=baos.toByteArray();
baos.close();
return encryptedData;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param encodedText
* @param publicKey
* @return
*/
public static byte[] decode(byte[] encodedText, PublicKey publicKey) {
try {
Cipher cipher=Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
int inputLen=encodedText.length;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int offSet=0;
byte[] cache;
int i=0;
while(inputLen-offSet>0){
if(inputLen-offSet>128){
cache=cipher.doFinal(encodedText, offSet, 128);
}else{
cache=cipher.doFinal(encodedText, offSet, inputLen-offSet);
}
baos.write(cache, 0, cache.length);
i++;
offSet=i*128;
}
byte[] decryptedData=baos.toByteArray();
baos.close();
return decryptedData;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public class Test {
// 密钥库密码
private static final String STORE_PASS = "Changeme123";
// 证书别名
private static final String ALIAS = "myCertificate";
// 服务器本地数字证书
private static final String KEYSTORE_PATH = "C:\\Users\\Administrator\\myKeystore.keystore";
// 数字证书(需要发送给客户端)
private static final String CERT_PATH = "C:\\Users\\Administrator\\myCer.cer";
// JDK6只支持X.509标准的证书
private static final String CERT_TYPE = "X.509";
public static void main(String[] args) throws Exception {
KeyStore keyStore = ServerCertificateUtil.getKeyStore(STORE_PASS,
KEYSTORE_PATH);
// 获取私钥
PrivateKey privateKey = ServerCertificateUtil.getPrivateKey(keyStore, ALIAS,
STORE_PASS);
// 获取证书
X509Certificate certificate = ServerCertificateUtil.getCertificateByKeystore(
keyStore, ALIAS);
X509Certificate receivedCertificate = ClientCertificateUtil.getCertificateByCertPath(
CERT_PATH, CERT_TYPE);
PublicKey publicKey = ClientCertificateUtil.getPublicKey(receivedCertificate);
byte[] bb={12,21,14,16};
byte[] bb1=ServerCertificateUtil.encrypt(privateKey, bb);
byte[] bb2=ClientCertificateUtil.decode(bb1, publicKey);
System.out.println("bb1的长度"+bb1.length);
System.out.println("bb1的长度"+bb2.length);
for(int i=0;i<bb2.length;i++){
System.out.println(bb2[i]);
}
}
}