protected static void test() throws Exception { final HashMap<String, Object> map = RSAUtils.generateKeys(); final RSAPublicKey publicKey = (RSAPublicKey) map.get("public"); final RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private"); final String modulus = publicKey.getModulus().toString(); final String public_exponent = publicKey.getPublicExponent().toString(); final String private_exponent = privateKey.getPrivateExponent().toString(); final RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent); final RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent); final String pubStr = new String(Base64.encodeBase64(pubKey.getEncoded())); System.err.println("Encoded 1: " + pubStr); final RSAPublicKey pk2 = RSAUtils.getPublicKey(pubStr); System.err.println("Encoded 2: " + new String(Base64.encodeBase64(pk2.getEncoded()))); final String priStr = new String(Base64.encodeBase64(priKey.getEncoded())); System.err.println("Encoded 1: " + priStr); final RSAPrivateKey pi2 = RSAUtils.getPrivateKey(priStr); System.err.println("Encoded 2: " + new String(Base64.encodeBase64(pi2.getEncoded()))); System.err.println("modulus: " + modulus); System.err.println("public_exponent: " + public_exponent); System.err.println("private_exponent: " + private_exponent); String ming = "123456789"; for (int i = 0; i < 1; i++) { ming += "123456789"; } final String mi = RSAUtils.encryptByPublicKey(ming, pubKey); System.err.println("mi: " + mi); ming = RSAUtils.decryptByPrivateKey(mi, priKey); System.err.println("ming: " + ming); final DynamicPasswordCipher cipher = new DynamicPasswordCipher(); System.err.println("cipher: " + cipher.decrypt(cipher.encrypt(ming))); }
@Override public String decrypt(final char[] encryptedPassword) { return RSAUtils.decryptByPrivateKey(new String(encryptedPassword), privateKey); }