summaryrefslogtreecommitdiffstats
path: root/Master/Kryptografie/krypto1/KryptModel.java
blob: 5027ff2bf1490613f604e237fef01032bc5daee1 (plain)
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
import java.util.Observable;

public class KryptModel extends Observable {
	private int key_t;
	private int key_s;
	private int mod;
	private char[][] knownPlain;
	private String plain;
	private String cipher;
	private String method;
	private int modInv;
	private String status;
	
	private KryptArith myKA=null;
	
	public KryptModel() {
		super();
		setMethod("TAUSCH");
		key_t=1;
		key_s=1;
		mod=1;
		myKA=new KryptArith();
		calcModInv();
		plain="";
		cipher="";
		knownPlain=new char[2][2];
		knownPlain[0][0]='1';
		knownPlain[0][1]='1';
		knownPlain[1][0]='1';
		knownPlain[1][1]='1';
	}
	public int calcModInv()
	{
		try {
			modInv=myKA.modInv(key_t, mod);
		}
		catch (Exception ae) {
			status="Calculating mod. Inverse failed";
			return 1;
		}
		status="Calculated mod. Inverse successfully";
		setChanged();
		notifyObservers();
		return 0;
	}
	
	public void setkey_t(int t)
	{
		key_t=t;
		status="key t set successfully";
		setChanged();
		notifyObservers();
	}
	public void setkey_s(int s)
	{
		key_s=s;
		status="key s set successfully";
		setChanged();
		notifyObservers();
	}
	public void setmod(int m)
	{
		mod=m;
		status="modulus m set successfully";
		setChanged();
		notifyObservers();
	}
	public void setPlain(String p)
	{
		plain=p.toLowerCase();
		setChanged();
		notifyObservers();
	}
	public void setCipher(String c)
	{
		cipher=c.toUpperCase();
		setChanged();
		notifyObservers();
	}
	public void setMethod(String m)
	{
		method=m;
		setChanged();
		notifyObservers();
	}
	public void setKnownPlain(char p1,char c1, char p2, char c2)
	{
		knownPlain[0][0]=p1;
		knownPlain[1][0]=p2;
		knownPlain[0][1]=c1;
		knownPlain[1][1]=c2;
		status="Known plaintext chars set. p1: "+
			knownPlain[0][0]+" p2: "+
			knownPlain[1][0]+" c1: "+
			knownPlain[0][1]+" c2: "+
			knownPlain[1][1];
		setChanged();
		notifyObservers();
	}
	
	public String getStatus() {
		return status;
	}
	
	public int getkey_t()
	{
		return key_t;
	}
	public int getkey_s()
	{
		return key_s;
	}
	public int getmod()
	{
		return mod;
	}
	public int getModInv()
	{
		return modInv;
	}
	public String getplain()
	{
		return plain;
	}
	public String getcipher()
	{
		return cipher;
	}
	public void encrypt()
	{
		if (method.equals("TAUSCH"))
		{
			if ( plain.isEmpty() == false)
			{
				char[] temp=plain.toCharArray();
				int tmp = 0;
				for (int i=0;i<temp.length;i++)
				{
					tmp=(int)(temp[i]);
					//tmp-=97;
					tmp=((tmp + key_s) * key_t) % mod;
					//tmp+=97;
					temp[i]=(char)(tmp);
				}
				setCipher(new String(temp));
				status="Encrypted successfully";
			}
		}
		setChanged();
		notifyObservers();
	}
	public void decrypt()
	{
		if (method.equals("TAUSCH"))
		{
			if ( cipher.isEmpty() == false)
			{
				char[] temp=cipher.toLowerCase().toCharArray();
				int tmp = 0;
				for (int i=0;i<temp.length;i++)
				{
					tmp=(int)(temp[i]);
					//tmp-=97;
					tmp= (tmp*modInv - key_s ) % mod;
					//tmp+=97;
					temp[i]=(char)(tmp);
				}
				setPlain(new String(temp));
				status="Decrypted successfully";
			}
		}
		setChanged();
		notifyObservers();
	}
	public void attack() {
		char p1=knownPlain[0][0];
		char p2=knownPlain[1][0];
		char c1=knownPlain[0][1];
		char c2=knownPlain[1][1];
		/*
		int ci1,ci2,pi1,pi2;
		
		ci1=((int)c1);
		ci2=((int)c2);
		pi1=((int)p1);
		pi2=((int)p2);
			*/
		try {
			key_t=( (c1-c2) * myKA.modInv(p1-p2,mod) )%mod;
			key_s = ( ( c1 * myKA.modInv(key_t,mod) ) - p1 ) % mod;
			status="Keys found";
		}
		catch (Exception e) {
			status="Could not find keys";
		}
		setChanged();
		notifyObservers();
	}
	public void genMod() {
		setmod(KryptArith.gen8bitPrim());
		status="Prime successfully generated";
		setChanged();
		notifyObservers();
	}
}