Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:27:42+00:00 2026-06-11T12:27:42+00:00

In my Java based web-app I would like to encrypt some data before it

  • 0

In my Java based web-app I would like to encrypt some data before it is written to DB and decrypt it once loaded back to memory. To do that I used bouncycastle API and created a class that looks like this:

public class BlowfishEnrypter implements IEncrypter {

    /*--- Members ---*/

    private BufferedBlockCipher cipher;
    private KeyParameter key;

    /*--- Constructors ---*/

    /**
     * Initialize the cryptographic engine. The key array should be at least 8
     * bytes long.
     * 
     * @param key
     */
    public BlowfishEnrypter(byte[] key) {
    cipher = new BufferedBlockCipher(new CBCBlockCipher(new BlowfishEngine()));
    this.key = new KeyParameter(key);
    }

    /**
     * Initialize the cryptographic engine. The key array should be at least 8
     * bytes long.
     * 
     * @param key
     */
    public BlowfishEnrypter(String key) {
    this(key.getBytes());
    }

    /*--- Public ---*/

    /**
     * {@inheritDoc}
     */
    public String encrypt(String input) throws EncryptionException {
    if (StringUtils.hasText(input)) {
        byte[] bytes = Hex.decode(input);
        try {
        return new String(encrypt(bytes));
        } catch (CryptoException e) {
        throw new EncryptionException("Error occured while trying to encrypt", e);
        }
    } else {
        throw new EncryptionException("Illegal argument for encryption: " + input);
    }
    }

    /**
     * {@inheritDoc}
     */
    public String decrypt(String input) throws EncryptionException {
    if (StringUtils.hasText(input)) {
        byte[] bytes = Hex.decode(input);
        try {
        return new String(decrypt(bytes));
        } catch (CryptoException e) {
        throw new EncryptionException("Error occured while trying to decrypt", e);
        }
    } else {
        throw new EncryptionException("Illegal argument for decryption: " + input);
    }
    }

    /*--- Private ---*/

    /**
     * Encrypt arbitrary byte array, returning the encrypted data in a different
     * byte array.
     * 
     * @param data
     * @return Encrypted byte array
     * @throws CryptoException
     */
    private synchronized byte[] encrypt(byte[] data) throws CryptoException {
    if (data == null || data.length == 0) {
        return new byte[0];
    }

    cipher.init(true, key);
    return callCipher(data);
    }

    /**
     * Decrypts arbitrary data
     * 
     * @param data
     *            To decrypts
     * @return Decrypted byte array
     * @throws CryptoException
     */
    private synchronized byte[] decrypt(byte[] data) throws CryptoException {
    if (data == null || data.length == 0) {
        return new byte[0];
    }

    cipher.init(false, key);
    return callCipher(data);
    }

    /**
     * Private routine that does the gritty work.
     * 
     * @param data
     *            Data to operate on
     * @return Processed byte array
     * @throws CryptoException
     */
    private byte[] callCipher(byte[] data) throws CryptoException {
    int size = cipher.getOutputSize(data.length);
    byte[] result = new byte[size];
    int olen = cipher.processBytes(data, 0, data.length, result, 0);
    olen += cipher.doFinal(result, olen);

    if (olen < size) {
        byte[] tmp = new byte[olen];
        System.arraycopy(result, 0, tmp, 0, olen);
        result = tmp;
    }

    return result;
    }
}

So far so good (I think so, if you have any comments on this class please go ahead). To initialize this class I should provide a key. My question would be – How should I manage this key?

To be more specific:

  • Should I create it using a specific technique?
  • Where should I store it? In a properties file? in the DB? somewhere in my code?
  • should this key (we are talking about a string here, right?) be encrypted and then decrypted once it is loaded or used? If so how should I do that?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:27:43+00:00Added an answer on June 11, 2026 at 12:27 pm

    Depends on a number of things:

    1. Is your code stored in a secured location that cannot be viewed/accessed by others? If so I’d just put it in the code.
    2. Do you envisage changing the key from time to time (more secure this way)? If so and point 1 is still true then stick it in a property file.
    3. Otherwise put it in a database and keep the username/password of the database secret!

    Not sure why you would want to encrypt the actual key though because where will you put the key to secure the original key?

    The better thing to do though is look at Java SE security to see what, such as the Java Keystore, can do for you. Furthermore you can also read resources on bouncy castle’s website which might be helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For Java web app development on GAE, I would like to add users androles
I'm designing a Java based web-app and I need a key-value store. Berkeley DB
Background: I have created a CRUD web app using a java based RAD tool
Google App Engine is a cloud computing architecture that supports java based web services
We have a Java EE-based web application running on a Glassfish app server cluster.
I have a java web app that makes back-end use of a third-party web
I'm writing a backend for a mobile web-app based in Java and I was
Some colleagues of mine have a large Java web app that uses a search
As part of a Java based web app, I'm going to be accepting uploaded
I am working to create a Java based web app that also has ability

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.