示例#1
0
文件: Util.java 项目: gubo/slipwire
  /**
   * @param s
   * @return
   * @throws GeneralSecurityException
   * @throws UnsupportedEncodingException
   */
  public static String decrypt(final String s)
      throws GeneralSecurityException, UnsupportedEncodingException {
    String decrypted = null;

    try {
      if (s != null) {
        final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        final SecretKey secretKey =
            secretKeyFactory.generateSecret(new PBEKeySpec(secret.toCharArray()));
        final Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new PBEParameterSpec(SALT, 20));
        final byte[] stringBytes = s.getBytes("UTF-8");
        final byte[] decodedBytes = Base64.decode(stringBytes, Base64.DEFAULT);
        final byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        decrypted = new String(decryptedBytes, "UTF-8");
      }
    } catch (GeneralSecurityException x) {
      throw x;
    } catch (UnsupportedEncodingException x) {
      throw x;
    } catch (Exception x) {
      DBG.m(x);
    }

    return decrypted;
  }
示例#2
0
  // encrypts string
  public String encrypt(String toEncrypt) {
    try {
      // encrypt
      byte[] bytes = toEncrypt.getBytes("ASCII");
      byte[] encoded = encryptionCipher.doFinal(bytes);

      return Base64.encode(encoded);
    } catch (Exception e) {
      System.out.println("Encryption Error: " + e);
    }
    return null;
  }
示例#3
0
  // decrypts string
  public String decrypt(String toDecrypt) {
    try {
      // see the above comment for rant, replacing Encoder with Decoder... kthxbye
      // note: "above comment" no longer exists
      byte[] decoded = Base64.decode(toDecrypt);

      // decrypt
      byte[] bytes = decryptionCipher.doFinal(decoded);
      return new String(bytes, "ASCII");
    } catch (Exception e) {
      System.out.println("Decryption Error: " + e);
      e.printStackTrace();
    }
    return null;
  }