示例#1
0
 public static void main(String[] args) throws Exception {
   String source = "zxp";
   InputStream publicIS = new FileInputStream("C:\\rsa_public_key.pem");
   InputStream privateIS = new FileInputStream("C:\\pkcs8_rsa_private_key.pem");
   // PublicKey publicKey = RSAUtils.loadPublicKey(PUCLIC_KEY);
   PublicKey publicKey = RSAUtils.loadPublicKey(publicIS);
   // PrivateKey privateKey = RSAUtils.loadPrivateKey(PRIVATE_KEY);
   PrivateKey privateKey = RSAUtils.loadPrivateKey(privateIS);
   byte[] b1 = RSAUtils.encryptData(source.getBytes(), publicKey);
   System.out.println(">>>" + new String(RSAUtils.decryptData(b1, privateKey)));
 }
示例#2
0
  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)));
  }
示例#3
0
 @Override
 public String decrypt(final char[] encryptedPassword) {
   return RSAUtils.decryptByPrivateKey(new String(encryptedPassword), privateKey);
 }
示例#4
0
 @Override
 public char[] encrypt(final String plainPassword) {
   return RSAUtils.encryptByPublicKey(plainPassword, publicKey).toCharArray();
 }