java-使用base64解密文件
在我的项目中,选择一个文本文件并进行加密。加密文本与密钥分开保存。现在我尝试创建一个程序,当正确的密钥文件可用时,该程序将解密该文件。我认为解密程序需要看起来很像处于java-使用base64解密文件,java,encryption,cryptography,base64,Java,Encryption,Cryptography,Base64,在我的项目中,选择一个文本文件并进行加密。加密文本与密钥分开保存。现在我尝试创建一个程序,当正确的密钥文件可用时,该程序将解密该文件。我认为解密程序需要看起来很像处于DECRYPT\u模式的加密程序。当我读入密钥时,我不知道下一步如何解密文本文件。也许任何人都可以帮助我如何使用.txt文件中的密钥并使用它来解密编码文件 加密程序: public class encrypt { public static void main(String[] args) throws NoSuchAlg
DECRYPT\u模式的加密程序。当我读入密钥时,我不知道下一步如何解密文本文件。也许任何人都可以帮助我如何使用.txt文件中的密钥并使用它来解密编码文件
加密程序:
public class encrypt {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
//Key is created and saved in File
KeyGenerator keygenerator = KeyGenerator.getInstance("AES");
SecretKey myDesKey = keygenerator.generateKey();
String encodedKey = Base64.getEncoder().encodeToString(myDesKey.getEncoded());
Path keypath = Paths.get("C:/xxx/key.txt");
Path keyfile = Files.createFile(keypath);
Files.write(keyfile, encodedKey.getBytes(), StandardOpenOption.WRITE);
Cipher desalgCipher;
desalgCipher = Cipher.getInstance("AES");
desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
Path target = Paths.get("C:/xxx/encrypted.txt");
Path file = Files.createFile(target);
Path path = Paths.get("test.txt");
try(InputStream is = Files.newInputStream(path);
CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher);
BufferedReader reader = new BufferedReader(new InputStreamReader(cipherIS));){
String line;
while((line = reader.readLine()) != null){
System.out.println(line);
Files.write(file, line.getBytes(), StandardOpenOption.WRITE);
}
}
}
}
解密:读入密钥并解密
public class decrypt {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
try {
File fileDir = new File("C:/Users/JT/Desktop/key.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(fileDir), "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
byte[] decodedKey = Base64.getDecoder().decode(sb.toString());
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
SecretKeySpec key = new SecretKeySpec(sb.toString().getBytes(), "Base64");
Cipher desalgCipher;
desalgCipher = Cipher.getInstance("AES");
desalgCipher.init(Cipher.DECRYPT_MODE, key);
Path path = Paths.get("encrypted.txt"); // path to your file
try(InputStream is = Files.newInputStream(path); // get an IS on your file
CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher); // wraps stream using cipher
BufferedReader reader = new BufferedReader(new InputStreamReader(cipherIS));){ // init reader.
String line;
while((line = reader.readLine()) != null){
System.out.println(line);
}
}
}
}
您的应用程序编程方式不正确。当前,您试图通过使用cipheriputstream
实例包装输入流来进行加密。然后,再次使用BufferedReader
实例包装该实例
因此,您要做的是首先将输入文件的字节(可能是文本)转换为密文。此密文可以包含任何字节值。然后尝试使用默认字符集和行尾逐行读取这些字节。显然,加密后,即使是行的概念也不再存在,因此在最后一步中,您将丢失数据
然后将其转换回字节,然后(不知何故)尝试解密。这显然会失败,因为在readLine
语句中丢失了数据
您应该做的是使用字节读取文件。然后可以写入CipherOutputStream
。如果带有密文的文件需要是实际文本,则可以使用新的java.util.Base64
提供的Base64
流
只有在正确编程加密后,才能尝试并反转该过程。只要数据丢失,解密就会失败(根据模式和您的运气,会有错误或垃圾输出)
如果你运气不好,你最终会得到99%的代码。因此,祝你好运,注意下面的评论:不要在不了解你在做什么的情况下尝试执行加密。它将以眼泪结束,或是键盘被砸碎。您应该先尝试编写解密代码。除非您复制并粘贴了加密代码。在这种情况下,这是一个代码请求,与主题无关。不要复制和粘贴安全关键代码。好的,谢谢你的建议,但这对我如何处理密钥没有帮助?嗯。。。是的,我知道。你还没有写解密代码?我编辑了解密部分。读取密钥并选择要解密的文件。但由于这些原因,我得到了一个错误的错误算法:AES或Rinjndael required
。你知道如何避免吗?你知道base64不是一种加密算法,对吗?您不能创建base64“密钥”,因为没有这样的东西。