我的程序需要一些帮助。任何人都可以帮我解决这个问题吗?
谢谢!
每次运行代码时,都会得到以下输出:
但是我希望输出在一个盒子中而不是多个:
代码:
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] + " ");
}
}
}
}
chkEight代码:
public class chkEight {
public static Boolean chkEight (int num) {
int num1, rem;
num1 = num % 8;
if(num1== 0) {
return true;
}
else
{
return false;
}
}
}
答案 0 :(得分:2)
您的错误就在这一部分:
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。这将达到预期的效果。
答案 1 :(得分:1)
扩展Jean-Bernard的答案:
字符串连接在Java中完成:
String s1 = "hello";
String s2 = "world";
String s3 = s1+" "+s2; // "hello world"
因此,在显示对话框之前,您要做的是连接所有字符串(使用循环)。
你会这样做:
String collection = "";
for(int i = 0; i < cihpertext.length; i++) {
collection += " "+ciphertext[i];
if(chkEight(i)) [
collection += "\n"
}
}
JOptionPane.showMessageDialog(null, collection);
编辑:澄清你的错误:
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] + " ");
}
在此代码中:
如果chkEight(i)返回true,尝试在终端上打印换行符;这不会在字符串中附加任何内容。
然后为循环中的每次迭代调用showMessageDialog,显示当前的密文元素和空格。
您确定自己了解自己的代码吗?