-1

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

    }
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Anupam
  • 11
  • 5
  • In all technicality, when you call `ImageIO.write(input,"PNG",cos);`, you are longer actually dealing with image data! – trumpetlicks Apr 23 '14 at 16:20
  • correct it for android application. – Anupam Apr 23 '14 at 17:54
  • "I've tried to implement "Bitmap" and "BitmapFactory", but it did not work." - As these are your only options on Android (ImageIO and BufferedImage is not part of the Android API), maybe you should post what you did, and explain what errors you got and why it din't work. – Harald K Apr 23 '14 at 18:17
  • Android API does not contain `java.awt` and `javax.imageio` packages. You will have to find android-specific way to work with images. – Oleg Estekhin Apr 24 '14 at 09:48

1 Answers1

0

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());
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
Anupam
  • 11
  • 5
  • i m getting this exception in decode method and failed to decode – Erum Dec 23 '14 at 07:38
  • 12-23 12:31:40.987: W/System.err(20481): java.io.IOException: Error while finalizing cipher 12-23 12:31:40.987: W/System.err(20481): at javax.crypto.CipherInputStream.read(CipherInputStream.java:107) 12-23 12:31:40.987: W/System.err(20481): at – Erum Dec 23 '14 at 07:47