Android的图像加密/解密的BufferedImage和ImageIO的未解决图像、Android、ImageIO、BufferedImage

2023-09-13 02:21:40 作者:毫无防备

code如下,工作不与Android SDK的BufferdImage和ImageIO的没有解决。我试图实施位图和BitmapFactory,但它没有work.Please帮我在Android应用程序做到这一点。请更正为Android应用程序。 公共类计算器{

 公共静态无效的主要(字串[] ARG)抛出异常{

    扫描仪扫描=新的扫描仪(System.in);
    byte []的盐= {(字节)0xc7,(字节)0x73(字节)为0x21,(字节)0x8c,
               (字节)0x7e格式,(字节)0xc8,(字节)0xee,(字节)0x99};

    {
        inputfile文件=新的文件(C:/Users/AMD/Desktop/bhp/pngg.jpg);
        BufferedImage中输入= ImageIO.read(INPUTFILE);
        密密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithMD5AndDES);

        PBEKeySpec pbeKeySpec =新PBEKeySpec(通.toCharArray());

        PBEParameterSpec pbeParamSpec =新PBEParameterSpec(盐,20);
        SecretKey的PBEKey调用= keyFac.generateSecret(pbeKeySpec);

        密pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
        pbeCipher.init(Cipher.ENCRYPT_MODE,PBEKey调用,pbeParamSpec);
        的FileOutputStream输出=新的FileOutputStream(C:/Users/AMD/Desktop/bhp/encrpngg.png);
        CipherOutputStream COS =新CipherOutputStream(输出,pbeCipher);

        ImageIO.write(输入,PNG,COS);
        cos.close();
        inputFile.delete();

    }

    {
        PBEKeySpec pbeKeySpec =新PBEKeySpec(通.toCharArray());

        PBEParameterSpec pbeParamSpec =新PBEParameterSpec(盐,20);
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithMD5AndDES);
        SecretKey的PBEKey调用= keyFac.generateSecret(pbeKeySpec);

        密pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
        pbeCipher.init(Cipher.DECRYPT_MODE,PBEKey调用,pbeParamSpec);

        文件INFILE =新的文件(C:/Users/AMD/Desktop/bhp/encrpngg.png);
        的FileInputStream FIS =新的FileInputStream(INFILE);
        CipherInputStream顺=新CipherInputStream(FIS,pbeCipher);
        BufferedImage的INPT = ImageIO.read(CIS)
        cis.close();
        的FileOutputStream输出=新的FileOutputStream(C:/Users/AMD/Desktop/bhp/decrpngg.jpg);
        ImageIO.write(INPT,PNG,输出);

    }
}
 

解决方案

我没有得到encrpted图像文件,可能是code不写加密文件。解密code具有同样的事情......

加密code:

  fileinputstrm =新的FileInputStream(路径);
的BufferedInputStream输入=新的BufferedInputStream(fileinputstrm);
密密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithMD5AndDES);
PBEKeySpec pbeKeySpec =新PBEKeySpec(通.toCharArray());

PBEParameterSpec pbeParamSpec =新PBEParameterSpec(盐,20);
SecretKey的PBEKey调用= keyFac.generateSecret(pbeKeySpec);
密pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
pbeCipher.init(Cipher.ENCRYPT_MODE,PBEKey调用,pbeParamSpec);

的FileOutputStream输出=新的FileOutputStream(路径+.icrpt);
CipherOutputStream COS =新CipherOutputStream(输出,pbeCipher);

ByteArrayOutputStream字节=新ByteArrayOutputStream();
output.write(bytes.toByteArray());
cos.close();
 

解密code:

 字节[]盐= {(字节)0xc7,(字节)0x73(字节)为0x21,(字节)0x8c,
        (字节)0x7e格式,(字节)0xc8,(字节)0xee,(字节)0x99};
fileinputstrm =新的FileInputStream(路径);

PBEKeySpec pbeKeySpec =新PBEKeySpec(通.toCharArray());

PBEParameterSpec pbeParamSpec =新PBEParameterSpec(盐,20);
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(PBEWithMD5AndDES);
SecretKey的PBEKey调用= keyFac.generateSecret(pbeKeySpec);

密pbeCipher = Cipher.getInstance(PBEWithMD5AndDES);
pbeCipher.init(Cipher.DECRYPT_MODE,PBEKey调用,pbeParamSpec);

的FileInputStream FIS =新的FileInputStream(路径);
CipherInputStream顺=新CipherInputStream(FIS,pbeCipher);
的BufferedInputStream BFI =新的BufferedInputStream(CIS)
bfi.read();
cis.close();
FileOutputStream中输出1 =新的FileOutputStream(路径+JPEG);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
的BufferedOutputStream BFO =新的BufferedOutputStream(第一路);
output1.write(baos.toByteArray());
 
opencv4android 图片处理, 基于Android Studio的OpenCV简单使用

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 ....

Encryption Code:

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

Decryption Code :

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