gpt4 book ai didi

java - Java中如何避免输出回车?

转载 作者:行者123 更新时间:2023-12-01 21:38:48 26 4
gpt4 key购买 nike

我已经编写了一个用 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");
}
}
}

最佳答案

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

asciiEncoder.linelength = 0;

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

关于java - Java中如何避免输出回车?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36670498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com