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
|
package otp;
import java.util.Random;
import java.util.Observable;
public class OTP extends Observable {
LinReg enc_reg = null;
LinReg dec_reg = null;
Random rnd = new Random();
int config;
int init;
String plain="";
String cipher="";
public int[] getKeys()
{
int [] keys = new int[2];
keys[0] = config;
keys[1] = init;
return keys;
}
public String getPlain() {
return this.plain;
}
public String getCipher() {
return this.cipher;
}
public void setPlain(String p)
{
this.plain=p;
}
public void setCypher(String c)
{
this.cipher=c;
}
public void encrypt()
{
this.cipher = this.encrypt(this.config, this.init, this.plain);
}
public void decrypt()
{
this.plain = this.decrypt(this.config, this.init, this.cipher);
}
public int encryptOne(int clear, int key)
{
int cipher_int=0;
int keyBit=0;
int clearBit=0;
int cipherBit=0;
for (int i=7;i>=0;i--)
{
clearBit = clear & (1<<i);
keyBit = key & (1<<i);
cipherBit = keyBit ^ clearBit;
cipher_int |= cipherBit;
}
return cipher_int;
}
public int decryptOne(int cipher, int key)
{
int clear = encryptOne(cipher,key);
return clear;
}
private int encode(char c)
{
int ret = (int) c;
return ret;
}
private char decode(int i)
{
char ret = (char) i;
return ret;
}
public String encrypt(int config, int init, String plain)
{
enc_reg = new LinReg(config,init);
String cipherStr=new String();
int key=0;
int plain_int=0;
int cipher_int = 0;
char cipher_char = ' ';
char[] cipher_chars= new char[plain.length()];
for (int i = 0;i < plain.length(); i++)
{
key=enc_reg.getNextKey();
plain_int = encode(plain.charAt(i));
cipher_int = encryptOne(plain_int,key);
cipher_char = decode(cipher_int);
cipher_chars[i]=cipher_char;
}
cipherStr = String.copyValueOf(cipher_chars);
return cipherStr;
}
public String decrypt(int config, int init, String cipher)
{
dec_reg = new LinReg(config,init);
String plainStr=new String();
int key=0;
int plain_int=0;
int cipher_int = 0;
char plain_char = ' ';
char[] plain_chars= new char[cipher.length()];
for (int i = 0;i < cipher.length(); i++)
{
key=dec_reg.getNextKey();
cipher_int=encode(cipher.charAt(i));
plain_int=decryptOne(cipher_int,key);
plain_char=decode(plain_int);
plain_chars[i]=plain_char;
}
plainStr = String.copyValueOf(plain_chars);
return plainStr;
}
public void initialize()
{
setKeys(rnd.nextInt(0xff), rnd.nextInt(0xff));
}
public void setKeys(int c, int i)
{
config=c;
init=i;
enc_reg = new LinReg(config,init);
dec_reg = new LinReg(config,init);
}
public void doIt()
{
//int config=0x56;
//int init=0xf4;
this.initialize();
/* String plain = "A quite long text";
String cipher= new String();
cipher=encrypt(config,init,plain);
System.out.println("Plain: "+plain.toUpperCase());
System.out.println("Cipher ASCII: "+cipher.toLowerCase());
System.out.print("Cipher dec: ");
for (int i=0;i<cipher.length();i++)
{
System.out.print((int)cipher.charAt(i)+" ");
}
System.out.println();
System.out.print("Cipher hex: ");
for (int i=0;i<cipher.length();i++)
{
System.out.print(Integer.toHexString((int)cipher.charAt(i))+" ");
}
System.out.println();
System.out.println("Decrypted: "+decrypt(config,init,cipher).toUpperCase());*/
}
}
|