【发布时间】:2026-01-12 13:40:01
【问题描述】:
我有一个代码,它应该在按下按钮后使用某种字符串键从我的 textArea 中加密文本。问题是,这种方法只能使用一个字符而不是整个字符串来加密文本。我需要更长的密钥,所以这里需要字符串。我该如何改变呢?
btnCipher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent klik) {
String textToCipher = textArea.getText();
String cipherKey = textField.getText();
String cipheredText = "";
int xor;
char temp;
for (int i=0; i<textToCipher.length(); i++){
xor = textToCipher.charAt(i)^cipherKey; //error
temp = (char)xor;
cipheredText += temp;
}
textArea.setText(cipheredText);
}
});
【问题讨论】:
-
如果这只是为了掩盖一些文本,那很好,但如果这是为了安全,你不应该编写自己的加密。讨论于this SO question
标签: java string char xor encryption