-
package com.algorithm;
-
-
import java.security.Key;
-
import java.security.KeyFactory;
-
import java.security.KeyPair;
-
import java.security.KeyPairGenerator;
-
import java.security.PrivateKey;
-
import java.security.PublicKey;
-
import java.security.Signature;
-
import java.security.interfaces.RSAPrivateKey;
-
import java.security.interfaces.RSAPublicKey;
-
import java.security.spec.PKCS8EncodedKeySpec;
-
import java.security.spec.X509EncodedKeySpec;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import javax.crypto.Cipher;
-
-
import org.apache.commons.codec.binary.Hex;
-
-
-
-
-
-
-
public abstract class RSACoder {
-
-
public static final String KEY_ALGORITHM="RSA";
-
-
public static final String SIGNATURE_ALGORRITHM="SHA1withRSA";
-
-
private static final String PUBLIC_KEY="RSAPublicKey";
-
-
private static final String PRIVATE_KEY="RSAPrivateKey";
-
-
private static final int KEY_SIZE=512;
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,byte[]key) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,String privateKey) throws Exception
-
{
-
return decryptByPrivateKey(data,getKey(privateKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,String publicKey) throws Exception
-
{
-
return decryptByPublicKey(data,getKey(publicKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,byte[] key) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,String publicKey) throws Exception
-
{
-
return encryptByPublicKey(data,getKey(publicKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,String key) throws Exception
-
{
-
return encryptByPrivateKey(data,getKey(key));
-
}
-
-
-
-
-
-
-
public static byte[] getPrivateKey(Map<String,Object> keyMap) throws Exception
-
{
-
Key key=(Key)keyMap.get(PRIVATE_KEY);
-
return key.getEncoded();
-
}
-
-
-
-
-
-
-
public static byte[] getPublicKey(Map<String,Object> keyMap) throws Exception
-
{
-
Key key=(Key)keyMap.get(PUBLIC_KEY);
-
return key.getEncoded();
-
}
-
-
-
-
-
-
public static Map<String,Object> initKey() throws Exception
-
{
-
-
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);
-
-
keyPairGen.initialize(KEY_SIZE);
-
-
KeyPair keyPair=keyPairGen.generateKeyPair();
-
-
RSAPublicKey publicKey=(RSAPublicKey) keyPair.getPublic();
-
-
RSAPrivateKey privateKey=(RSAPrivateKey) keyPair.getPrivate();
-
-
Map<String,Object> keyMap=new HashMap<String,Object>(2);
-
keyMap.put(PUBLIC_KEY, publicKey);
-
keyMap.put(PRIVATE_KEY, privateKey);
-
return keyMap;
-
}
-
-
-
-
-
-
-
-
public static byte[] sign(byte[] data,byte[] privateKey) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(privateKey);
-
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey priKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Signature signature=Signature.getInstance(SIGNATURE_ALGORRITHM);
-
-
signature.initSign(priKey);
-
-
signature.update(data);
-
-
return signature.sign();
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,byte[] publicKey,byte[] sign) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(publicKey);
-
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PublicKey pubKey=keyFactory.generatePublic(x509KeySpec);
-
-
Signature signature=Signature.getInstance(SIGNATURE_ALGORRITHM);
-
-
signature.initVerify(pubKey);
-
-
signature.update(data);
-
-
return signature.verify(sign);
-
}
-
-
-
-
-
-
-
-
public static String sign(byte[] data,String privateKey) throws Exception
-
{
-
byte[] sign=sign(data,getKey(privateKey));
-
return Hex.encodeHexString(sign);
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,String publicKey,String sign) throws Exception
-
{
-
return verify(data,getKey(publicKey),Hex.decodeHex(sign.toCharArray()));
-
}
-
-
-
-
-
-
-
-
public static String getPrivateKeyString(Map<String,Object> keyMap) throws Exception
-
{
-
return Hex.encodeHexString(getPrivateKey(keyMap));
-
}
-
-
-
-
-
-
-
public static String getPublicKeyString(Map<String,Object> keyMap) throws Exception
-
{
-
return Hex.encodeHexString(getPublicKey(keyMap));
-
}
-
-
-
-
-
-
-
public static byte[] getKey(String key) throws Exception
-
{
-
return Hex.decodeHex(key.toCharArray());
-
}
-
}
-
package com.algorithm;
-
-
import java.security.Key;
-
import java.security.KeyFactory;
-
import java.security.KeyPair;
-
import java.security.KeyPairGenerator;
-
import java.security.PrivateKey;
-
import java.security.PublicKey;
-
import java.security.Signature;
-
import java.security.interfaces.RSAPrivateKey;
-
import java.security.interfaces.RSAPublicKey;
-
import java.security.spec.PKCS8EncodedKeySpec;
-
import java.security.spec.X509EncodedKeySpec;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import javax.crypto.Cipher;
-
-
import org.apache.commons.codec.binary.Hex;
-
-
-
-
-
-
-
public abstract class RSACoder {
-
-
public static final String KEY_ALGORITHM="RSA";
-
-
public static final String SIGNATURE_ALGORRITHM="SHA1withRSA";
-
-
private static final String PUBLIC_KEY="RSAPublicKey";
-
-
private static final String PRIVATE_KEY="RSAPrivateKey";
-
-
private static final int KEY_SIZE=512;
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,byte[]key) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,String privateKey) throws Exception
-
{
-
return decryptByPrivateKey(data,getKey(privateKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,byte[] key) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,String publicKey) throws Exception
-
{
-
return decryptByPublicKey(data,getKey(publicKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,byte[] key) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
PublicKey publicKey=keyFactory.generatePublic(x509KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,String publicKey) throws Exception
-
{
-
return encryptByPublicKey(data,getKey(publicKey));
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(key);
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,String key) throws Exception
-
{
-
return encryptByPrivateKey(data,getKey(key));
-
}
-
-
-
-
-
-
-
public static byte[] getPrivateKey(Map<String,Object> keyMap) throws Exception
-
{
-
Key key=(Key)keyMap.get(PRIVATE_KEY);
-
return key.getEncoded();
-
}
-
-
-
-
-
-
-
public static byte[] getPublicKey(Map<String,Object> keyMap) throws Exception
-
{
-
Key key=(Key)keyMap.get(PUBLIC_KEY);
-
return key.getEncoded();
-
}
-
-
-
-
-
-
public static Map<String,Object> initKey() throws Exception
-
{
-
-
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(KEY_ALGORITHM);
-
-
keyPairGen.initialize(KEY_SIZE);
-
-
KeyPair keyPair=keyPairGen.generateKeyPair();
-
-
RSAPublicKey publicKey=(RSAPublicKey) keyPair.getPublic();
-
-
RSAPrivateKey privateKey=(RSAPrivateKey) keyPair.getPrivate();
-
-
Map<String,Object> keyMap=new HashMap<String,Object>(2);
-
keyMap.put(PUBLIC_KEY, publicKey);
-
keyMap.put(PRIVATE_KEY, privateKey);
-
return keyMap;
-
}
-
-
-
-
-
-
-
-
public static byte[] sign(byte[] data,byte[] privateKey) throws Exception
-
{
-
-
PKCS8EncodedKeySpec pkcs8KeySpec=new PKCS8EncodedKeySpec(privateKey);
-
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PrivateKey priKey=keyFactory.generatePrivate(pkcs8KeySpec);
-
-
Signature signature=Signature.getInstance(SIGNATURE_ALGORRITHM);
-
-
signature.initSign(priKey);
-
-
signature.update(data);
-
-
return signature.sign();
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,byte[] publicKey,byte[] sign) throws Exception
-
{
-
-
X509EncodedKeySpec x509KeySpec=new X509EncodedKeySpec(publicKey);
-
-
KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);
-
-
PublicKey pubKey=keyFactory.generatePublic(x509KeySpec);
-
-
Signature signature=Signature.getInstance(SIGNATURE_ALGORRITHM);
-
-
signature.initVerify(pubKey);
-
-
signature.update(data);
-
-
return signature.verify(sign);
-
}
-
-
-
-
-
-
-
-
public static String sign(byte[] data,String privateKey) throws Exception
-
{
-
byte[] sign=sign(data,getKey(privateKey));
-
return Hex.encodeHexString(sign);
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,String publicKey,String sign) throws Exception
-
{
-
return verify(data,getKey(publicKey),Hex.decodeHex(sign.toCharArray()));
-
}
-
-
-
-
-
-
-
-
public static String getPrivateKeyString(Map<String,Object> keyMap) throws Exception
-
{
-
return Hex.encodeHexString(getPrivateKey(keyMap));
-
}
-
-
-
-
-
-
-
public static String getPublicKeyString(Map<String,Object> keyMap) throws Exception
-
{
-
return Hex.encodeHexString(getPublicKey(keyMap));
-
}
-
-
-
-
-
-
-
public static byte[] getKey(String key) throws Exception
-
{
-
return Hex.decodeHex(key.toCharArray());
-
}
-
}
-
package com.algorithm;
-
-
import java.util.Map;
-
-
import org.apache.commons.codec.binary.Base64;
-
-
-
-
-
-
-
public class RSACoderTest {
-
-
private static byte[] publicKey;
-
-
private static byte[] privateKey;
-
-
-
-
-
public static void initKey() throws Exception
-
{
-
-
Map<String,Object> keyMap=RSACoder.initKey();
-
publicKey=RSACoder.getPublicKey(keyMap);
-
privateKey=RSACoder.getPrivateKey(keyMap);
-
System.out.println("公钥:"+Base64.encodeBase64String(publicKey));
-
System.out.println("私钥:"+Base64.encodeBase64String(privateKey));
-
}
-
public static void test() throws Exception
-
{
-
String inputStr="RSA加密算法,私钥加密,公钥解密";
-
byte[] data=inputStr.getBytes();
-
-
byte[] enCodeData=RSACoder.encryptByPrivateKey(data,privateKey);
-
System.out.println("加密字符串:"+Base64.encodeBase64String(enCodeData));
-
-
byte[] deCodeData=RSACoder.decryptByPublicKey(enCodeData, publicKey);
-
System.out.println(new String(deCodeData).equals(inputStr));
-
}
-
-
-
-
-
public static void main(String[] args) throws Exception {
-
-
initKey();
-
RSACoderTest.test();
-
}
-
-
}
-
package com.algorithm;
-
-
import java.util.Map;
-
-
import org.apache.commons.codec.binary.Base64;
-
-
-
-
-
-
-
public class RSACoderTest {
-
-
private static byte[] publicKey;
-
-
private static byte[] privateKey;
-
-
-
-
-
public static void initKey() throws Exception
-
{
-
-
Map<String,Object> keyMap=RSACoder.initKey();
-
publicKey=RSACoder.getPublicKey(keyMap);
-
privateKey=RSACoder.getPrivateKey(keyMap);
-
System.out.println("公钥:"+Base64.encodeBase64String(publicKey));
-
System.out.println("私钥:"+Base64.encodeBase64String(privateKey));
-
}
-
public static void test() throws Exception
-
{
-
String inputStr="RSA加密算法,私钥加密,公钥解密";
-
byte[] data=inputStr.getBytes();
-
-
byte[] enCodeData=RSACoder.encryptByPrivateKey(data,privateKey);
-
System.out.println("加密字符串:"+Base64.encodeBase64String(enCodeData));
-
-
byte[] deCodeData=RSACoder.decryptByPublicKey(enCodeData, publicKey);
-
System.out.println(new String(deCodeData).equals(inputStr));
-
}
-
-
-
-
-
public static void main(String[] args) throws Exception {
-
-
initKey();
-
RSACoderTest.test();
-
}
-
-
}
-
package com.algorithm;
-
-
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 abstract class CertificateCoder {
-
-
private static final String CERT_TYPE="X.509";
-
-
-
-
-
-
-
-
-
private static PrivateKey getPrivateKeyByKeyStore(String keyStorePath,String alias,String password) throws Exception
-
{
-
-
KeyStore ks=getKeyStore(keyStorePath,password);
-
-
return (PrivateKey)ks.getKey(alias, password.toCharArray());
-
}
-
-
-
-
-
-
private static PublicKey getPublicKeyByCertificate(String certificatePath) throws Exception
-
{
-
-
Certificate certificate=getCertificate(certificatePath);
-
-
return certificate.getPublicKey();
-
}
-
-
-
-
-
-
-
private static Certificate getCertificate(String certificatePath) throws Exception
-
{
-
-
CertificateFactory certificateFactory=CertificateFactory.getInstance(CERT_TYPE);
-
-
FileInputStream in=new FileInputStream(certificatePath);
-
-
Certificate certificate=certificateFactory.generateCertificate(in);
-
-
in.close();
-
return certificate;
-
}
-
-
-
-
-
-
-
-
-
private static Certificate getCertificate(String keyStorePath,String alias,String password) throws Exception
-
{
-
-
KeyStore ks=getKeyStore(keyStorePath,password);
-
-
return ks.getCertificate(alias);
-
}
-
-
-
-
-
-
-
-
private static KeyStore getKeyStore(String keyStorePath,String password) throws Exception
-
{
-
-
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
-
-
FileInputStream in=new FileInputStream(keyStorePath);
-
-
ks.load(in, password.toCharArray());
-
-
in.close();
-
return ks;
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath,alias,password);
-
-
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath,alias,password);
-
-
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,String certificatePath) throws Exception
-
{
-
-
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
-
-
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,String certificatePath) throws Exception
-
{
-
-
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
-
-
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] sign(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
X509Certificate x509Certificate=(X509Certificate)getCertificate(keyStorePath,alias,password);
-
-
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);
-
-
signature.initSign(privateKey);
-
signature.update(data);
-
return signature.sign();
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,byte[] sign,String certificatePath) throws Exception
-
{
-
-
X509Certificate x509Certificate=(X509Certificate)getCertificate(certificatePath);
-
-
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
-
-
signature.initVerify(x509Certificate);
-
signature.update(data);
-
return signature.verify(sign);
-
}
-
}
-
package com.algorithm;
-
-
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 abstract class CertificateCoder {
-
-
private static final String CERT_TYPE="X.509";
-
-
-
-
-
-
-
-
-
private static PrivateKey getPrivateKeyByKeyStore(String keyStorePath,String alias,String password) throws Exception
-
{
-
-
KeyStore ks=getKeyStore(keyStorePath,password);
-
-
return (PrivateKey)ks.getKey(alias, password.toCharArray());
-
}
-
-
-
-
-
-
private static PublicKey getPublicKeyByCertificate(String certificatePath) throws Exception
-
{
-
-
Certificate certificate=getCertificate(certificatePath);
-
-
return certificate.getPublicKey();
-
}
-
-
-
-
-
-
-
private static Certificate getCertificate(String certificatePath) throws Exception
-
{
-
-
CertificateFactory certificateFactory=CertificateFactory.getInstance(CERT_TYPE);
-
-
FileInputStream in=new FileInputStream(certificatePath);
-
-
Certificate certificate=certificateFactory.generateCertificate(in);
-
-
in.close();
-
return certificate;
-
}
-
-
-
-
-
-
-
-
-
private static Certificate getCertificate(String keyStorePath,String alias,String password) throws Exception
-
{
-
-
KeyStore ks=getKeyStore(keyStorePath,password);
-
-
return ks.getCertificate(alias);
-
}
-
-
-
-
-
-
-
-
private static KeyStore getKeyStore(String keyStorePath,String password) throws Exception
-
{
-
-
KeyStore ks=KeyStore.getInstance(KeyStore.getDefaultType());
-
-
FileInputStream in=new FileInputStream(keyStorePath);
-
-
ks.load(in, password.toCharArray());
-
-
in.close();
-
return ks;
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] encryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath,alias,password);
-
-
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] decryptByPrivateKey(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath,alias,password);
-
-
Cipher cipher=Cipher.getInstance(privateKey.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encryptByPublicKey(byte[] data,String certificatePath) throws Exception
-
{
-
-
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
-
-
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decryptByPublicKey(byte[] data,String certificatePath) throws Exception
-
{
-
-
PublicKey publicKey=getPublicKeyByCertificate(certificatePath);
-
-
Cipher cipher=Cipher.getInstance(publicKey.getAlgorithm());
-
cipher.init(Cipher.DECRYPT_MODE, publicKey);
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
-
-
public static byte[] sign(byte[] data,String keyStorePath,String alias,String password) throws Exception
-
{
-
-
X509Certificate x509Certificate=(X509Certificate)getCertificate(keyStorePath,alias,password);
-
-
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
-
-
PrivateKey privateKey=getPrivateKeyByKeyStore(keyStorePath, alias, password);
-
-
signature.initSign(privateKey);
-
signature.update(data);
-
return signature.sign();
-
}
-
-
-
-
-
-
-
-
-
public static boolean verify(byte[] data,byte[] sign,String certificatePath) throws Exception
-
{
-
-
X509Certificate x509Certificate=(X509Certificate)getCertificate(certificatePath);
-
-
Signature signature=Signature.getInstance(x509Certificate.getSigAlgName());
-
-
signature.initVerify(x509Certificate);
-
signature.update(data);
-
return signature.verify(sign);
-
}
-
}
-
package com.algorithm;
-
-
import java.io.InputStream;
-
-
import org.apache.commons.codec.binary.Hex;
-
-
-
-
-
-
-
public class CertificateCoderTest {
-
-
private static String password="123456";
-
-
private static String alias="www.dominic.com";
-
-
private static String certificatePath="D:\\Program Files\\OpenSSL-Win32\\ca\\certs\\dominic.cer";
-
-
private static String keyStorePath="D:\\Program Files\\OpenSSL-Win32\\ca\\certs\\domini.keystore";
-
-
-
-
public static void test1() throws Exception
-
{
-
String inputStr="数字证书";
-
byte[] data=inputStr.getBytes();
-
-
byte[] encrypt=CertificateCoder.encryptByPublicKey(data, certificatePath);
-
-
byte[] decrypt=CertificateCoder.decryptByPrivateKey(encrypt, keyStorePath, alias, password);
-
System.out.println(new String(decrypt).equals(inputStr));
-
}
-
-
-
-
public static void test2() throws Exception
-
{
-
String inputStr="数字证书";
-
byte[] data=inputStr.getBytes();
-
-
byte[] encrypt=CertificateCoder.encryptByPrivateKey(data, keyStorePath, alias, password);
-
-
byte[] decrypt=CertificateCoder.decryptByPublicKey(encrypt, certificatePath);
-
System.out.println(new String(decrypt).equals(inputStr));
-
}
-
-
-
-
public static void test3() throws Exception
-
{
-
String inputStr="数字签名";
-
byte[] data=inputStr.getBytes();
-
-
byte[] sign=CertificateCoder.sign(data, keyStorePath, alias, password);
-
System.out.println("签名:"+Hex.encodeHexString(sign));
-
-
System.out.println(CertificateCoder.verify(data, sign, certificatePath));
-
}
-
public static void main(String[] args) throws Exception
-
{
-
CertificateCoderTest.test1();
-
CertificateCoderTest.test2();
-
CertificateCoderTest.test3();
-
}
-
}
-
-
package com.algorithm;
-
-
import java.io.InputStream;
-
-
import org.apache.commons.codec.binary.Hex;
-
-
-
-
-
-
-
public class CertificateCoderTest {
-
-
private static String password="123456";
-
-
private static String alias="www.dominic.com";
-
-
private static String certificatePath="D:\\Program Files\\OpenSSL-Win32\\ca\\certs\\dominic.cer";
-
-
private static String keyStorePath="D:\\Program Files\\OpenSSL-Win32\\ca\\certs\\domini.keystore";
-
-
-
-
public static void test1() throws Exception
-
{
-
String inputStr="数字证书";
-
byte[] data=inputStr.getBytes();
-
-
byte[] encrypt=CertificateCoder.encryptByPublicKey(data, certificatePath);
-
-
byte[] decrypt=CertificateCoder.decryptByPrivateKey(encrypt, keyStorePath, alias, password);
-
System.out.println(new String(decrypt).equals(inputStr));
-
}
-
-
-
-
public static void test2() throws Exception
-
{
-
String inputStr="数字证书";
-
byte[] data=inputStr.getBytes();
-
-
byte[] encrypt=CertificateCoder.encryptByPrivateKey(data, keyStorePath, alias, password);
-
-
byte[] decrypt=CertificateCoder.decryptByPublicKey(encrypt, certificatePath);
-
System.out.println(new String(decrypt).equals(inputStr));
-
}
-
-
-
-
public static void test3() throws Exception
-
{
-
String inputStr="数字签名";
-
byte[] data=inputStr.getBytes();
-
-
byte[] sign=CertificateCoder.sign(data, keyStorePath, alias, password);
-
System.out.println("签名:"+Hex.encodeHexString(sign));
-
-
System.out.println(CertificateCoder.verify(data, sign, certificatePath));
-
}
-
public static void main(String[] args) throws Exception
-
{
-
CertificateCoderTest.test1();
-
CertificateCoderTest.test2();
-
CertificateCoderTest.test3();
-
}
-
}
-
-
package com.algorithm;
-
-
import java.security.Key;
-
-
import javax.crypto.Cipher;
-
import javax.crypto.KeyGenerator;
-
import javax.crypto.SecretKey;
-
import javax.crypto.spec.SecretKeySpec;
-
-
import org.apache.commons.codec.binary.Base64;
-
import org.apache.commons.codec.digest.DigestUtils;
-
-
-
-
-
-
public abstract class AESCoder {
-
-
public static final String ALGORITHM="AES";
-
-
public static final int KEY_SIZE=128;
-
-
-
-
-
-
-
private static Key toKey(byte[] key) throws Exception
-
{
-
-
SecretKey secretKey=new SecretKeySpec(key,ALGORITHM);
-
return secretKey;
-
}
-
-
-
-
-
-
-
-
public static byte[] decrypt(byte[] data,byte[] key) throws Exception
-
{
-
-
Key k=toKey(key);
-
-
Cipher cipher=Cipher.getInstance(ALGORITHM);
-
-
cipher.init(Cipher.DECRYPT_MODE, k);
-
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decrypt(byte[] data,String key) throws Exception
-
{
-
return decrypt(data,getKey(key));
-
}
-
-
-
-
-
-
-
-
public static byte[] encrypt(byte[] data,byte[] key) throws Exception
-
{
-
-
Key k=toKey(key);
-
-
Cipher cipher=Cipher.getInstance(ALGORITHM);
-
-
cipher.init(Cipher.ENCRYPT_MODE, k);
-
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encrypt(byte[] data,String key) throws Exception
-
{
-
return encrypt(data,getKey(key));
-
}
-
-
-
-
-
-
public static byte[] initKey() throws Exception
-
{
-
-
KeyGenerator kg=KeyGenerator.getInstance(ALGORITHM);
-
-
kg.init(KEY_SIZE);
-
-
SecretKey secretKey=kg.generateKey();
-
-
return secretKey.getEncoded();
-
}
-
-
-
-
-
-
public static String initKeyString() throws Exception
-
{
-
return Base64.encodeBase64String(initKey());
-
}
-
-
-
-
-
-
-
public static byte[] getKey(String key) throws Exception
-
{
-
return Base64.decodeBase64(key);
-
}
-
-
-
-
-
-
public static String shaHex(byte[] data)
-
{
-
return DigestUtils.md5Hex(data);
-
}
-
-
-
-
-
-
-
public static boolean validate(byte[] data,String messageDigest)
-
{
-
return messageDigest.equals(shaHex(data));
-
}
-
-
}
-
package com.algorithm;
-
-
import java.security.Key;
-
-
import javax.crypto.Cipher;
-
import javax.crypto.KeyGenerator;
-
import javax.crypto.SecretKey;
-
import javax.crypto.spec.SecretKeySpec;
-
-
import org.apache.commons.codec.binary.Base64;
-
import org.apache.commons.codec.digest.DigestUtils;
-
-
-
-
-
-
public abstract class AESCoder {
-
-
public static final String ALGORITHM="AES";
-
-
public static final int KEY_SIZE=128;
-
-
-
-
-
-
-
private static Key toKey(byte[] key) throws Exception
-
{
-
-
SecretKey secretKey=new SecretKeySpec(key,ALGORITHM);
-
return secretKey;
-
}
-
-
-
-
-
-
-
-
public static byte[] decrypt(byte[] data,byte[] key) throws Exception
-
{
-
-
Key k=toKey(key);
-
-
Cipher cipher=Cipher.getInstance(ALGORITHM);
-
-
cipher.init(Cipher.DECRYPT_MODE, k);
-
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] decrypt(byte[] data,String key) throws Exception
-
{
-
return decrypt(data,getKey(key));
-
}
-
-
-
-
-
-
-
-
public static byte[] encrypt(byte[] data,byte[] key) throws Exception
-
{
-
-
Key k=toKey(key);
-
-
Cipher cipher=Cipher.getInstance(ALGORITHM);
-
-
cipher.init(Cipher.ENCRYPT_MODE, k);
-
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
public static byte[] encrypt(byte[] data,String key) throws Exception
-
{
-
return encrypt(data,getKey(key));
-
}
-
-
-
-
-
-
public static byte[] initKey() throws Exception
-
{
-
-
KeyGenerator kg=KeyGenerator.getInstance(ALGORITHM);
-
-
kg.init(KEY_SIZE);
-
-
SecretKey secretKey=kg.generateKey();
-
-
return secretKey.getEncoded();
-
}
-
-
-
-
-
-
public static String initKeyString() throws Exception
-
{
-
return Base64.encodeBase64String(initKey());
-
}
-
-
-
-
-
-
-
public static byte[] getKey(String key) throws Exception
-
{
-
return Base64.decodeBase64(key);
-
}
-
-
-
-
-
-
public static String shaHex(byte[] data)
-
{
-
return DigestUtils.md5Hex(data);
-
}
-
-
-
-
-
-
-
public static boolean validate(byte[] data,String messageDigest)
-
{
-
return messageDigest.equals(shaHex(data));
-
}
-
-
}
-
package com.algorithm;
-
-
-
-
-
-
public class AESCoderTest {
-
-
public static void main(String args[])
-
{
-
try {
-
-
String secretKey=AESCoder.initKeyString();
-
System.out.println("密钥为:"+secretKey);
-
String s="我们的大中国";
-
-
byte[] encryptData=AESCoder.encrypt(s.getBytes(), secretKey);
-
-
byte[] data=AESCoder.decrypt(encryptData, secretKey);
-
-
System.out.println(new String(data).equals(s));
-
} catch (Exception e) {
-
-
e.printStackTrace();
-
}
-
}
-
}
-
package com.algorithm;
-
-
-
-
-
-
public class AESCoderTest {
-
-
public static void main(String args[])
-
{
-
try {
-
-
String secretKey=AESCoder.initKeyString();
-
System.out.println("密钥为:"+secretKey);
-
String s="我们的大中国";
-
-
byte[] encryptData=AESCoder.encrypt(s.getBytes(), secretKey);
-
-
byte[] data=AESCoder.decrypt(encryptData, secretKey);
-
-
System.out.println(new String(data).equals(s));
-
} catch (Exception e) {
-
-
e.printStackTrace();
-
}
-
}
-
}
附证书生成命令:
keytool生成证书
keytool -genkeypair -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -validity 360 -alias www.dominic.com -keystore dominic.keystore
导出数字证书
keytool -exportcert -alias www.dominic.com -keystore dominic.keystore -file dominic.cer -rfc
打印数字证书
keytool -printcert -file dominic.cer
这里是自制证书,如果需要权威机构签发,需要导出证书申请文件由第三方签发
openssl证书创建
根证书构建命令?
echo 构建随机数 private/.rand
openssl rand -out private/.rand 1000
echo 构建根证书私钥 private/ca.key.pem
openssl genrsa -aes256 -out private/ca.key.pem 2048
echo 生成根证书签发申请 private/ca.csr
openssl req -new -key private/ca.key.pem -out private/ca.csr -subj "/C=CN/ST=BJ/L=BJ/O=dominic/OU=dominic/CN=www.dominic.com"
echo 签发根证书 private/ca.cer
openssl x509 -req -days 10000 -sha1 -extensions v3_ca -signkey private/ca.key.pem -in private/ca.csr -out certs/ca.cer
echo 根证书转换 private/ca.p12
openssl pkcs12 -export -cacerts inkey private/ca.key.pem -in private/ca.csr -out certs/ca.cer
服务器证书构建步骤命令?
(1)echo 构建服务器私钥 private/server.key.pem
(2)openssl genrsa -aes256 -out private/server.key.pem 2048
echo 生成服务器证书签发申请 private/server.csr
(3)openssl req -new -key private/server.key.pem -out private/server.csr -subj "/C=CN/ST=BJ/L=BJ/O=dominic/OU=dominic/CN=www.dominic.com"
echo 签发服务器证书 private/server.cer
openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA certs/ca.cer -CAkey private/ca.key.pem -CAserial ca.srl -CAcreateserial -in private/server.csr -out certs/server.cer
echo 服务器证书转换 private/server.p12
openssl pkcs12 -export -clcerts -inkey private/server.key.pem -in certs/server.cer -out certs/server.p12
客户证书构建命令?
echo 构建客户私钥 private/client.key.pem
openssl genrsa -aes256 -out private/client.key.pem 2048
echo 生成服务器证书签发申请 private/client.csr
openssl req -new -key private/client.key.pem -out private/client.csr -subj "/C=CN/ST=BJ/L=BJ/O=dominic/OU=dominic/CN=www.dominic.com"
echo 签发客户证书 private/server.cer
openssl ca -days 3650 -in private/client.csr -out certs/client.cer -cert certs/ca.cer -keyfile private/ca.key.pem
echo 客户证书转换 certs/client.p12
openssl pkcs12 -export -inkey private/client.key.pem -in certs/client.cer -out certs/client.p12
java加密解密算法记录
原文:http://blog.csdn.net/l569590478/article/details/50977727