如何避免在Java中输出carriage-returns?

How to avoid outputting carriage-returns in Java?

我写了一个程序来加密 Java 中的文本。 大部分情况下工作正常。

短字符串加密和解密都很好。

但是,如果我输入的字数比较多,输出到控制台的密文就包含回车符-returns。关于可能发生的事情有什么想法吗?

如果我将输出粘贴到记事本中,然后删除 returns,然后用我的程序对其进行解密,它 returns 是预期的原始输入文本。

我已经包含了大部分代码,因为我不知道错误发生在哪里。

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class Application
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String textToEncrypt = "Hello World";
        String textToDecrypt;
        String textToDecryptAscii;
        String result;
        int operation;
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchPaddingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String key = "Baw12345Baw12345"; // 128 bit key

        BASE64Encoder asciiEncoder = new BASE64Encoder();
        BASE64Decoder asciiDecoder = new BASE64Decoder();

        System.out.printf("Enter:\n1 for encryption\n2 for decryption\n\nChoice: ");
        operation = input.nextInt();
        input.nextLine();

        if (operation == 1)
        {
            try 
            {
                System.out.printf("\n---------\n\nText to encrypt: ");
                textToEncrypt = input.nextLine();

                //Create key and cipher
                Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
                //Cipher cipher = Cipher.getInstance("AES");

                //encrypt the text
                cipher.init(Cipher.ENCRYPT_MODE, aesKey);
                byte[] encrypted = cipher.doFinal(textToEncrypt.getBytes());

                StringBuilder sb = new StringBuilder();
                for (byte b: encrypted)
                {
                    sb.append((char)b);
                }

                // the encrypted String
                String enc = sb.toString();
                //System.out.println("encrypted:" + enc);

                String asciiEncodedEncryptedResult = asciiEncoder.encodeBuffer(enc.getBytes());
                System.out.println("Encrypted text: " + asciiEncodedEncryptedResult);
                //System.out.printf("\n------------------------------\nDecrypted text: " + asciiEncodedEncryptedResult + "\n------------------------------\n\n\n");

            }
            catch(Exception e) 
            {
                e.printStackTrace();
            }
        }
        else if (operation == 2)
        {
            System.out.printf("\n---------\n\nText to decrypt: ");
            textToDecryptAscii = input.nextLine();

            byte[] decodedBytes = null;
            try
            {
                decodedBytes = asciiDecoder.decodeBuffer(textToDecryptAscii);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            //System.out.println("decodedBytes " + new String(decodedBytes));

            textToDecrypt = new String(decodedBytes);

            //Convert the string to byte array
            //for decryption
            byte[] bb = new byte[textToDecrypt.length()];
            for (int i=0; i<textToDecrypt.length(); i++)
            {
                bb[i] = (byte) textToDecrypt.charAt(i);
            }

            //decrypt the text
            Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
            try
            {
                cipher.init(Cipher.DECRYPT_MODE, aesKey);
            }
            catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String decrypted = null;
            try
            {
                decrypted = new String(cipher.doFinal(bb));
            }
            catch (IllegalBlockSizeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (BadPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.printf("\n------------------------------\nDecrypted text: " + decrypted + "\n------------------------------\n\n\n");
        }
    }
}

您的 asciiEncoder(实际上是 base64)将自动添加换行符作为 base64 工作原理的一部分。您可以通过类似于此的方式在某些实现中删除此功能:

asciiEncoder.linelength = 0;

此外,您可以将换行符替换为任何内容,从而从结果字符串中删除换行符。