我已经尝试了几天用java解密用openssl加密的消息 . 使用以下命令加密消息:
openssl enc -e -aes-256-cbc -kfile $ file.key -in toto -out toto.enc .
文件file.key包含256位的对称密钥 . 在命令中没有指定salt,但文件以Salted__开头 . 这是我编写的类,试图解密该文件,但即使删除该文件的16个字符即可获取任何内容,即:Salted__ salt加密 . 我得到错误:线程“main”中的异常javax.crypto.BadPaddingException:给定最终块未正确填充 .
有人能帮助我吗?
非常感谢你 .
public class Java {
private static SecretKey key = null;
private static Cipher cipher = null;
public static void main(String[] args) throws Exception
{
String filename = RESOURCES_DIR + "toto.enc";
byte[] key = Base64.decode("2AxIw+/AzDBj83OILV9GDpOs+izDFJEhD6pve/IPsN9=");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] test = Base64.decode(readFile(filename));
byte[] decryptedBytes = cipher.doFinal(test);
String decryptedText = new String(decryptedBytes, "UTF8");
System.out.println("After decryption: " + decryptedText);
}
public final static String RESOURCES_DIR = "C:/Users/toto/Desktop/";
static String readFile(String filename) throws FileNotFoundException, IOException {
FileReader fr;
BufferedReader br;
fr = new FileReader(new File(filename));
br = new BufferedReader(fr);
String str;
String res = "";
while ((str = br.readLine()) != null) {
res += str;
}
return res;
}
}