Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image encryption, why it can't decrypt?

I'm trying to encrypt an image using ACM and henon, encryption is successful, but it can't be decrypted. The problem is (problem decrypt is after XORing pixel value, it can't restore it to initial position).

encryption scheme according to this : http://ieeexplore.ieee.org/xpl/articleDetails.jsp?tp=&arnumber=5054653&contentType=Conference+Publications&queryText%3Dimage+encryption+henon

Encryption step is:

  1. Read image
  2. Pixel extraction
  3. Shuffle pixel using ACM
  4. Generate henon pseudorandom
  5. Rounded henon pseudo random
  6. XORing shufle pixel vaue with henon pseudorandom
  7. Write image
  8. Cipher image done

Decryption step is:

  1. Read cipher image
  2. Pixel extraction
  3. Generate henon pseudorandom
  4. Rounded henon pseudo random
  5. XORing shufle pixel vaue with henon pseudorandom
  6. Restore pixel position using inverse ACM
  7. Write image
  8. image restored

Thanks.

Encryption code below:

  img = ImageIO.read(new File("5x5grayscale.bmp"));    
  Raster pixel = img.getData();               
  pxl = new int[img.getWidth()][img.getHeight()];
  pxl2 = new int[img.getWidth()][img.getHeight()];           

  for(int j=0;j<img.getHeight();j++){ 
    for(int i=0;i<img.getWidth();i++){                 
      pxl[i][j]= pixel.getSample(i, j, 0);  
      pxl2[i][j]= pixel.getSample(i, j, 0);  
    }                                                                       
  }           

  // shuffe pixel ACM iterration 1
  System.out.println("shuffle iteration  1");
  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){                 
      x[xx]=(1*i + c*j)%5;            
      y[xx]=(d*i + e*j)%5; 
      //System.out.println("-new coordinate ="+i+" , "+j+"="+x[xx]+","+y[xx]);
      xx++; 
    }
  }

  //shuffle pixel ACM 2 sd 10
  for(int k=0;k<9;k++){
    System.out.println("shuffle iteration "+(k+2));
    xx=0; 
    for(int i=0;i<5;i++){
      for(int j=0;j<5;j++){                 
        xtemp = x[xx];
        ytemp = y[xx];
        x[xx] = (1*xtemp + c*ytemp) % 5;
        y[xx] = (d*xtemp + e*ytemp) % 5;   

        System.out.println("-new coordinate="+i+" , "+j+"="+x[xx]+","+y[xx]);
        xx++; 
      }
    }              
  } 

  xx=0; 
  for (int a =0;a<5;a++){
    for(int b=0;b<5;b++){
      pxl[a][b]= pxl2[x[xx]][y[xx]];
      xx++;
      //System.out.println(pxl[a][b]);
    }
  }

  System.out.println("===================================henon============================");
  double k[] = new  double[102]; 
  int inter[] = new int [102];

  k[0] = 0.01;
  k[1] = 0.02; 
  double a=1.4;
  double b=0.3;


  System.out.println("generate pseudo random");
  for(int i=0;i<100;i++){                       
    k[i+2] =1-a*(Math.pow(k[i+1], 2))+b*k[i];
    // System.out.println(k[i]);            
  }  

  System.out.println("after rounded");
  for(int i=0;i<100;i++){
    inter[i]= (int) Math.round((k[i]*65536)%256);
    if(inter[i]<0){
      inter[i]=inter[i]+256;
    }
    //    System.out.println(inter[i]);
  }

  System.out.println("setelah xor");
  System.out.println("setelah xor");
  cipher = new int[img.getWidth()][img.getHeight()];
  int z=0;  
  for(int ii=0;ii<img.getWidth();ii++){
    for(int jj=0;jj<img.getHeight();jj++){
      cipher[ii][jj]=inter[z]^pxl[ii][jj];
      // System.out.println(cipher[ii][jj]);
      z++;
    }
  }

  image = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_BYTE_GRAY);          
  WritableRaster write = image.getRaster();                 

  for(int ii=0;ii<img.getHeight();ii++){
    for(int jj=0;jj<img.getWidth();jj++){          
      write.setSample(jj, ii, 0,cipher[jj][ii] );   
    }  
  }  

  ImageIO.write(image, "bmp", new File("acmhenonenkrip5x5.bmp"));  
  System.out.println("cipher image done");           
} 

Decrypt code below:

