Crypt Utils : Encryption « Security « Java

Crypt Utils

//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    This file is part of Openbravo POS.
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    GNU General Public License for more details.
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <>.

//package com.openbravo.pos.pda.util;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

 * @author jaroslawwozniak
public class CryptUtils {

    private Cipher cipherDecrypt;

    /** Creates a new instance of Encrypter */
    public CryptUtils(String passPhrase) {

        try {
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            KeyGenerator kGen = KeyGenerator.getInstance("DESEDE");
            kGen.init(168, sr);
            Key key = kGen.generateKey();

            cipherDecrypt = Cipher.getInstance("DESEDE/ECB/PKCS5Padding");
            cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
        } catch (UnsupportedEncodingException e) {
        } catch (NoSuchPaddingException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (InvalidKeyException e) {

    public String decrypt(String str) {
        try {
            return new String(cipherDecrypt.doFinal(StringUtils.hex2byte(str)), "UTF8");
        } catch (UnsupportedEncodingException e) {
        } catch (BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        return null;


Related examples in the same category

1.Basic symmetric encryption example
2.Encryption and decryption with AES/ECB/PKCS7Padding
3.Cipher with AESECBPKCS7Padding BC
4.Basic symmetric encryption example with CTR using DES
5.Basic symmetric encryption example with padding and CBC using DES
6.Basic symmetric encryption example with padding and ECB using DES
7.CBC using DES with an IV based on a nonce: a hypothetical message number
8.Example of using PBE with a PBEParameterSpec
9.Get Cipher Instance Blowfish
10.Message without tampering with MAC (DES), encryption AES in CTR mode
11.Example of using PBE without using a PBEParameterSpec
12.Getting the Bytes of a Generated Symmetric Key
13.Encryption and Decryption using Symmetric Keys
14.Encrypt a password
15.Cryptography Streams: True Mirror
16.Get the formats of the encoded bytes
17.Create an encrypted string for password
18.Cryptography Streams: URLDigest
19.Easy Blowfish encryption
20.This program tests the AES cipher
21.This program tests the RSA cipher
22.Crypt demo
23.Encrypt User name
24.Encode a string using algorithm specified in web.xml and return the resulting encrypted password.
25.Encrypts the string along with salt, Decrypts the string and removes the salt