-1

Please help me to encrypt full string "TEST STRING TO ENCRYPT". here encryption is done for 16 bytes only. The following is my code for md5 encrption and decryption. Please help me to encrypt more bytes.

import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Md5Encryption 
{
    private static final String ALGORITHM = "md5";
    private static final String DIGEST_STRING = "HG58YZ3CR9";
    private static final String CHARSET_UTF_8 = "utf-8";
    private static final String SECRET_KEY_ALGORITHM = "DESede";
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding";

    /* Encryption Method */
    public String encrypt(String message) throws Exception 
    { 
        final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
        final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
        for (int j = 0, k = 16; j < 8;) { 
                keyBytes[k++] = keyBytes[j++]; 
        } 

        final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
        final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
        cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

        final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8); 
        final byte[] cipherText = cipher.doFinal(plainTextBytes); 

        return new String(cipherText); 
    } 

   /* Decryption Method */
    public String decrypt(String message) throws Exception { 
        final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
        final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
        for (int j = 0, k = 16; j < 8;) { 
                keyBytes[k++] = keyBytes[j++]; 
        } 

        final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
        final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
        decipher.init(Cipher.DECRYPT_MODE, key, iv); 

        final byte[] plainText = decipher.update(message.getBytes()); 

        return new String(plainText, CHARSET_UTF_8); 
    }

    public static void main(String[] args) throws Exception {


        String text = "TEST STRING TO ENCRYPT";
        String codedtext = new Md5Encryption().encrypt(text);
//        String codedtext = ".ªÉ…U$L§U`8ˉ­?¦”›°„";
        String decodedtext = new Md5Encryption().decrypt(codedtext); 

        System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
        System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


}
vimal
  • 55
  • 1
  • 2
  • 9
  • 1
    MD5 isn't an encryption algorithm - it is an (insecure) one way hashing algorithm. – chrixm Feb 23 '17 at 12:54
  • 2
    MD5 is a hashing algorithm, not encryption, and therefore you can't decrypt it either. It's also been considered totally pointless for years, as it's simply too quick to break. – Rik Lewis Feb 23 '17 at 12:54
  • it is just for internal use...So no problem – vimal Feb 23 '17 at 12:55
  • 1
    You cannot decrypt a hash, it simply doesn't exist. Hashing is one way. – Rik Lewis Feb 23 '17 at 12:56
  • 1
    You are not using MD5 Encryption ... rather you are using the MD5 Digest of a String as your Key for DES/CBC Encryption and Decryption. While Encrypting you do a `cipher.doFinal` whereas while Decrypting you do an `decipher.update` instead. I would recommend that you read [this Link](http://stackoverflow.com/questions/20227/how-do-i-use-3des-encryption-decryption-in-java) where you'll get a little bit more clarification. – Exception_al Feb 23 '17 at 13:44
  • Do not use DES, it is not secure and has been superseded by AES and AES is no harder to use. – zaph Feb 23 '17 at 14:00

2 Answers2

0

Hi you are not doing MD5 Encryption, you are getting the MD5 Digest of a String and then using it to generate a Key that does DESede/CBC Encryption for you. Kindly note that MD5 is not an Encryption but a Hashing Algorithm. Details available at this Link

Hashing is one way . You can not get convert your data/ string from a hash code. Encryption is 2 way - you can decrypt again the encrypted string if you have the key with you. Encryption function transforms a text into a nonsensical ciphertext by using an encryption key, and vice versa.

Instead of doing a decipher.update(..) you need a doFinal(..) , and also instead of converting the encrytped byte[] to String, try returning a byte[] from encrypt and pass a byte[] to decrypt as parameter; the way it is meant to be use. Below should solve your problem :

import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Md5Encryption 
{
    private static final String ALGORITHM = "md5";
    private static final String DIGEST_STRING = "HG58YZ3CR9";
    private static final String CHARSET_UTF_8 = "utf-8";
    private static final String SECRET_KEY_ALGORITHM = "DESede";
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding";

    /* Encryption Method */
    public byte[] encrypt(String message) throws Exception 
    { 
        final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
        final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
        for (int j = 0, k = 16; j < 8;) { 
                keyBytes[k++] = keyBytes[j++]; 
        } 

        final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
        final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
        cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

        final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8); 
        final byte[] cipherText = cipher.doFinal(plainTextBytes); 

        return cipherText; 
    } 

   /* Decryption Method */
    public String decrypt(byte[]  message) throws Exception { 
        final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
        final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
        for (int j = 0, k = 16; j < 8;) { 
                keyBytes[k++] = keyBytes[j++]; 
        } 

        final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
        final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
        decipher.init(Cipher.DECRYPT_MODE, key, iv); 

        final byte[] plainText = decipher.doFinal(message); 

        return new String(plainText, CHARSET_UTF_8); 
    }

    public static void main(String[] args) throws Exception {


        String text = "TEST STRING TO ENCRYPT";
        byte[] codedtext = new Md5Encryption().encrypt(text);
//        String codedtext = ".ªÉ…U$L§U`8ˉ­?¦”›°„";
        String decodedtext = new Md5Encryption().decrypt(codedtext); 

        System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
        System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


}
Community
  • 1
  • 1
Exception_al
  • 1,049
  • 1
  • 11
  • 21
0

There are two issues in the above code. 1. You should use doFinal to decrypt instead of update as it will only decrypt part of data. 2. You should pass byte array rather than converting it into string which may cause differences and you may get badpaddingexcpetion. Corrected code is as below:

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Md5Encryption 
{
    private static final String ALGORITHM = "md5";
    private static final String DIGEST_STRING = "HG58YZ3CR9";
    private static final String CHARSET_UTF_8 = "utf-8";
    private static final String SECRET_KEY_ALGORITHM = "DESede";
    private static final String TRANSFORMATION_PADDING = "DESede/CBC/PKCS5Padding";

/* Encryption Method */
public byte[] encrypt(String message) throws Exception 
{ 
    final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
    final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
    final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
    for (int j = 0, k = 16; j < 8;) { 
            keyBytes[k++] = keyBytes[j++]; 
    } 
    System.out.println(new String(keyBytes));
    final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
    final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
    final Cipher cipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
    cipher.init(Cipher.ENCRYPT_MODE, key, iv); 

    final byte[] plainTextBytes = message.getBytes(CHARSET_UTF_8);
    System.out.println(new String(plainTextBytes));
    final byte[] cipherText = cipher.doFinal(plainTextBytes); 

    //BASE64Encoder base64encoder = new BASE64Encoder();
    //return base64encoder.encode(cipherText);
    return cipherText; 
} 



/* Decryption Method */
    public String decrypt(byte[] message) throws Exception { 
        final MessageDigest md = MessageDigest.getInstance(ALGORITHM); 
        final byte[] digestOfPassword = md.digest(DIGEST_STRING.getBytes(CHARSET_UTF_8)); 
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
        for (int j = 0, k = 16; j < 8;) { 
                keyBytes[k++] = keyBytes[j++]; 
        } 
        System.out.println(new String(keyBytes));
        final SecretKey key = new SecretKeySpec(keyBytes, SECRET_KEY_ALGORITHM); 
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]); 
        final Cipher decipher = Cipher.getInstance(TRANSFORMATION_PADDING); 
        decipher.init(Cipher.DECRYPT_MODE, key, iv); 

        final byte[] plainText = decipher.doFinal(message); 

        return new String(plainText); 
    }

    public static void main(String[] args) throws Exception {


        String text = "TEST STRING TO ENCRYPT";
        byte[] codedtext = new Md5Encryption().encrypt(text);
//        String codedtext = ".ªÉ…U$L§U`8ˉ­?¦”›°„";
        String decodedtext = new Md5Encryption().decrypt(codedtext); 

        System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array 
        System.out.println(decodedtext); // This correctly shows "TEST STRING TO ENCRYPT" 
    } 


}
Tarun
  • 184
  • 6