예제 #1
0
  public void doDecrypt(String encryptText) throws NoSuchAlgorithmException {
    RSA encrypt = new RSA();
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

    // byte[] e = ' &r?
    // ????gf??¦?_???(?)Z!-Dl?tA/??rl???`l???????iR?"|$?;D?I???|?F?JH???h???S?)u-?H??"`?M?2?#¢]?¬?6r?(??Y??:?
    // b ';
    // encrypt.encrypt(publicKey,encryptText.getBytes());

    byte[] de = encrypt.decrypt(privateKey, encryptText.getBytes());

    // System.out.println("加密:"+encrypt.bytesToString(e));
    System.out.println("11111111111111");
    System.out.println("解密:" + encrypt.bytesToString(de));
  }
예제 #2
0
  public static void main(String[] args) {
    try {

      RSA encrypt = new RSA(); // 加密对象
      String encryptText = "wangkang"; // 需要加密的文本(无法加密中文,好像也有长度限制)
      KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
      keyPairGen.initialize(1024);
      KeyPair keyPair = keyPairGen.generateKeyPair();
      // Generate keys
      RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
      RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
      byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());
      byte[] de = encrypt.decrypt(privateKey, e);
      System.out.println("加密:" + encrypt.bytesToString(e));
      System.out.println("解密:" + encrypt.bytesToString(de));

    } catch (Exception e) {
      e.printStackTrace();
    }
  }