Remove \ r and \ n from AES encrypted string

I encrypt a string using AES, but contains encrypted string \nand \rat the end.

 public class AESImpl {

  private static String decryptedString;

  private static String encryptedString;

  public static void main(String[] args) throws NoSuchAlgorithmException, IOException,  ClassNotFoundException {

    String strToEncrypt = "This text has to be encrypted";
    SecretKey secretKey = generateSecretKey();
    String encryptStr = encrypt(strToEncrypt, secretKey);
    System.out.println("Encrypted String : " + encryptStr + "It should not come in new line");
    String decryptStr = decrypt(encryptStr, secretKey);
    System.out.println("Decrypted String : " + decryptStr);
  }

  private static SecretKey generateSecretKey() throws NoSuchAlgorithmException, IOException {
    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(128);
    SecretKey sk = kg.generateKey();
    String secretKey = String.valueOf(Hex.encodeHex(sk.getEncoded()));
    System.out.println("Secret key is " + secretKey);
    return sk;
  }

  public static String encrypt(String strToEncrypt, SecretKey secretKey) {
    try {
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);
      encryptedString = new String(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes())));
    } catch (Exception e) {
      System.out.println("Error while encrypting: " + e.toString());
    }

    return encryptedString;
  }

  public static String decrypt(String strToDecrypt, SecretKey secretKey) {
    try {
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
      cipher.init(Cipher.DECRYPT_MODE, secretKey);
      decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
    } catch (Exception e) {
      System.out.println("Error while decrypting: " + e.toString());
    }

    return decryptedString;
  }
}

Exit

Secret key is 2df36561b09370637d35b4a310617e60
Encrypted String : TUDUORnWtsZFJAhBw1fYMF9CFExb/tSsLeDx++cpupI=
It should not come in new line
Decrypted String : This text has to be encrypted

Actually, the encrypted string TUDUORnWtsZFJAhBw1fYMF9CFExb/tSsLeDx++cpupI=/r/n. Do I need to explicitly change lines \rand \nfrom the encrypted string, or I did something wrong in the code above?

+4
source share
4 answers

, apache commons-codec-1.4.0.jar . . encodeBase64String chunking (commons-codec-1.4) -chunking (commons-codec-1.5).

, . http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html

+4

Base64.encodeBase64String(hashPassword,Base64.NO_WRAP) \n.

Base64.DEFAULT, .

:

:

+7

, base64 , 75 . , base64 , , , . , base64 , , , ...

. -, , base64: http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001AprJun/0183.html

0

encryptedString = encryptedString.replaceAll("(?:\\r\\n|\\n\\r|\\n|\\r)", "") .

It works great when you try to decode it back to bytes. I tested it several times with random generated byte arrays. Obviously, the decoding process simply ignores the newlines that they represent, or not. I tested this "confirmed work" with com.sun.org.apache.xml.internal.security.utils.Base64 Other encoders were not tested.

-1
source

Source: https://habr.com/ru/post/1527499/


All Articles