-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCryptography.java
More file actions
127 lines (88 loc) · 3.1 KB
/
Cryptography.java
File metadata and controls
127 lines (88 loc) · 3.1 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
import java.io.*;
import java.util.*;
class Cryptography{
static boolean encrypt(String src, String trgt){
try{
KeyGenerator kgen = new KeyGenerator(1234);
ArmstrongCryptograph ac = new ArmstrongCryptograph(kgen.getKey());
ColorCryptograph cc = new ColorCryptograph(kgen.getKey());
FileInputStream fin = new FileInputStream(src);
FileOutputStream fout = new FileOutputStream(trgt);
int x, y, z;
while((x = fin.read()) != -1){
y = ac.encrypt(x);
z = cc.encrypt(y);
fout.write(z);
}//while
System.out.println("C");
fin.close();
fout.close();
System.out.println("D");
return true;
}catch(Exception ex){
System.out.println("Error in encrypt: " + ex.getMessage());
//ex.printStackTrace();
return false;
}
}//encrypt
static boolean decrypt(String src, String trgt){
try{
KeyGenerator kgen = new KeyGenerator(1234);
ArmstrongCryptograph ac = new ArmstrongCryptograph(kgen.getKey());
ColorCryptograph cc = new ColorCryptograph(kgen.getKey());
FileInputStream fin = new FileInputStream(src);
FileOutputStream fout = new FileOutputStream(trgt);
int x, y,z;
while((z = fin.read()) != -1){
y = cc.decrypt(z);
x = ac.decrypt(y);
fout.write(x);
}//while
fin.close();
fout.close();
return true;
}catch(Exception ex){
System.out.println("Error in decrypt: " + ex.getMessage());
return false;
}
}//decrypt
public static void main(String args[]){
try{
String srcFile, trgtFile;
Scanner scn = new Scanner(System.in);
int ch;
while(true){
System.out.println("1. Encrypt ");
System.out.println("2. Decrypt ");
System.out.println("3. Exit");
ch = scn.nextInt();
if(ch == 1){
System.out.println("Enter the absolute path of file to encrypt");
srcFile = scn.next();
System.out.println("Enter the absolute path of target file");
//scn.next();
trgtFile = scn.next();
if(encrypt(srcFile, trgtFile))
System.out.println("Encryption Successfull");
else
System.out.println("Encryption Failed");
}else if(ch == 2){
System.out.println("Enter the absolute path of file to decrypt");
srcFile = scn.next();
System.out.println("Enter the absolute path of target file");
trgtFile = scn.next();
if(decrypt(srcFile, trgtFile))
System.out.println("Decryption Successfull");
else
System.out.println("Decryption Failed");
}else if(ch == 3){
break;
}else{
System.out.println("Wrong Choice");
}
}//while
}catch(Exception ex){
System.out.println("Error in decrypt: " + ex.getMessage());
}
}//main
}