gpt4 book ai didi

java - 程序给出错误的输出

转载 作者:行者123 更新时间:2023-12-01 23:16:45 25 4
gpt4 key购买 nike

我的程序需要一些帮助。谁能帮我解决这个问题吗?

谢谢!

每次运行代码时,我都会得到以下输出:

enter image description here enter image description here enter image description here

但我希望输出在一个框中而不是多个框中如下所示: enter image description here

代码:

public class myDesCbc2 {

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

JFrame frame = null;
JFileChooser fChoose = new JFileChooser(System.getProperty("user.home"));
int returnVal = fChoose.showOpenDialog(frame);
File myFile = fChoose.getSelectedFile();

//Read file and store to String line
FileInputStream fis = new FileInputStream(myFile);
BufferedReader stream = new BufferedReader(new InputStreamReader(fis, "ISO-8859-1"));
String file;
while ((file = stream.readLine()) != null) {

JOptionPane.showOptionDialog(
null, "Generating a 56-bit DES key...", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
// Create an 8-byte initialization vector
SecureRandom sr = new SecureRandom();
byte[] iv = new byte[8];
sr.nextBytes(iv);
IvParameterSpec IV = new IvParameterSpec(iv);

// Create a 56-bit DES key
KeyGenerator kg = KeyGenerator.getInstance("DES");

// Initialize with keysize
kg.init(56);
Key mykey = kg.generateKey();

JOptionPane.showOptionDialog(
null, "Your key has been generated!", "Processing...", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

// Create a cipher object and use the generated key to initialize it
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, mykey, IV);

byte[] plaintext = file.getBytes("UTF8");

// Encrypt the text
byte[] ciphertext = cipher.doFinal(plaintext);

JOptionPane.showMessageDialog(null,"\n\nCiphertext: ");
for (int i = 0; i < ciphertext.length; i++) {

if (chkEight(i)) {
System.out.print("\n");
}
JOptionPane.showMessageDialog(null,ciphertext[i] + " ");
}
}
}
}

chk8 代码:

public class chkEight {
public static Boolean chkEight (int num) {
int num1, rem;
num1 = num % 8;
if(num1== 0) {
return true;
}
else
{
return false;
}
}
}

最佳答案

您的错误在这部分:

JOptionPane.showMessageDialog(null,"\n\nCiphertext: ");
for (int i = 0; i < ciphertext.length; i++) {

if (chkEight(i)) {
System.out.print("\n");
}
JOptionPane.showMessageDialog(null,ciphertext[i] + " ");
}

您想要获取所有这些ciphertext[i] 部分并以某种方式将它们组合起来。然后您可以在循环之外显示单个 MessageDialog。这样就可以达到预期的效果了。

关于java - 程序给出错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21105156/

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