我在学校的项目中遇到了问题。我想要做的是读取纯文本文件并使用AES 128对其进行加密,这将给我一个密文文件,然后我想获取密码文件并将其转换为将其嵌入到图像中。 处理输入流的AES类和处理字符串的隐写术。如果我从图像中提取密文并将其保存到文件中以使用AES再次读取它我会遇到问题,因为有一些符号会使文件之间产生不同。
AES加密方法:
public static void encrypt(int keyLength, char[] password, InputStream input, OutputStream output){
if (keyLength != 128 && keyLength != 192 && keyLength != 256) {
throw new InvalidKeyLengthException(keyLength);
}
byte[] salt = generateSalt(SALT_LENGTH);
Keys keys = keygen(keyLength, password, salt);
Cipher encrypt = null;
try {
encrypt = Cipher.getInstance(CIPHER_SPEC);
encrypt.init(Cipher.ENCRYPT_MODE, keys.encryption);
} catch (NoSuchAlgorithmException | NoSuchPaddingException impossible) {
} catch (InvalidKeyException e) {
throw new StrongEncryptionNotAvailableException(keyLength);
}
byte[] iv = null;
try {
iv = encrypt.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
} catch (InvalidParameterSpecException impossible) {
}
output.write(keyLength / 8);
output.write(salt);
output.write(keys.authentication.getEncoded());
output.write(iv);
byte[] buffer = new byte[BUFFER_SIZE];
int numRead;
byte[] encrypted = null;
while ((numRead = input.read(buffer)) > 0) {
encrypted = encrypt.update(buffer, 0, numRead);
if (encrypted != null) {
output.write(encrypted);
}
}
try {
encrypted = encrypt.doFinal();
} catch (IllegalBlockSizeException | BadPaddingException impossible) {
}
if (encrypted != null) {
output.write(encrypted);
}
}
隐写编码方法:
public boolean encode(BufferedImage img, String message) {
BufferedImage image = user_space(img);
image = add_text(image, message);
return (setImage(image));
}
如果我可以让隐写术返回文本文件,我认为这样可以解决问题。
你觉得怎么样?