コード例 #1
0
  /**
   * Decrypts a String encrypted encrypted by the encrypt(String, Key) function.
   *
   * @param encryptedVersion The encrypted string
   * @param keyData The byte[] representation of the Key used in encryption.
   * @return The decrypted String.
   * @throws EncryptionException
   * @throws InvalidKeyException
   * @throws NoSuchAlgorithmException
   * @throws NoSuchPaddingException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   * @throws IOException
   */
  public static byte[] decrypt(byte[] encryptedVersion, Key salt) throws EncryptionException {
    // Get a cipher object.
    byte[] result = null;

    Cipher cipher;
    try {
      cipher = Cipher.getInstance(EncryptionAlgorithm.DES.toString());

      cipher.init(Cipher.DECRYPT_MODE, salt);

      result = cipher.doFinal(encryptedVersion);

    } catch (NoSuchAlgorithmException e) {
      throw new EncryptionException(e);
    } catch (NoSuchPaddingException e) {
      throw new EncryptionException(e);
    } catch (InvalidKeyException e) {
      throw new EncryptionException(e);
    } catch (IllegalBlockSizeException e) {
      throw new EncryptionException(e);
    } catch (BadPaddingException e) {
      throw new EncryptionException(e);
    }
    return result;
  }
コード例 #2
0
  public static byte[] encrypt(byte[] toEncrypt, byte[] rawSaltKey) throws EncryptionException {
    byte[] result = null;
    Key salt =
        new SecretKeySpec(rawSaltKey, 0, rawSaltKey.length, EncryptionAlgorithm.DES.toString());
    try {
      Cipher cipher = Cipher.getInstance(EncryptionAlgorithm.DES.toString());
      cipher.init(Cipher.ENCRYPT_MODE, salt);

      result = cipher.doFinal(toEncrypt);
    } catch (NoSuchAlgorithmException e) {
      throw new EncryptionException(e);
    } catch (InvalidKeyException e) {
      throw new EncryptionException(e);
    } catch (NoSuchPaddingException e) {
      throw new EncryptionException(e);
    } catch (IllegalBlockSizeException e) {
      throw new EncryptionException(e);
    } catch (BadPaddingException e) {
      throw new EncryptionException(e);
    }
    return result;
  }
コード例 #3
0
  /**
   * Decrypts a String encrypted encrypted by the encrypt(String, Key) function.
   *
   * @param encryptedVersion The encrypted string
   * @param rawSaltKey The byte[] representation of the Key used in encryption.
   * @return The decrypted String.
   * @throws EncryptionException
   * @throws InvalidKeyException
   * @throws NoSuchAlgorithmException
   * @throws NoSuchPaddingException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   * @throws IOException
   */
  public static byte[] decrypt(byte[] encryptedVersion, byte[] rawSaltKey)
      throws EncryptionException {
    // Get a cipher object.
    byte[] result = null;
    Key salt =
        new SecretKeySpec(rawSaltKey, 0, rawSaltKey.length, EncryptionAlgorithm.DES.toString());

    Cipher cipher;

    // decode the BASE64 coded message
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] raw = decoder.decodeBuffer(encrypted);

    try {
      cipher = Cipher.getInstance(EncryptionAlgorithm.DES.toString());

      cipher.init(Cipher.DECRYPT_MODE, salt);

      result = cipher.doFinal(encryptedVersion);

    } catch (NoSuchAlgorithmException e) {
      throw new EncryptionException(e);
    } catch (NoSuchPaddingException e) {
      throw new EncryptionException(e);
    } catch (IllegalBlockSizeException e) {
      throw new EncryptionException(e);
    } catch (BadPaddingException e) {
      throw new EncryptionException(e);
    } catch (InvalidKeyException e) {
      throw new EncryptionException(e);
    }
    //		If you want to return a string
    //		String clear = new String(stringBytes, "UTF8");
    //		return clear;

    return result;
  }
コード例 #4
0
  public static byte[] decrypt(String encryptedString, String rawSaltKeyString)
      throws EncryptionException {
    byte[] result = null;
    try {
      byte[] rawSaltKey = decode(rawSaltKeyString);
      byte[] encryptedVersion = decode(encryptedString);
      Key salt =
          new SecretKeySpec(rawSaltKey, 0, rawSaltKey.length, EncryptionAlgorithm.DES.toString());

      result = decrypt(encryptedVersion, salt);

    } catch (IOException e) {
      throw new EncryptionException(e);
    }
    //		If you want to return a string
    //		String clear = new String(stringBytes, "UTF8");
    //		return clear;
    return result;
  }
コード例 #5
0
 /**
  * Generates a java.security.Key using the 'DES' algorithm.
  *
  * @return The newly generated Key.
  * @throws NoSuchAlgorithmException
  */
 public static Key generateKey() throws NoSuchAlgorithmException {
   KeyGenerator generator;
   generator = KeyGenerator.getInstance(EncryptionAlgorithm.DES.toString());
   generator.init(new SecureRandom());
   return generator.generateKey();
 }