public static void main (String[] args) throws java.lang.Exception
{
  int c =2;
  int d = 2;
  int e = c*d+1;
  int x[]= new int[100];
  int y[]= new int[100];
  int xx=0;
  int xtemp;
  int ytemp;
  int sama=1;
  BufferedImage img ;
  BufferedImage image = null;
  int [][]pxl = null ;
  int [][]pxl2 = null ;
  int [][]cipher=null;

  img = ImageIO.read(new File("acmhenonenkrip5x5.bmp"));    
  Raster pixel = img.getData();     

  pxl = new int[img.getWidth()][img.getHeight()];
  pxl2 = new int[img.getWidth()][img.getHeight()];


  for(int j=0;j<img.getHeight();j++){ 
    for(int i=0;i<img.getWidth();i++){                 
      pxl[i][j]= pixel.getSample(i, j, 0);  
      pxl2[i][j]= pixel.getSample(i, j, 0);  
    }                                                                       
  }   

  System.out.println("===================================henon============================");
  double k[] = new  double[30];            int inter[] = new int [30];

  k[0] = 0.01;
  k[1] = 0.02; 
  double a=1.4;
  double b=0.3;


  System.out.println("generate pseudo random");
  for(int i=0;i<27;i++){                       
    k[i+2] =1-a*(Math.pow(k[i+1], 2))+b*k[i];
    // System.out.println(k[i]);            
  }  

  System.out.println("after rounded");
  for(int i=0;i<27;i++){
    inter[i]= (int) Math.round((k[i]*65536)%256);
    if(inter[i]<0){
      inter[i]=inter[i]+256;
    }
    // System.out.println(inter[i]);
  }

  System.out.println("after  xor");
  cipher = new int[img.getWidth()][img.getHeight()];
  int z=0;  
  for(int ii=0;ii<img.getWidth();ii++){
    for(int jj=0;jj<img.getHeight();jj++){
      cipher[ii][jj]=inter[z]^pxl[ii][jj];
      // System.out.println(cipher[ii][jj]);
      z++;
    }
  }

  System.out.println("===================================inverseacm============================");

  System.out.println("decrypt iteration1");

  for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){      
      x[xx]=((e*i) + (-c*j))%5; 
      if(x[xx]<0){
        x[xx]=x[xx]+5;
      }
      y[xx]=((-d*i) + (1*j))%5;  
      if(y[xx]<0){
        y[xx]=y[xx]+5;
      }
      // System.out.println(xx+"-new coordinate ="+i+","+j+"="+x[xx]+","+y[xx]);
      xx++; 
    }    
  }

  for(int iter=0;iter<9;iter++){
    System.out.println("decrypt iteration "+(iter+2));
    xx=0;  
    for(int i=0;i<5;i++){
      for(int j=0;j<5;j++){     
        xtemp = x[xx];
        ytemp = y[xx];
        x[xx]=((e*xtemp) + (-c*ytemp))%5; 
        if(x[xx]<0){
            x[xx]=x[xx]+5;
        }
        y[xx]=((-d*xtemp) + (1*ytemp))%5;  
        if(y[xx]<0){
          y[xx]=y[xx]+5;
        }
        // System.out.println(xx+"-new coordinate ="+i+","+j+"="+x[xx]+","+y[xx]);
        xx++; 
      }          
    }
  }    

  xx=0; 
  for(int ii=0;ii<img.getWidth();ii++){
    for(int jj=0;jj<img.getHeight();jj++){
      System.out.println("cip"+cipher[x[xx]][y[xx]]); //tracing pixel value of cipher
      xx++;     
    }            
  } 

  xx=0; 
  for(int ii=0;ii<img.getWidth();ii++){
    for(int jj=0;jj<img.getHeight();jj++){
      System.out.println("pxl"+pxl[x[xx]][y[xx]]); //tracing pixel value of pxl
      xx++;     
    }
  }

  image = new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_BYTE_GRAY);          
  WritableRaster write = image.getRaster();                 

  for(int ii=0;ii<img.getHeight();ii++){
    for(int jj=0;jj<img.getWidth();jj++){          
      write.setSample(jj, ii, 0,cipher[jj][ii] );                             
    }  
  }  

  ImageIO.write(image, "bmp", new File("acmhenondekrip5x5.bmp"));  
}
like image 933
user1552917 Avatar asked Jul 31 '12 14:07

user1552917


People also ask

Why can't I decrypt a file?

Use Microsoft Windows Malicious Software Removal ToolOne of the main factors that you fail to decrypt a file is a malware attack. Actually, file decryption failure happens to be the most common sign of a ransomware or malware attack.

How do I unlock an encrypted image?

Try online photo decryption tools. Google search would display several online decryption tools that you can use to view the encrypted photos. They are usually free to use. You just need to open the program in your browser, upload the encrypted image file, and select the 'decrypt' option in the program.

Why can't the public key decrypt a message?

The power of public key encryption is in that mathematical operation. It's a "one-way function", which means it's incredibly difficult for a computer to reverse the operation and discover the original data. Even the public key cannot be used to decrypt the data.

Is it possible to decrypt encrypted files?

You can decrypt your encrypted files and folders on Windows with the Command Prompt, a command-line interpreter referred to as cmd.exe or cmd. This works if you previously encrypted the file using the Cipher command, and you're using the exact same PC and copy of Windows as you did when you encrypted it.


1 Answers

You should separate each stage into two methods: one to do and another to undo. Then you should write a short set of tests that generates thousands of random inputs and on each, it checks input.equals(undoIt(doIt(input))); or something like that.

If any of the tests fails, you'll know where to start looking.

like image 137
Olathe Avatar answered Oct 04 '22 08:10

Olathe