Java暗号化と復号化の芸術の参考コード
17047 ワード
RSA、DES、DESedeなど、よく使われるコードがここにあります.
証明書のアクション
トリプルDES暗号化
RSA暗号化
DES暗号化
=======END=======
証明書のアクション
package encryption;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.crypto.Cipher;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-5-14
* Time: 9:22
* To change this template use File | Settings | File Templates.
*/
public abstract class CertificateCoder {
// X509
public static final String CERT_TYPE = "X.509";
/**
* KeyStore
*
* @param keyStorePath
* @param alias
* @param password
* @return PrivateKey
* @throws Exception
*/
private static PrivateKey getPrivateKeyByKeyStore(String keyStorePath, String alias, String password) throws Exception {
//
KeyStore ks = getKeyStore(keyStorePath, password);
//
return (PrivateKey) ks.getKey(alias, password.toCharArray());
}
/**
* Certificate
*
* @param certificatePath
* @return PublicKey
* @throws Exception
*/
private static PublicKey getPublicKeyByCertificate(String certificatePath)
throws Exception {
//
Certificate certificate = getCertificate(certificatePath);
//
return certificate.getPublicKey();
}
/**
* Certificate
*
* @param certificatePath
* @return Certificate
*/
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;
}
/**
* Certificate
*
* @param keyStorePath
* @param alias
* @param password
* @return Certificate
* @throws Exception
*/
private static Certificate getCertificate(String keyStorePath, String alias,
String password) throws Exception {
//
KeyStore ks = getKeyStore(keyStorePath, password);
//
return ks.getCertificate(alias);
}
/**
* KeyStore
*
* @param keyStorePath
* @param password
* @return KeyStore
* @throws Exception
*/
private static KeyStore getKeyStore(String keyStorePath, String password)
throws Exception {
//
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
//
FileInputStream is = new FileInputStream(keyStorePath);
//
ks.load(is, password.toCharArray());
//
is.close();
return ks;
}
/**
* *
*
* @param data
* @param keyStorePath
* @param alias
* @param password
* @return byte[]
* @throws Exception
*/
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);
}
/**
*
*
* @param data
* @param keyStorePath
* @param alias
* @param password
* @return byte[]
* @throws Exception
*/
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);
}
/**
*
*
* @param data
* @param certificatePath
* @return byte[]
* @throws Exception
*/
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);
}
/**
*
*
* @param data
* @param certificatePath
* @return byte[]
* @throws Exception
*/
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);
}
/**
*
*
* @param keyStorePath
* @param alias
* @param password
* @return byte[]
* @throws Exception
*/
public static byte[] sign(byte[] sign, 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(sign);
return signature.sign();
}
/**
*
*
* @param data
* @param sign
* @param certificatePath
* @return boolean
* @throws Exception
*/
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);
}
}
トリプルDES暗号化
package encryption;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;
/**
* DESede Triple DES
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-5-14
* Time: 10:07
* To change this template use File | Settings | File Templates.
*/
public class DESedeCoder {
/**
*
* Java6 112 168
*/
public static final String KEY_ALGORITHM = "DESede";
/**
* 、 、
* Java6 PKCS5Padding
*/
public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
/**
*
*
* @param key
* @return
* @throws Exception
*/
public static Key tokey(byte[] key) throws Exception {
// DES
DESedeKeySpec dks = new DESedeKeySpec(key);
//
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
return keyFactory.generateSecret(dks);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
//
Key k = tokey(key);
/**
*
* PKCS7Padding ,
* Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM,"BC");
*/
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
//
Key k = tokey(key);
/**
*
* PKCS7Padding ,
* Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM,"BC");
*/
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
/**
*
*
* @return
* @throws Exception
*/
public static byte[] initKey() throws Exception {
/**
*
* 128 192
*
* KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM,"BC");
*/
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
/**
*
* Java6 112 168
* 128 192 ,
* kg.init(128);
*
* kg.init(192);
*/
kg.init(168);
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}
}
RSA暗号化
package encryption;
import javax.crypto.Cipher;
import java.security.*;
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;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-5-13
* Time: 6:58
* To change this template use File | Settings | File Templates.
*/
public class RSACoder {
//
public static final String KEY_ALGORITMA = "RSA";
//
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVITE_KEY = "RSAPrivateKey";
/**
* RSA
* 1024
* 64
* 512-65536
*/
private static final int KEY_SIZE = 512;
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
//
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITMA);
//
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
//
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITMA);
//
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
//
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITMA);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
//
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITMA);
//
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
*
*
* @param keyMap
* @return
* @throws Exception
*/
public static byte[] getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVITE_KEY);
return key.getEncoded();
}
/**
*
*
* @param keyMap
* @return
* @throws Exception
*/
public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
/**
*
*
* @return
* @throws Exception
*/
public static Map<String, Object> initKey() throws Exception {
//
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITMA);
//
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
//
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVITE_KEY, privateKey);
return keyMap;
}
}
DES暗号化
package encryption;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-5-13
* Time: 8:19
* To change this template use File | Settings | File Templates.
*/
public class DESCoder {
/**
*
* java6 56
*/
public static final String KEY_ALGORITHM = "DES";
/**
* 、 、
*/
public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
/**
*
*
* @param key
* @return
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
//
DESKeySpec dks = new DESKeySpec(key);
//
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
//
SecretKey secretKey = keyFactory.generateSecret(dks);
return secretKey;
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
//
Key k = toKey(key);
//
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// ,
cipher.init(Cipher.DECRYPT_MODE, k);
//
return cipher.doFinal(data);
}
/**
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static byte[] initKey() throws Exception {
/**
*
* 64
* KeyGenerator kg = KeyGenerator.getInstance(CIPHER_ALGORITHM,"BC");
*/
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
}
=======END=======