gpt4 book ai didi

java - 必需的 String[] 找到 String Java

转载 作者:行者123 更新时间:2023-12-01 13:18:33 24 4
gpt4 key购买 nike

我试图循环遍历字节数组并将其解码为字符串,使用 RSA 加密,加密无需数组即可工作,但我试图通过加密字符串的每个单词来使其可用于更长的数据,但是执行此操作时,我收到错误 required String[] found String Java。

 // Decrypt the cipher text using the private key.
inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
String[][] decryptedText = new String[cipherText.length][];
for (int i = 0; i < cipherText.length; i++) {
**ERROR ON THIS LINE - required String[] found String Java**
decryptedText[i] = decrypt(cipherText[i], privateKey);

}

解密方法

  public static String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);

} catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException |IllegalBlockSizeException | NoSuchPaddingException ex) {
}

return new String(dectyptedText);
}

加密方法

public static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
}
return cipherText;
}

最佳答案

问题是你的decryptedText是一个二维数组

 String[][] decryptedText = new String[cipherText.length][];

所以这一行

decryptedText[i] = decrypt(cipherText[i], privateKey);

必须将一个数组放入decryptedText。您可以更改 decryptedText 的声明来解决此问题

String[] decryptedText = new String[cipherText.length];

希望这有帮助。

关于java - 必需的 String[] 找到 String Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22249329/

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