gpt4 book ai didi

Java 文本框 - 将字节转换为字符串

转载 作者:行者123 更新时间:2023-11-29 03:36:15 28 4
gpt4 key购买 nike

我有一个 Java 应用程序,您可以在其中将字符串输入文本框,点击加密,它会在单独的文本框中显示加密的字符串。我将为此使用 AES 加密。问题是我无法让加密文本按字节显示,但文本框不会显示字节(仅采用字符串)。下面是我的代码的一个例子。

 public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8")));
return cipher.doFinal(plainText.getBytes("UTF-8"));
}

private class HandleThat implements ActionListener{
public void actionPerformed(ActionEvent eve){
JTextField jtf; //user will enter string here
JTextField jtf1; //this will show the encrypted text
plaintext = jtf.getText();
String error = "Error, you must provide some text";
if(eve.getActionCommand().equals("Encrypt")){
if(!jtf.getText().equals("")){
try{
byte[] cipher = encrypt(plaintext, encryptionKey);
for (int i=0; i<cipher.length; i++)

jtf1.setText(cipher[i]); //here is where I get my error
} catch (Exception e) {
e.printStackTrace();}
}else{
label.setText(error);
}
}

错误 - “类 JTextComponent 中的方法 setText 不能应用于给定类型; 必填:字符串 发现:字节 原因:实参byte不能通过方法调用转换成String”

如何将密码从字节更改为字符串?

最佳答案

如果你想pretty-print byte 值数组使用:

Arrays.toString(密码)

如果您希望将密码解释为 String,请使用:

新字符串(密码)

关于Java 文本框 - 将字节转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15371370/

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