s.s*_*ash 1 java encryption blowfish
我写的加解密代码如下
import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.BadPaddingException;
import java.nio.file.Files;
import java.util.Scanner;
public class EncryptFile
{
public static void main(String args[]) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
//Encrypt Mode
FileOutputStream outputStream = new FileOutputStream(new File("D:\\encryptedNewStringFile.txt"));
Key secretKey = new SecretKeySpec("encKey".getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] getFileBytes = "writing a file using encryption ".getBytes();
byte[] outputBytes = cipher.doFinal(getFileBytes);
outputStream.write(outputBytes);
getFileBytes = "\n".getBytes();
outputBytes = cipher.doFinal(getFileBytes);
outputStream.write(outputBytes);
getFileBytes = "This is New Line 2 \nThis is NewLine 3".getBytes();
outputBytes = cipher.doFinal(getFileBytes);
outputStream.write(outputBytes);
outputStream.close();
//Decrypt Mode
File curFile = new File("D:\\encryptedNewStringFile.txt");
secretKey = new SecretKeySpec("encKey".getBytes(), "Blowfish");
cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
getFileBytes = Files.readAllBytes(curFile.toPath());
outputBytes = cipher.doFinal(getFileBytes);
InputStream bai = new ByteArrayInputStream(outputBytes);
BufferedReader bfReader = new BufferedReader(new InputStreamReader(bai));
Scanner scan = new Scanner(bfReader);
while(scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里我有一个输出问题,即打印输出中有一些额外的符号(即问号和框符号)。
我收到的输出是

任何建议都会非常有帮助,提前致谢
Cipher cipher = Cipher.getInstance("Blowfish");
Run Code Online (Sandbox Code Playgroud)
相当于
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
Run Code Online (Sandbox Code Playgroud)
这意味着每次调用时cipher.doFinal都会产生额外的填充。
为了写一个没有间歇性填充的文件,你应该使用
outputBytes = cipher.update(getFileBytes);
Run Code Online (Sandbox Code Playgroud)
并且cipher.doFinal仅在最后一次写入文件时使用。然后您将能够在解密期间使用 PKCS5Padding 而不是 NoPadding 以自动删除末尾的有效填充。
安全考虑:
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |