示例#1
0
  public static void main(final String[] args) throws Exception {
    // test ();

    final DynamicPasswordCipher cipher = new DynamicPasswordCipher();

    String ming = "1234567890";
    if (args != null && args.length > 0) {
      ming = args[0];
    }
    System.err.println("ming: " + ming);

    final char[] encrypt = cipher.encrypt(ming);
    final String decrypt = cipher.decrypt(encrypt);

    System.err.println("decrypt: " + decrypt);
    System.err.println("encrypt: " + new String(encrypt));
  }
示例#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)));
  }