android 图像加密/解密 BufferedImage 和 ImageIO 未解析 [英] android Image encryption/decryption BufferedImage and ImageIO not resolved

查看:73
本文介绍了android 图像加密/解密 BufferedImage 和 ImageIO 未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的代码不适用于未解决的 android sdkBufferdImage 和 ImageIO".我试过实现Bitmap"和BitmapFactory",但它没有用.请帮我在android应用程序上做到这一点.请更正 android 应用程序.公共类计算器溢出{

Code given below ,is not working with the android sdk "BufferdImage and ImageIO " not resolved . I've tried to implement "Bitmap" and "BitmapFactory", but it did not work.Please help me to do this at android application . Please correct in for an android application . public class stackoverflow {

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);

    }
}

推荐答案

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

I was not getting the encrpted image file, may be the code not write the encrypted file. Decryption code has the same thing ....

加密代码:

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());

这篇关于android 图像加密/解密 BufferedImage 和 ImageIO 未解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