@Test public void testEncryptByPublicKeyStringString() throws Exception { Map<String, Object> initKey = RSAUtil.initKey(); RSAPublicKey pubKey = (RSAPublicKey) initKey.get(RSAUtil.PUBLIC_KEY); byte[] pubKeyByte = pubKey.getEncoded(); long t11 = System.currentTimeMillis(); Cipher pubKeyCipher = RSA.getPubKeyEncryptCipher(pubKeyByte); long t12 = System.currentTimeMillis(); System.out.println("初始密钥时间"+(t12-t11)+"ms"); long t1 = System.currentTimeMillis(); byte[] data = RSA.doFinal("11111111111111111111111111111111".getBytes(),pubKeyCipher); long t2 = System.currentTimeMillis(); System.out.println(Base64Util.encodeBase64String(data)); System.out.println("加密字符串:11111111111111111111111111111111"); System.out.println("加密时间"+(t2-t1)+"ms"); RSAPrivateKey priKey = (RSAPrivateKey) initKey.get(RSAUtil.PRIVATE_KEY); Cipher priKeyCipher = RSA.getPriKeyDecryptCipher(priKey.getEncoded()); long t3 = System.currentTimeMillis(); byte[] data2 = RSA.doFinal(data,priKeyCipher); long t4 = System.currentTimeMillis(); System.out.println(new String(data2)); System.out.println("解密时间"+(t4-t3)+"ms"); }
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; }
RSAUtil { 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; } }
RSAUtil { 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; }  }
RSAUtil { 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; }  static byte[] decryptByPrivateKey(byte[] data, byte[] key); static String decryptByPrivateKey(String base64data, String base64key); static byte[] decryptByPublicKey(byte[] data, byte[] key); static String decryptByPublicKey(String base64data, String base64key); static byte[] encryptByPublicKey(byte[] data, byte[] key); static String encryptByPublicKey(String data, String base64key); static byte[] encryptByPrivateKey(byte[] data, byte[] key); static String encryptByPrivateKey(String data, String base64key); static boolean checkPublicEncrypt(String data, String sign,
			String pvKey); static byte[] getPrivateKey(Map<String, Object> keyMap); static byte[] getPublicKey(Map<String, Object> keyMap); static Map<String, Object> initKey(); }
RSAUtil { 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; }  static byte[] decryptByPrivateKey(byte[] data, byte[] key); static String decryptByPrivateKey(String base64data, String base64key); static byte[] decryptByPublicKey(byte[] data, byte[] key); static String decryptByPublicKey(String base64data, String base64key); static byte[] encryptByPublicKey(byte[] data, byte[] key); static String encryptByPublicKey(String data, String base64key); static byte[] encryptByPrivateKey(byte[] data, byte[] key); static String encryptByPrivateKey(String data, String base64key); static boolean checkPublicEncrypt(String data, String sign,
			String pvKey); static byte[] getPrivateKey(Map<String, Object> keyMap); static byte[] getPublicKey(Map<String, Object> keyMap); static Map<String, Object> initKey(); static final String KEY_ALGORITHM; static final String PUBLIC_KEY; static final String PRIVATE_KEY; static final String RSA_model; }