summaryrefslogtreecommitdiffstats
path: root/Master/Kryptografie/krypto1/KryptModel.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Kryptografie/krypto1/KryptModel.java
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Kryptografie/krypto1/KryptModel.java')
-rw-r--r--Master/Kryptografie/krypto1/KryptModel.java205
1 files changed, 205 insertions, 0 deletions
diff --git a/Master/Kryptografie/krypto1/KryptModel.java b/Master/Kryptografie/krypto1/KryptModel.java
new file mode 100644
index 0000000..5027ff2
--- /dev/null
+++ b/Master/Kryptografie/krypto1/KryptModel.java
@@ -0,0 +1,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();
+ }
+}