-1

I am using the service like this

String value = "test@example.com"
String encrypedValue = EncrypterService get().encrypt(value.getBytes())
String decryptedValue = EncrypterService get().decrypt(encrypedValue .getBytes())



public final class EncrypterService {

    private static Key keySpec;
    private static Cipher encryptCipher;
    private static Cipher decryptCipher;
    private static String passphrase = "IU1ZaypiTiVYc3AtPXMxNWNMYGUmVUF8YUAtUSMuKVI=";
    private static final String KEY_ALGORIGHT = "HmacSHA256";
    private static final String CIPHER_ALGORITHM = "AES";
    private static final String MD5_ALGORITH = "MD5";
    private static EncrypterService service;

    private EncrypterService(){

    }

    private static synchronized void initialize() {
        if (service == null) {
            service = new EncrypterService();
            service.init();
        }
    }

    public static EncrypterService get() {
        initialize();
        return service;
    }


    public String encrypt (byte[] plaintext){
        //returns byte array encrypted with key
        try {

            byte[] encode = encryptCipher.doFinal(plaintext);
            return new String(encode);
        }catch(Exception e){
            throw new RuntimeException("Unable to decrypt data" + e);
        }
    }

    public  String decrypt (byte[] ciphertext) {
        //returns byte array decrypted with key
        try {
            byte[] decode = decryptCipher.doFinal(ciphertext);
            return new String(decode);
        }catch(Exception e){
            throw new RuntimeException("Unable to decrypt data" + e);
        }
    }

    private static void init(){
       try {
           if (encryptCipher == null && decryptCipher == null) {
               byte[] bytesOfMessage = Base64.decode(passphrase, Base64.NO_WRAP);
               MessageDigest md = MessageDigest.getInstance(MD5_ALGORITH);
               byte[] thedigest = md.digest(bytesOfMessage);
               keySpec = new SecretKeySpec(thedigest, KEY_ALGORIGHT);
               encryptCipher = Cipher.getInstance(CIPHER_ALGORITHM);
               encryptCipher.init(Cipher.ENCRYPT_MODE, keySpec);
               decryptCipher = Cipher.getInstance(CIPHER_ALGORITHM);
               decryptCipher.init(Cipher.DECRYPT_MODE, keySpec);
           }
       }catch(Exception e){
           throw new RuntimeException("Unable to initialise encryption", e);
       }
    }

}

stacktrace

java.lang.RuntimeException·Unable to decrypt datajavax.crypto.IllegalBlockSizeException: last block incomplete in decryption
Full TraceRaw
EncrypterService .java:59 EncrypterService .decrypt
Coder
  • 3,090
  • 8
  • 49
  • 85

2 Answers2

0
return new String(encode);

The problem is here. String is not a container for binary data. The round trip between byte[] and String is not guaranteed. You should either just pass around the original byte[] or else hex- or base64-encode it.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Issue#1:

java.security.MessageDigest will provide an instance of MD5 digest.

For this, you need to import the following

import java.security.*;

Issue#2:

For encrypedValue, you are using value.getBytes() and For decryptedValue , you are using encrypedValue .getBytes().

Here is some limitation for using getBytes(). It is platform independent. so you should use getBytes("UTF-8") instead of getBytes()

byte[] bytesOfMessage = yourString.getBytes("UTF-8");

MessageDigest md = MessageDigest.getInstance(MD5_ALGORITH);
byte[] thedigest = md.digest(bytesOfMessage);

Resource Link: How can I generate an MD5 hash?


Issue#3: Encoding and Decoding

Mr. Andrea suggested like below:

In Java 8, there is an officially supported API for Base64 encoding and decoding

Sample code using the "basic" encoding:

import java.util.Base64;

byte[] bytes = "Hello, World!".getBytes("UTF-8");
String encoded = Base64.getEncoder().encodeToString(bytes);
byte[] decoded = Base64.getDecoder().decode(encoded);

Resource Link: Decode Base64 data in Java

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132