How Can We Help?
< All Topics
Print

Secure Java Encryption and Decryption

Secure Java Encryption and Decryption in BitssCloud

Java security technology includes a large set of APIs, tools, and implementations of commonly used security algorithms, mechanisms, and protocols. The Java security APIs include a wide range of areas such as cryptography, public key infrastructure, access control, secure communication, and authentication. Java security technology provides you with a comprehensive security framework for writing applications and also provides the user or administrator with a set of tools for secure applications management.

You can get the following benefits with this technology:

  • A safe and secure platform for developing and running applications.
  • An extensible, full-featured API for building secure applications.
  • Single sign-on of multiple authentication mechanisms and fine-grained access to resources based on the identity of the user or code signer.
  • Secure communications.
  • Easy development and deployment of complex PKI (Public Key Infrastructure) applications.

We use Java Cryptographic Extensions (JCE) in BitssCloud Java Platform. It is a set of Java APIs which provides cryptographic services such as encryption, secret key generation, message authentication code and key agreement. The ciphers supported by JCE include symmetric, asymmetric, block and stream ciphers. JCE APIs are implemented by Cryptographic Service Providers. Each of them implements the Service Provider Interface which specifies the functionalities which need to be implemented by the service providers.

We saved the native architecture and technology for you and you don’t need even to upload jar files or configure something to install JCE for your app, all the necessary libraries are included. Just come through the steps below.

Create Environment

Log into the BitssCloud Manager.

Ask BitssCloud to create a new environment.
< im 1 .>

Pick the application server you want to use (for example Tomcat 7), set the cloudlets limit and type the name of the environment, for example, secureapp. Then click Create.
< im 2 >

Wait just a minute for your environment to be created.

Upload and Run Java Package

Upload your Java package to the Deployment manager.
< im 3 >

As an example we use a simple Java app, which encrypts and decrypts text, using DES algorithm.

package com.encoding;
  
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
  
public class DesEncrypter {
  
    Cipher ecipher;
    Cipher dcipher;
  
    DesEncrypter(SecretKey key) {
        try {
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);
  
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }
  
    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");
  
            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);
  
            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
  
    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
}

You can download the WAR file here.

Once the package is in BitssCloud, deploy it to the environment you have created earlier.
< im4 >

Now you can open your application in a web browser.< im 5 >

Here’s the result of our program execution.< im 6 >

Enjoy!

Table of Contents