package com.util; /** * 引进的包都是Java自带的jar包 * 秘钥相关包 * base64 编解码 * 这里只用到了编码 */ import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; 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 com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class CreateSecrteKey { public class Keys { } public static final String KEY_ALGORITHM = "RSA"; //public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; private static final String PUBLIC_KEY = "RSAPublicKey"; private static final String PRIVATE_KEY = "RSAPrivateKey"; public static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding"; //获得公钥 public static String getPublicKey(Map<String, Object> keyMap) throws Exception { //获得map中的公钥对象 转为key对象 Key key = (Key) keyMap.get(PUBLIC_KEY); //byte[] publicKey = key.getEncoded(); //编码返回字符串 return encryptBASE64(key.getEncoded()); } //获得公钥 public static String getPublicKey(String key,int size) throws Exception { //获得map中的公钥对象 转为key对象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(key); keyPairGen.initialize(size); //通过对象 KeyPairGenerator 获取对象KeyPair KeyPair keyPair = keyPairGen.generateKeyPair(); //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //公私钥对象存入map中 Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); Key key1 = (Key) keyMap.get(PUBLIC_KEY); //byte[] publicKey = key.getEncoded(); //编码返回字符串 return encryptBASE64(key1.getEncoded()); } //获得私钥 public static String getPrivateKey(Map<String, Object> keyMap) throws Exception { //获得map中的私钥对象 转为key对象 Key key = (Key) keyMap.get(PRIVATE_KEY); //byte[] privateKey = key.getEncoded(); //编码返回字符串 return encryptBASE64(key.getEncoded()); } //解码返回byte public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } //编码返回字符串 public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } //map对象中存放公私钥 public static Map<String, Object> initKey() throws Exception { //获得对象 KeyPairGenerator 参数 RSA 1024个字节 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(1024); //通过对象 KeyPairGenerator 获取对象KeyPair KeyPair keyPair = keyPairGen.generateKeyPair(); //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //公私钥对象存入map中 Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } //map对象中存放公私钥 public static Map<String, Object> initKey(String key,int size) throws Exception { //获得对象 KeyPairGenerator 参数 RSA 1024个字节 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(key); keyPairGen.initialize(size); //通过对象 KeyPairGenerator 获取对象KeyPair KeyPair keyPair = keyPairGen.generateKeyPair(); //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //公私钥对象存入map中 Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } //map 加密 public static String jiami(String key,int size,String ystr) throws Exception { String str=""; KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(key); keyPairGen.initialize(size); //通过对象 KeyPairGenerator 获取对象KeyPair KeyPair keyPair = keyPairGen.generateKeyPair(); //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //公私钥对象存入map中 Map<String, byte[]> keyMap = new HashMap<String, byte[]>(); keyMap.put(PUBLIC_KEY, publicKey.getEncoded()); keyMap.put(PRIVATE_KEY, privateKey.getEncoded()); // PublicKey key1 = (PublicKey ) keyMap.get(PUBLIC_KEY); PublicKey publicKey1 = restorePublicKey(keyMap.get(PUBLIC_KEY)); byte[] encodedText = RSAEncode(publicKey1, ystr.getBytes()); // System.out.println("RSA encoded: " + Base64.encode(encodedText)); String s =Base64.encode(encodedText) ;//new String(encodedText); return s; } /** * 还原公钥,X509EncodedKeySpec 用于构建公钥的规范 * * @param keyBytes * @return */ public static PublicKey restorePublicKey(byte[] keyBytes) { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); try { KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec); return publicKey; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } //map 加密 public static String jiemi(String key,int size,String ystr) throws Exception { String str=""; KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(key); keyPairGen.initialize(size); //通过对象 KeyPairGenerator 获取对象KeyPair KeyPair keyPair = keyPairGen.generateKeyPair(); //通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //公私钥对象存入map中 Map<String, byte[]> keyMap = new HashMap<String, byte[]>(); keyMap.put(PUBLIC_KEY, publicKey.getEncoded()); keyMap.put(PRIVATE_KEY, privateKey.getEncoded()); // 解密 PrivateKey privateKey1 = restorePrivateKey(keyMap.get(PRIVATE_KEY)); // PrivateKey key1 = (PrivateKey ) keyMap.get(PRIVATE_KEY); // byte[] encodedText = restorePrivateKey(key1); byte[] bytes = Base64.decode(ystr); // System.out.println("RSA encoded: " + Base64.encode(encodedText)); // System.out.println("RSA decoded: " + RSADecode(key1, bytes)); str=RSADecode(privateKey1, bytes); return str; } /** * 解密,三步走。 * * @param key * @param encodedText * @return */ public static String RSADecode(PrivateKey key, byte[] encodedText) { try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(encodedText)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 还原私钥,PKCS8EncodedKeySpec 用于构建私钥的规范 * * @param keyBytes * @return */ public static PrivateKey restorePrivateKey(byte[] keyBytes) { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( keyBytes); try { KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateKey = factory .generatePrivate(pkcs8EncodedKeySpec); return privateKey; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * 加密,三步走。 * * @param key * @param plainText * @return */ public static byte[] RSAEncode(PublicKey key, byte[] plainText) { try { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(plainText); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void main(String[] args) { Map<String, Object> keyMap; try { keyMap = initKey(); String publicKey = getPublicKey(keyMap); System.out.println("public="+publicKey); String privateKey = getPrivateKey(keyMap); System.out.println("private="+privateKey); String a=jiami("rsa",1024,"aaa"); System.out.println("jiamia="+a); String b=jiemi("rsa",1024,a); System.out.println("b="+b); } catch (Exception e) { e.printStackTrace(); } } }
最近下载更多
except I LV2
2023年11月5日
2036495585 LV9
2023年9月25日
yymmdm LV6
2022年8月30日
novice2 LV1
2022年8月28日
bai620123 LV16
2022年8月9日
liu2022 LV14
2022年7月31日
dasdascccf LV10
2022年6月16日
小丶无奈 LV10
2022年6月14日
wanglinddad LV55
2022年3月13日
lijun1314 LV6
2021年9月13日
最近浏览更多
233002037 LV3
10月20日
cuiiii
10月12日
暂无贡献等级
kyrie1102 LV3
6月16日
吞吞吐吐她 LV6
3月28日
17693282606 LV12
2023年12月20日
1222222222222222222 LV2
2023年12月17日
2749263182
2023年11月12日
暂无贡献等级
except I LV2
2023年11月5日
pangzhihui LV14
2023年10月23日
微信网友_6680567232876544 LV8
2023年10月10日