Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了java – 在Android上使用AES加密文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我正在为自己的个人项目工作,我正在尝试加密手机上的文件.这些文件可以是任何文件,照片等.现在我试图让这个工作正常.当我运行加密时,它似乎正常工作并加密文件.当我运行解密时,有时它的工作,其他时候它不.当它失败时,我通常得到一个错误,而最终确定密码,垫块损坏”错误.我也没有使用不同的测试文件,所以它不像一些文件工作,而其他文件没有.这是我每次尝试的两个文件.
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,b);
    }
    cos.flush();
    cos.close();
    fis.close();
}

public static void decrypt(String path,InvalidKeyException {
    FileInputStream fis = new FileInputStream(path);
    FiLeoutputStream fos = new FiLeoutputStream(path.replace(".crypt",""));
    byte[] key = (salt + Pass).getBytes("UTF-8");
    messageDigest sha = messageDigest.geTinstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key,"AES");
    Cipher cipher = Cipher.geTinstance("AES");
    cipher.init(Cipher.DECRYPT_MODE,sks);
    CipherInputStream cis = new CipherInputStream(fis,cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d,b);
    }
    fos.flush();
    fos.close();
    cis.close();
}@H_673_3@ 
 

目前,Salt和@R_616_5747@d是静态的,为了测试目的不要改变.一半的时间仍然会出错.@H_450_5@

有没有人有什么想法为什么会发生这种情况?我一直在寻找,我已经找到了几件事情尝试,没有一个工作.我已经虑了以下解决方案的以下问题:@H_450_5@

Android decryption: Error while finalizing cipher@H_450_5@

last block incomplete with CipherInputStream/CipherOutputStream,even with padding AES/CBC/PKCS5Padding@H_450_5@

Encryption error on Android 4.2@H_450_5@

Decrypting error : “no iv set when one expected”@H_450_5@

How to handle “last block incomplete in decryption”@H_450_5@

Encryption and decryption of image file@H_450_5@

Tips on encryption/decryption of images in java using AES@H_450_5@

任何帮助是极大的赞赏!我想我只是想念一些简单的东西@H_450_5@

更新!@H_450_5@

当盐是正确的时候.当我删除盐,问题解决了…有一点挖掘,结果盐通过是问题,但因为盐是一个字节[],而且是一个字符串.我把salt改成了String,然后用salt.concat(pass),问题解决了!@H_450_5@

解决方法

也许我错过了一些东西,但在我身边没有任何问题.
可以尝试以下类简单的更改fileToBeCrypted,fileToBeDecrypted和fileDecryptedOutput变量吗?
package test;

import java.io.FileInputStream;
import java.io.FiLeoutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.messageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class TESTCrypt{

    private static final String salt = "t784";
    private static final String crypt@R_616_5747@d = "873147cbn9x5'2 79'79314";
    private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
    private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
    private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";

    public static void main(String[] args) throws Exception
    {
        for (int i=0; i<100; i++)
        {
            encryptfile(fileToBeCrypted,crypt@R_616_5747@d);
            decrypt(fileToBeDecrypted,crypt@R_616_5747@d,fileDecryptedOutput);
            System.out.println(i);
        }
    }

    public static void encryptfile(String path,String @R_616_5747@d) throws IOException,InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FiLeoutputStream fos = new FiLeoutputStream(path.concat(".crypt"));
        byte[] key = (salt + @R_616_5747@d).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,b);
        }
        cos.flush();
        cos.close();
        fis.close();
    }

    public static void decrypt(String path,String @R_616_5747@d,String outPath) throws IOException,InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FiLeoutputStream fos = new FiLeoutputStream(outPath);
        byte[] key = (salt + @R_616_5747@d).getBytes("UTF-8");
        messageDigest sha = messageDigest.geTinstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key,"AES");
        Cipher cipher = Cipher.geTinstance("AES");
        cipher.init(Cipher.DECRYPT_MODE,sks);
        CipherInputStream cis = new CipherInputStream(fis,cipher);
        int b;
        byte[] d = new byte[8];
        while((b = cis.read(d)) != -1) {
            fos.write(d,b);
        }
        fos.flush();
        fos.close();
        cis.close();
    }

}@H_673_3@ 
 

我可以迭代多次没有错误
我正在使用Oracle JDK 1.8,但是以1.7兼容模式运行.@H_450_5@

希望这能帮助你.@H_450_5@

再见皮耶罗@H_450_5@

大佬总结

以上是大佬教程为你收集整理的java – 在Android上使用AES加密文件全部内容,希望文章能够帮你解决java – 在Android上使用AES加密文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。