Exemple #1
0
 /**
  * 这个方法用于生成密钥,如果已经有密钥了就不需要在调用方法生成了
  *
  * @return
  * @throws Exception
  */
 public static Map<String, String> 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, String> keyMap = new HashMap<String, String>();
   keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));
   keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
   return keyMap;
 }
  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)));
  }
  /**
   * 生成密钥对�?�注意这里是生成密钥对KeyPair,再由密钥对获取公私�?
   *
   * @return
   */
  public static Map<String, byte[]> generateKeyBytes() {

    try {
      KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
      keyPairGenerator.initialize(KEY_SIZE);
      KeyPair keyPair = keyPairGenerator.generateKeyPair();
      RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
      RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

      Map<String, byte[]> keyMap = new HashMap<String, byte[]>();
      keyMap.put(PUBLIC_KEY, publicKey.getEncoded());
      keyMap.put(PRIVATE_KEY, privateKey.getEncoded());
      return keyMap;
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }