-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCryptographer.java
More file actions
135 lines (112 loc) · 4.74 KB
/
FileCryptographer.java
File metadata and controls
135 lines (112 loc) · 4.74 KB
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
package Cryptography;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class Cryptography {
public static void operation(int key) {
// Create a file chooser dialog to select a file
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
// File input stream for reading input
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[fis.available()];
fis.read(data);
// Print the byte values of the data array
for (byte b : data) {
System.out.print(b + " ");
}
int i = 0;
for (byte b : data) {
// XOR operation on each byte of data with the given key
data[i] = (byte) (b ^ key);
i++;
}
// File output stream for writing modified data back to the file
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
fis.close();
// Display a message dialog to indicate successful operation
JOptionPane.showMessageDialog(null, "Done");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ignored) {}
// GUI setup
System.out.println("Hello, This is the Image Cryptography model made by Sunder Singh");
JFrame f = new JFrame();
JLabel label = new JLabel();
label.setText("THE IMAGE CODER / DECODER");
label.setFont(new Font("PLAIN", Font.BOLD, 25));
label.setBackground(Color.GREEN);
label.setOpaque(true);
JLabel labelB = new JLabel();
ImageIcon img = new ImageIcon("D:\\Codes\\eclipse workspace\\mini_project_4th_sem\\src\\newicon.gif");
labelB.setIcon(img);
f.setTitle("Image Cryptography System - By Sunder Singh");
f.setSize(500, 500);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("ROBOTO", Font.BOLD, 25);
JButton button = new JButton();
button.setText("Encrypt");
button.setFont(new Font("PLAIN", Font.PLAIN, 20));
JButton button1 = new JButton();
button1.setText("Decrypt");
button1.setFont(new Font("PLAIN", Font.PLAIN, 20));
JTextField textfield = new JTextField(10);
textfield.setFont(font);
textfield.setPreferredSize(new Dimension(250, 40));
textfield.setFont(new Font("PLAIN", Font.PLAIN, 35));
textfield.setBackground(Color.white);
textfield.setCaretColor(Color.black);
// ActionListener for the "Encrypt" button
button.addActionListener(e -> {
System.out.println("Button clicked");
String text = textfield.getText();
if (!text.isEmpty()) {
try {
int temp = Integer.parseInt(text);
operation(temp); // Call the operation method with the entered key
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid key format. Please enter a valid integer key.");
}
} else {
JOptionPane.showMessageDialog(null, "Please enter a key.");
}
textfield.setText(null);
});
// ActionListener for the "Decrypt" button (same as Encrypt button)
button1.addActionListener(e -> {
System.out.println("Button clicked");
String text = textfield.getText();
if (!text.isEmpty()) {
try {
int temp = Integer.parseInt(text);
operation(temp); // Call the operation method with the entered key
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid key format. Please enter a valid integer key.");
}
} else {
JOptionPane.showMessageDialog(null, "Please enter a key.");
}
textfield.setText(null);
});
// Add components to the JFrame
f.setLayout(new FlowLayout());
f.add(label);
f.add(button);
f.add(button1);
f.add(textfield);
f.add(labelB);
f.setVisible(true);
}
}