Secure Java Encryption and Decryption

Secure Java Encryption and Decryption in Apiqcloud

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 Apiqcloud 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 Apiqcloud Manager.

Ask Apiqcloud 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 Apiqcloud, 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!

  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

Timezone Data for Java/PHP App Server

  Managing Timezone Data for Java/PHP App Server All of the Java and PHP application servers...

Tomcat HTTP to HTTPS redirect

Option 1 Update web.xml configuration file and add the following content into the...

Environment Variables - Java custom Environment Variables

Environment variables represent a kind of placeholders, where you can store the frequently...

Java App server Configuration

To perform the necessary configurations, press the Config button next to your application...

WildFly server

WildFly is a modern, flexible, lightweight and managed application runtime, which is...