/**
* 加密。注意密钥是可以获取到它适用的算法的。
*
* @param plainText
* @param privateKey
* @return
*/
public static byte[] encode(byte[] plainFile, PrivateKey privateKey) {
Cipher cipher;
try {
cipher = Cipher.getInstance(privateKey.getAlgorithm());
cipher.init(2, privateKey);
int inputLen = plainFile.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
for(int i = 0; inputLen - offSet > 0; offSet = i * 128) {
byte[] cache;
if(inputLen - offSet > 256) {
cache = cipher.doFinal(plainFile, offSet, 128);
} else {
cache = cipher.doFinal(plainFile, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
++i;
}
byte[] decryptedData = out.toByteArray();
out.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 (IOException 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();
}
return null;
}
/**
* 解密
*
* @param encodedText
* @param publicKey
* @return
*/
public static byte[] decode(byte[] encodedText, PublicKey publicKey) {
Cipher cipher;
try {
cipher = Cipher.getInstance(publicKey.getAlgorithm());
cipher.init(1, publicKey);
int inputLen = encodedText.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
for(int i = 0; inputLen - offSet > 0; offSet = i * 244) {
byte[] cache;
if(inputLen - offSet > 244) {
cache = cipher.doFinal(encodedText, offSet, 244);
} else {
cache = cipher.doFinal(encodedText, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
++i;
}
byte[] encryptedData = out.toByteArray();
out.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;
}
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import com.insigma.siis.local.utils.ServerCertificateUtil;
public class Test {
private static final String CERT_PATH = "C:\\Users\\handong\\Downloads\\workspace\\myCer.cer";
// JDK6只支持X.509标准的证书
private static final String CERT_TYPE = "X.509";
public static void main(String[] args) throws IOException {
// 接收证书
X509Certificate receivedCertificate = null;
// 获取公钥
PublicKey publicKey = null;
receivedCertificate = ClientCertificateUtil.getCertificateByCertPath(CERT_PATH, CERT_TYPE);
publicKey = ClientCertificateUtil.getPublicKey(receivedCertificate);
// 获取密钥库
KeyStore keyStore = ServerCertificateUtil.getKeyStore("Changeme123",
"C:\\Users\\handong\\Downloads\\workspace\\myKeystore.keystore");
// 获取私钥
PrivateKey privateKey = ServerCertificateUtil.getPrivateKey(keyStore, "myCertificate", "Changeme123");
byte[] as = { 0x12 };
byte[] sdf = ServerCertificateUtil.encode(as, privateKey);
byte[] fd = ClientCertificateUtil.decode(sdf, publicKey);
for (int i = 0; i < fd.length; i++) {
System.out.println(fd[1]);
}
}
不知道问题出在哪了,我用的RSA