1.Base64 加密
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.security.MessageDigest; public class Coder { public static void main(String[] args) { try { //1. base64 加密、解密 System.out.println("=========1. base64 加密、解密========="); String name_for_base64 = "123456"; System.out.println("加密之前:" + name_for_base64); String encrypt_name_for_base64 = encryptBASE64(name_for_base64.getBytes()); System.out.println("加密之后:" + encrypt_name_for_base64); byte[] decrypt_name_for_base64 = decryptBASE64(encrypt_name_for_base64); System.out.println("解密之后:" + new String(decrypt_name_for_base64)); //2. md5 加密 System.out.println("=========2. md5 加密========="); String name_for_md5= "123456"; System.out.println("加密之前:" + name_for_md5); byte[] encrypt_name_for_md5 =encryptMD5(name_for_md5.getBytes()); System.out.println("加密之后:" + new String(encrypt_name_for_md5)); //3. sha 加密 System.out.println("=========2. sha 加密========="); String name_for_sha= "123456"; System.out.println("加密之前:" + name_for_sha); byte[] encrypt_name_for_sha =encryptSHA(name_for_sha.getBytes()); System.out.println("加密之后:" + new String(encrypt_name_for_sha)); } catch (Exception e) { e.printStackTrace(); } } public static final String KEY_SHA = "SHA"; public static final String KEY_MD5 = "MD5"; /** * BASE64解密 * * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } /** * MD5加密 * * @param data * @return * @throws Exception */ public static byte[] encryptMD5(byte[] data) throws Exception { MessageDigest md5 = MessageDigest.getInstance(KEY_MD5); md5.update(data); return md5.digest(); } /** * SHA加密 * * @param data * @return * @throws Exception */ public static byte[] encryptSHA(byte[] data) throws Exception { MessageDigest sha = MessageDigest.getInstance(KEY_SHA); sha.update(data); return sha.digest(); } }
import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; /** * 这个加密用到了上面 base64 的加密类 * @author Administrator * */ public class RSAEncrypter { public static void main(String[] args) throws Exception { String name = "123abc"; // 初始化之后的公钥、私钥 System.out.println("公钥:" + getPublicKey(keyMap)); System.out.println("私钥:" +getPrivateKey(keyMap)); // 使用 rsa 公钥加密,私钥解密 System.out.println("使用 公钥->加密、私钥->解密"); System.out.println("加密之前数据:" + name); byte[] encrypt_by_pubkey = encryptByPublicKey(name.getBytes(), getPublicKey(keyMap)); System.out.println("公钥加密之后 byte[] : " + Arrays.toString(encrypt_by_pubkey)); byte[] decrypt_by_prikey = decryptByPrivateKey(encrypt_by_pubkey, getPrivateKey(keyMap)); System.out.println("私钥解密之后 byte[] : " + Arrays.toString(decrypt_by_prikey)); System.out.println("解密之后数据:" + new String(decrypt_by_prikey)); System.out.println("=============================="); // 使用 rsa 私钥加密,公钥解密 System.out.println("使用 私钥->加密,公钥->解密"); System.out.println("加密之前数据:" + name); byte[] encrypt_by_prikey = encryptByPrivateKey(name.getBytes(), getPrivateKey(keyMap)); System.out.println("私钥加密之后 byte[] : " + Arrays.toString(encrypt_by_prikey)); byte[] decrypt_by_pubkey = decryptByPublicKey(encrypt_by_prikey, getPublicKey(keyMap)); System.out.println("私钥解密之后 byte[] : " + Arrays.toString(decrypt_by_pubkey)); System.out.println("解密之后数据:" + new String(decrypt_by_pubkey)); } /** * 加密的方式 */ public static final String KEY_ALGORTHM="RSA";// public static final String SIGNATURE_ALGORITHM="MD5withRSA"; /** * 公钥 */ public static final String PUBLIC_KEY = "RSAPublicKey";//公钥 /** * 私钥 */ public static final String PRIVATE_KEY = "RSAPrivateKey";//私钥 private static Map<String,Object> keyMap = initKey(); /** * 初始化密钥 * @return * @throws Exception */ public static Map<String,Object> initKey() { Map<String,Object> keyMap = new HashMap<String, Object>(2); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM); keyPairGenerator.initialize(1024); // keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); //公钥 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); //私钥 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); } catch (Exception e) { e.printStackTrace(); } return keyMap; } /** * 取得公钥,并转化为String类型 * @param keyMap * @return * @throws Exception */ public static String getPublicKey(Map<String, Object> keyMap)throws Exception{ Key key = (Key) keyMap.get(PUBLIC_KEY); return Coder.encryptBASE64(key.getEncoded()); } /** * 取得私钥,并转化为String类型 * @param keyMap * @return * @throws Exception */ public static String getPrivateKey(Map<String, Object> keyMap) throws Exception{ Key key = (Key) keyMap.get(PRIVATE_KEY); return Coder.encryptBASE64(key.getEncoded()); } /** * 用私钥加密 * @param data 加密数据 * @param key 密钥 * @return * @throws Exception */ public static byte[] encryptByPrivateKey(byte[] data,String key)throws Exception{ //解密密钥 byte[] keyBytes = Coder.decryptBASE64(key); //取私钥 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); //对数据加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); } /** * 用私钥解密<span style="color:#000000;"></span> * @param data 加密数据 * @param key 密钥 * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data,String key)throws Exception{ //对私钥解密 byte[] keyBytes = Coder.decryptBASE64(key); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key 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[] encryptByPublicKey(byte[] data,String key)throws Exception{ //对公钥解密 byte[] keyBytes = Coder.decryptBASE64(key); //取公钥 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key 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[] decryptByPublicKey(byte[] data,String key)throws Exception{ //对私钥解密 byte[] keyBytes = Coder.decryptBASE64(key); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); //对数据解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); } }