我试图通过使用RSA加密循环遍历字节数组并将其解码为字符串,该加密在没有数组的情况下起作用,但是我试图通过对字符串的每个单词进行加密来使其可用于更长的数据,但是这样做时我得到所需的错误String []找到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];
希望这可以帮助。