5

While encrypting a string, it generates '\n' at end of the string.

This is how I'm doing encryption

public static String encrypt(String plainText) throws Exception {
        byte[] tdesKeyData = Consts.getSecretKey().getBytes();
        byte[] myIV = Consts.getInitializationVector().getBytes();
        SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DES");
        IvParameterSpec ivspec = new IvParameterSpec(myIV);
        Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, myKey, ivspec);

        byte[] plainTextBytes = plainText.getBytes("UTF-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encode(buf, Base64.DEFAULT);
        String base64EncryptedString = new String(base64Bytes);
        return base64EncryptedString;
    }

Please, somebody, guide me, what am I doing wrong here? Thanks in Advance.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
Make it Simple
  • 1,832
  • 5
  • 32
  • 57
  • 1
    Once you solve your error, just one advice: Don't use DES, it's outdated nowadays. Use something like AES. – Gaurav Mall Aug 11 '19 at 11:58
  • 4
    If `Base64.DEFAULT` is selected as the 2nd parameter in `Base64#encode`, the result contains line breaks. With [`Base64.NO_WRAP`](https://developer.android.com/reference/android/util/Base64.html#NO_WRAP) the result has no line breaks. – Topaco Aug 11 '19 at 12:57
  • yes Just saw in documentation just now https://developer.android.com/reference/android/util/Base64.html and fixed @Topaco Yes ur right. – Make it Simple Aug 11 '19 at 14:11

1 Answers1

3

Base64.NO_WRAP

Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Make it Simple
  • 1,832
  • 5
  • 32
  • 57