所以我正在为自己制作个人项目,我正在尝试加密手机上的文件.这些文件可以是任何东西,例如文档,照片等.现在我正在尝试使其正常工作.当我运行加密时,它似乎正常工作并加密文件.当我运行解密时,有时它会起作用,有时却不起作用.当它失败时,我通常会在"敲定密码,填充块损坏"错误时出现"错误".我也没有使用不同的测试文件,因此它不像某些文件有效,而有些则不然.这是我每次尝试的两个文件.
public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (salt + Pass).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
public static …Run Code Online (Sandbox Code Playgroud) 我正在使用一些使用Blowfish加密文本文件内容的java代码.当我将加密文件转换回来(即解密它)时,字符串从末尾开始缺少一个字符.有什么想法吗?我是Java的新手,并且在没有运气的情况下花了好几个小时.
war_and_peace.txt文件只包含字符串"This is some text".decrypted.txt包含"这是一些tex"(最后没有t).这是java代码:
public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
private static byte[] getBytes(String toGet)
{
try
{
byte[] retVal = new byte[toGet.length()];
for (int i = 0; i < toGet.length(); i++)
{
char anychar = toGet.charAt(i);
retVal[i] = (byte)anychar;
}
return retVal;
}catch(Exception e)
{
String errorMsg = "ERROR: getBytes …Run Code Online (Sandbox Code Playgroud)