Ejemplo n.º 1
0
  /**
   * @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;
  }
Ejemplo n.º 2
0
  /*
   * Generate PBE key
   */
  private SecretKey getPBEKey(char[] password) throws IOException {
    SecretKey skey = null;

    try {
      PBEKeySpec keySpec = new PBEKeySpec(password);
      SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
      skey = skFac.generateSecret(keySpec);
    } catch (Exception e) {
      IOException ioe = new IOException("getSecretKey failed: " + e.getMessage());
      ioe.initCause(e);
      throw ioe;
    }
    return skey;
  }
Ejemplo n.º 3
0
  private CryptUtils(String passwd) {
    try {
      char[] chars = new char[passwd.length()];
      for (int i = 0; i < chars.length; i++) chars[i] = passwd.charAt(i);
      PBEKeySpec pbeKeySpec = new PBEKeySpec(chars);
      SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
      SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

      // Create PBE encode Cipher
      encodeCipher = Cipher.getInstance("PBEWithMD5AndDES");
      // Salt
      byte[] salt = {
        (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
        (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
      };

      // Iteration count
      int count = 20;

      // Create PBE parameter set
      PBEParameterSpec encodePbeParamSpec = new PBEParameterSpec(salt, count);

      // Initialize PBE encode Cipher with key and parameters
      encodeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, encodePbeParamSpec);

      // Create PBE decode Cipher
      decodeCipher = Cipher.getInstance("PBEWithMD5AndDES");

      // Create PBE parameter set
      PBEParameterSpec decodePbeParamSpec = new PBEParameterSpec(salt, count);

      // Initialize PBE decode Cipher with key and parameters
      decodeCipher.init(Cipher.DECRYPT_MODE, pbeKey, decodePbeParamSpec);

    } catch (NoSuchPaddingException ex) {
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }