0

I tried to do encrypt and decrypt using des algorithm. For enryption i have no problems and successfully convert the plaintext to byte array. However i unable to decrypt the cipher text to normal plaintext. I think my decryption method have some problems. Here my coding

import java.io.ByteArrayInputStream;    
import java.io.ByteArrayOutputStream;    
import javax.crypto.Cipher;    
import javax.crypto.CipherInputStream;    
import javax.crypto.CipherOutputStream;    
import javax.crypto.spec.IvParameterSpec;    
import javax.crypto.spec.SecretKeySpec;    
import javax.xml.bind.DatatypeConverter;

public class FileEncryption {

    //Initial Vector
    public static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };      

    //EncryptAndDecrypt String -> Input : PlainText + Return : CipherText+DecipherText
    public static String encryptString(String src) throws Exception
    {
        String dst="";
        //Not Input!
        if(src == null || src.length()==0)
            return "";

        //Encryption Setting
        byte[] k="Multimediaproces".getBytes();
        SecretKeySpec Key = new SecretKeySpec(k,"AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE,Key,ivspec);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CipherOutputStream cout = new CipherOutputStream(baos,encryptCipher);
        cout.write(src.getBytes());
        cout.flush();               //ByteOutputStream -> Write Encryption Text
        cout.close();           
        dst = DatatypeConverter.printHexBinary(baos.toByteArray());
        return dst;
    }   
    //String src -> EncryptedData
    public static String decryptString(String src) throws Exception 
    {
        //src value is Encrypted Value!
        //So, src value -> Not Byte!
        //String dst="";
        byte[] encryptedBytes = src.getBytes();         
        //Not Input!
        if(src == null || src.length()==0)
            return "";          
        //Decryption Setting
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        byte[] k="Multimediaproces".getBytes();
        SecretKeySpec Key = new SecretKeySpec(k,"AES");
        Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        decryptCipher.init(Cipher.DECRYPT_MODE,Key,ivspec); 

        //ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(encryptedBytes);
        CipherInputStream cin = new CipherInputStream(bais,decryptCipher);
        //CipherOutputStream cout = new CipherOutputStream(baos,decryptCipher);
        //cout.write(src.getBytes());
        byte[] buf = new byte[1024];
        int read;
        while((read=cin.read(buf))>=0)  //reading encrypted data!
        {
            cin.read(buf,0,read);     //writing decrypted data!
            //read = cin.read(buf);
        }
        // closing streams
        //baos.write(decryptCipher.doFinal());
        //return baos.toString();
        //cin.close(); 
        return bais.toString();
    }
}

The error stated when i run the code error messages

5
  • Do not use DES in new code, it is not secure and has been superseded by AES. Also do not use a static IV, use a random IV and prepend it to the encrypted data for use during decryption. Commented Apr 4, 2017 at 17:36
  • You use getBytes() but do not specify an encoding so you might get UTF-16 or UTF-8, specify an encoding. Instead use getBytes("UTF-8"). Commented Apr 4, 2017 at 17:39
  • so how should i fix it? can you give some code for me Commented Apr 4, 2017 at 17:44
  • There are many examples of code here on SO. Since the encrypted data has not been provided there is little checking that can be done. Commented Apr 4, 2017 at 17:45
  • okay thanks for the help. i will try fix and find the solutions. thanks bro :) Commented Apr 4, 2017 at 17:50

1 Answer 1

1

In your encryptString method, you're transforming the bytes to a Hex string:

dst = DatatypeConverter.printHexBinary(baos.toByteArray());

So, in order to decrypt, you must convert this Hex string back to bytes:

// Instead of this: byte[] encryptedBytes = src.getBytes();
// Do this:
byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);

And you're returning the toString() representation of the ByteArrayInputStream, but what you actually need is the buf variable (as it'll contain the decrypted data). So your decryptString method should return:

// Instead of this: return bais.toString();
// do this:
return new String(buf);

Note: as you're converting String to bytes, and bytes back to String, you might face some encoding issues. To prevent that, it's recommended to always work with the same encoding for both conversions. In this case, you could do the following (assuming you want to use UTF-8, for example).

In encryptString:

cout.write(src.getBytes("UTF-8")); // instead of cout.write(src.getBytes());

In decryptString:

return new String(buf, "UTF-8"); // instead of return new String(buf);

This should work with any encoding, as long as you use the same for both methods.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah finally i got it! very appreciate it .. thanks buddy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.