Saturday, February 20, 2016

Encrypting Image Files

This program encrypts jpg and png images. It works by repositioning all image pixels. It supports passwords of up to 20 characters (a-z, A-Z, and 0-9) and can be made longer easily. The filename of the saved encrypted image is "result.png". Entering the correct password into the text field and pressing the "Decrypt" button will convert it back into its original form.



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.awt.*;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class ImageEncryptor extends JFrame{
    private BufferedImage bufimage=null;
    private ImageIcon icon=null;
    private JTextField tfield=null;
    private JLabel label=null;
    
    public ImageEncryptor(){
        super("Image Encryptor");
        setSize(800,600);
        
        setLayout(new BorderLayout());        
        
        JButton bopen=new JButton("Open");
        bopen.addActionListener(new ButtonHandler());
        
        tfield=new JTextField(20);
        
        JButton bencrypt=new JButton("Encrypt");
        bencrypt.addActionListener(new ButtonHandler());
        
        JButton bdecrypt=new JButton("Decrypt");
        bdecrypt.addActionListener(new ButtonHandler());

        JPanel p1=new JPanel();
        p1.add(bopen);
        p1.add(tfield);
        p1.add(bencrypt);
        p1.add(bdecrypt);
        add(p1,BorderLayout.NORTH);
        
        label=new JLabel();
        
        JPanel p2=new JPanel();
        p2.add(label);
        add(p2,BorderLayout.CENTER);
        
        JButton bsave=new JButton("Save");
        bsave.addActionListener(new ButtonHandler());
        
        JPanel p3=new JPanel();
        p3.add(bsave);
        add(p3,BorderLayout.SOUTH);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        show();
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getActionCommand()=="Open"){
                FileDialog fd=new FileDialog(ImageEncryptor.this,"Open Image File",FileDialog.LOAD);
                fd.setDirectory("C:\\");
                fd.setVisible(true);
                String filename=fd.getFile();
                if (filename!= null){
                    try{
                        bufimage=ImageIO.read(new File(filename));
                    }catch(Exception ex){
                    }
                    icon=new ImageIcon(bufimage);
                    label.setIcon(icon);
                }
            }
            if(e.getActionCommand()=="Encrypt"){
                int width=bufimage.getWidth();
                int height=bufimage.getHeight();
                
                String passkey=tfield.getText();
                if(passkey==null||passkey.length()>20){
                    return;
                }
                int passkeyint[]=new int[passkey.length()];
                
                for(int i=0;i<passkey.length();i++){
                    if((passkey.charAt(i)>='0'&&passkey.charAt(i)<='9')||(passkey.charAt(i)>='a'&&passkey.charAt(i)<='z')||(passkey.charAt(i)>='A'&&passkey.charAt(i)<='Z')){
                        passkeyint[i]=Integer.valueOf(passkey.charAt(i));
                    }else{
                        return;
                    }
                }
                
                int counter=0;
                int pixels[]=new int[width*height];
                for(int i=0;i<height;i++){
                    for(int j=0;j<width;j++){
                        pixels[counter]=bufimage.getRGB(j,i);
                        counter++;
                    }
                }
                
                ArrayList<Integer> arrayList=new ArrayList<Integer>();
                
                int pos=0;
                counter=0;
                for(int i=0;i<pixels.length;i++){
                    int temp=pos+passkeyint[counter];
                    if(i==temp){
                        int counter2=0;
                        for(int j=pos;j<(pos+passkeyint[counter]);j++){
                            arrayList.add(counter2,pixels[j]);
                            counter2++;
                        }
                        counter2=0;
                        Collections.reverse(arrayList);
                        for(int j=pos;j<(pos+passkeyint[counter]);j++){
                            pixels[j]=arrayList.get(counter2);
                            counter2++;
                        }
                        arrayList.clear();
                        
                        pos=pos+passkeyint[counter];
                        if(pos>=(pixels.length))
                            break;
                        
                        counter++;
                        if(counter==passkeyint.length){
                            counter=0;
                        }
                    }
                }
                
                counter=0;
                for(int i=0;i<height;i++){
                    for(int j=0;j<width;j++){
                        bufimage.setRGB(j,i,pixels[counter]);
                        counter++;
                    }
                }
                
                icon=new ImageIcon(bufimage);
                label.setIcon(icon);
            }
            if(e.getActionCommand()=="Decrypt"){
                int width=bufimage.getWidth();
                int height=bufimage.getHeight();
                
                String passkey=tfield.getText();
                if(passkey==null||passkey.length()>20){
                    return;
                }
                int passkeyint[]=new int[passkey.length()];
                
                for(int i=0;i<passkey.length();i++){
                    if((passkey.charAt(i)>='0'&&passkey.charAt(i)<='9')||(passkey.charAt(i)>='a'&&passkey.charAt(i)<='z')||(passkey.charAt(i)>='A'&&passkey.charAt(i)<='Z')){
                        passkeyint[i]=Integer.valueOf(passkey.charAt(i));
                    }else{
                        return;
                    }
                }
                
                int counter=0;
                int pixels[]=new int[width*height];
                for(int i=0;i<height;i++){
                    for(int j=0;j<width;j++){
                        pixels[counter]=bufimage.getRGB(j,i);
                        counter++;
                    }
                }
                
                ArrayList<Integer> arrayList=new ArrayList<Integer>();
                
                int pos=0;
                counter=0;
                int counter2=0;
                for(int i=0;i<pixels.length;i++){
                    int temp=pos+passkeyint[counter];
                    if(i==temp){
                        counter2=0;
                        for(int j=pos;j<(pos+passkeyint[counter]);j++){
                            arrayList.add(counter2,pixels[j]);
                            counter2++;
                        }
                        counter2=0;
                        Collections.reverse(arrayList);
                        for(int j=pos;j<(pos+passkeyint[counter]);j++){
                            pixels[j]=arrayList.get(counter2);
                            counter2++;
                        }
                        arrayList.clear();
                        
                        pos=pos+passkeyint[counter];
                        if(pos>=(pixels.length))
                            break;
                        
                        counter++;
                        if(counter==passkeyint.length){
                            counter=0;
                        }
                    }
                }
                
                counter=0;
                counter2=0;
                for(int i=0;i<height;i++){
                    for(int j=0;j<width;j++){
                        bufimage.setRGB(j,i,pixels[counter]);
                        counter++;
                        if(counter2==passkeyint.length){
                            counter2=0;
                        }
                    }
                }
                
                icon=new ImageIcon(bufimage);
                label.setIcon(icon);
            }
            if(e.getActionCommand()=="Save"){
                try{
                    ImageIO.write(bufimage,"png",new File("result.png"));
                }
                catch(Exception ex){
                }
            }
        }
    }
    
    public static void main(String[] args){
        ImageEncryptor ap=new ImageEncryptor();
    }
}

No comments:

Post a Comment