android图像加密/解密缓冲区未解析图像和图像IO

android图像加密/解密缓冲区未解析图像和图像IO,android,image,encryption,bitmap,javax.imageio,Android,Image,Encryption,Bitmap,Javax.imageio,下面给出的代码不适用于android sdk“BufferdImage和ImageIO”未解析。我曾经尝试过实现“位图”和“位图工厂”,但没有成功。请帮助我在android应用程序中实现。请为android应用程序更正。 公共类堆栈溢出{ public static void main(String[] arg) throws Exception { Scanner scanner = new Scanner(System.in); byte[] salt = { (byte)

下面给出的代码不适用于android sdk“BufferdImage和ImageIO”未解析。我曾经尝试过实现“位图”和“位图工厂”,但没有成功。请帮助我在android应用程序中实现。请为android应用程序更正。 公共类堆栈溢出{

public static void main(String[] arg) throws Exception {

    Scanner scanner = new Scanner(System.in);
    byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
               (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };

    {
        File inputFile = new File("C:/Users/AMD/Desktop/bhp/pngg.jpg");
        BufferedImage input = ImageIO.read(inputFile);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");

        PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray());          

        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
        FileOutputStream output = new FileOutputStream("C:/Users/AMD/Desktop/bhp/encrpngg.png");
        CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);

        ImageIO.write(input,"PNG",cos);
        cos.close();
        inputFile.delete();

    }

    {
        PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray());

        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

        File inFile=new File("C:/Users/AMD/Desktop/bhp/encrpngg.png");
        FileInputStream fis=new FileInputStream(inFile);
        CipherInputStream cis=new CipherInputStream(fis, pbeCipher);
        BufferedImage inpt=ImageIO.read(cis);
        cis.close();
        FileOutputStream output = new FileOutputStream("C:/Users/AMD/Desktop/bhp/decrpngg.jpg");
        ImageIO.write(inpt,"PNG",  output);

    }
}

我没有得到加密的图像文件,可能是代码没有写加密的文件。解密代码有同样的东西

加密代码:

fileinputstrm=new FileInputStream(path);
BufferedInputStream input=new BufferedInputStream(fileinputstrm);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray());          

PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

FileOutputStream output = new FileOutputStream(path + ".icrpt");
CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
output.write(bytes.toByteArray());
cos.close();
解密代码:

byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
        (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
fileinputstrm = new FileInputStream(path);

PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray());

PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

FileInputStream fis=new FileInputStream(path);
CipherInputStream cis=new CipherInputStream(fis, pbeCipher);
BufferedInputStream bfi=new BufferedInputStream(cis);
bfi.read();
cis.close();
FileOutputStream output1 = new FileOutputStream(path+".jpeg");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
BufferedOutputStream bfo=new BufferedOutputStream(output1);
output1.write(baos.toByteArray());

从技术上讲,当您调用
ImageIO.write(输入,“PNG”,cos);
时,您实际上不再处理图像数据了!请为android应用程序更正它。“我曾尝试实现“位图”和“BitmapFactory”,但不起作用。”-因为这些是您在android上的唯一选项(ImageIO和BuffereImage不是android API的一部分),也许你应该发布你所做的,并解释你得到了什么错误以及为什么它不起作用。Android API不包含
java.awt
javax.imageio
包。你必须找到Android特有的处理图像的方法。我在解码方法中遇到了这个异常,未能解码12-23 12:31:40.987:W/System.err(20481):java.io.IOException:完成密码12-23 12:31:40.987时出错:W/System.err(20481):at javax.crypto.cipheriputstream.read(cipheriputstream.java:107)12-23 12:31:40.987:W/System.err(20481):at