Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免在Java中输出回车符?_Java_String_Carriage Return - Fatal编程技术网

如何避免在Java中输出回车符?

如何避免在Java中输出回车符?,java,string,carriage-return,Java,String,Carriage Return,我已经编写了一个用Java加密文本的程序它在大部分情况下工作正常。 短字符串可以进行加密和解密 但是,如果我输入了多个单词,则输出到控制台的加密文本包含回车。对可能发生的事情有什么想法吗 如果我将输出粘贴到记事本中,删除返回值,然后用我的程序对其进行解密,它将按预期返回最初输入的文本 我已经包含了大部分代码,因为我不知道错误发生在哪里 import java.io.IOException; import java.security.InvalidKeyException; import java

我已经编写了一个用Java加密文本的程序它在大部分情况下工作正常。

短字符串可以进行加密和解密

但是,如果我输入了多个单词,则输出到控制台的加密文本包含回车。对可能发生的事情有什么想法吗

如果我将输出粘贴到记事本中,删除返回值,然后用我的程序对其进行解密,它将按预期返回最初输入的文本

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

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");
        }
    }
}
import java.io.IOException;
导入java.security.InvalidKeyException;
导入java.security.Key;
导入java.security.NoSuchAlgorithmException;
导入java.util.Scanner;
导入javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.spec.SecretKeySpec;
导入sun.misc.base64编码器;
导入sun.misc.base64解码器;
公共类应用程序
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
字符串textToEncrypt=“Hello World”;
字符串textToDecrypt;
字符串textToDecryptAscii;
字符串结果;
int操作;
密码=空;
试一试{
cipher=cipher.getInstance(“AES”);
}捕获(无算法异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(无填充异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
String key=“Baw12345Baw12345”;//128位密钥
BASE64Encoder AscienceOrder=新的BASE64Encoder();
base64解码器asciiDecoder=新的base64解码器();
System.out.printf(“输入:\n1进行加密\n2进行解密\n\n选择:”;
操作=输入.nextInt();
input.nextLine();
如果(操作==1)
{
尝试
{
System.out.printf(“\n-------------\n\n要加密的文本:”;
textToEncrypt=input.nextLine();
//创建密钥和密码
Key aesKey=新的SecretKeySpec(Key.getBytes(),“AES”);
//Cipher Cipher=Cipher.getInstance(“AES”);
//加密文本
cipher.init(cipher.ENCRYPT_模式,aesKey);
byte[]encrypted=cipher.doFinal(textToEncrypt.getBytes());
StringBuilder sb=新的StringBuilder();
for(字节b:加密)
{
sb.追加((char)b);
}
//加密字符串
字符串enc=sb.toString();
//System.out.println(“加密:+enc”);
字符串ascienceodedencryptedresult=ascienceoder.encodeBuffer(enc.getBytes());
System.out.println(“加密文本:+ascienceodedencryptedresult”);
//System.out.printf(“\n--------------------------------------\n加密文本:“+AscienceNodeEncryptedResult+”\n-------------------\n\n”);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
else if(操作==2)
{
System.out.printf(“\n------\n\n要解密的文本:”);
textToDecryptAscii=input.nextLine();
字节[]decodedBytes=null;
尝试
{
decodedBytes=asciiDecoder.decodeBuffer(texttodecryptscii);
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//System.out.println(“decodedBytes”+新字符串(decodedBytes));
textToDecrypt=新字符串(解码字节);
//将字符串转换为字节数组
//用于解密
byte[]bb=新字节[textToDecrypt.length()];

for(int i=0;i您的
ascienceorder
(实际上是base64)将自动添加新行字符,作为base64工作方式的一部分。在某些实现中,您可以通过以下类似方式删除此功能:

ascienceorder.linelength=0;


此外,您只需将换行符从结果字符串中删除,方法是将其替换为零。

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

ascienceorder.linelength=0;


此外,您可以通过不替换任何内容来删除结果字符串中的换行符。

免责声明:在某些实现中。使用
replace
从结果字符串中删除换行符会更容易。仅供参考:您的加密实际上也不安全,因为它使用ECB模式。是否有简单的方法进行调整我现有的使用CBC的代码?是的。添加一个IV。上面已经有很多信息。谢谢。你知道为什么这个问题被否决了吗?免责声明:在一些实现中。使用
replace
从结果字符串中删除它们会更容易。仅供参考:你的加密实际上也不安全,因为它使用ECB模式。有没有一个简单的方法来调整我现有的代码以使用CBC?是的。添加一个IV。已经有很多关于它的信息了。谢谢。你知道为什么这个问题被否决了吗?