0

I have facing issue after applying encryption into a string, I want to decrypt that encrypted_string to a normal string, But none of the examples is working.

Also, they are working for byte array code, Byte_array encrypted and decrypted very well, But I need this working for the string.

Example, I tried already, How to encrypt and decrypt String with my passphrase in Java (Pc not mobile platform)?

public static String encrypt(String strClearText,String strKey) throws Exception{
    String strData="";

    try {
        SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
        Cipher cipher=Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
        byte[] encrypted=cipher.doFinal(strClearText.getBytes());
        strData=new String(encrypted);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
    return strData;
}

public static String decrypt(String strEncrypted,String strKey) throws Exception{
    String strData="";

    try {
        SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
        Cipher cipher=Cipher.getInstance("Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, skeyspec);
        byte[] decrypted=cipher.doFinal(strEncrypted.getBytes());
        strData=new String(decrypted);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e);
    }
    return strData;
}

String to byte[] then byte[] to string conversion not working properly?

meegle84
  • 125
  • 1
  • 6

2 Answers2

0

You can use Base64 enocde and decode.

Example:

package test;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class Test2 {

    public static void main(String[] args) throws Exception {
        String key = "abc123!";
        String encrypted = encrypt("test", key);
        System.out.println(encrypted);
        String decrypted = decrypt(encrypted, key);
        System.out.println(decrypted);
    }

    public static String encrypt(String strClearText, String strKey) throws Exception {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
            byte[] encrypted = cipher.doFinal(Base64.getDecoder().decode(strClearText));
            strData = Base64.getEncoder().encodeToString(encrypted);

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
        return strData;
    }

    public static String decrypt(String strEncrypted, String strKey) throws Exception {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, skeyspec);
            byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(strEncrypted));
            strData = Base64.getEncoder().encodeToString(decrypted);

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
        return strData;
    }

}

Output:

Fsmwp8c1n9w=

test

Community
  • 1
  • 1
heiwil
  • 612
  • 4
  • 8
  • I edited it to use default java.util.Base64 instead of com.unboundid.util.Base64 – heiwil Jun 06 '19 at 07:34
  • Its work on oreo, but not in lower version devices. Any other idea, Please help I have been stuck in this problem from last two days. – Akshay Jaiswal Jun 06 '19 at 07:45
  • If `java.util.Base64` is not working, can you use `android.util.Base64` and it's methods `Base64.encodeToString(..)` and `Base64.decode(..)` – heiwil Jun 06 '19 at 07:59
-1

Here simple encoding and decoding (above kitkat)

Write this class and just call method

class EncodeDecode
{
    public String encodeString(String text)
  {
    String b64;
    byte[] data=new byte[0];
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) 
    {
        data=text.getBytes(StandardCharsets.UTF_8);
        b64=Base64.encodeToString(data,Base64.DEFAULT);
    }

    return b64;
  } 

public String decodeString(String text)
   {
    String deString;
        if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT)
        {
            byte[] data2=Base64.decode(base64,Base64.DEFAULT);
            deString=new String (data2,StandardCharsets.UTF_8);
        }
        return deString;
   }
 }

I hope this answer will help you..

Mohsin kazi
  • 532
  • 1
  • 8
  • 15