From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Master/Kryptografie/krypto1/.classpath | 6 + Master/Kryptografie/krypto1/.project | 19 + Master/Kryptografie/krypto1/IntVerifier.java | 33 ++ Master/Kryptografie/krypto1/KryptArith.java | 97 +++ Master/Kryptografie/krypto1/KryptModel.java | 205 +++++++ Master/Kryptografie/krypto1/KryptTool.java | 855 +++++++++++++++++++++++++++ 6 files changed, 1215 insertions(+) create mode 100644 Master/Kryptografie/krypto1/.classpath create mode 100644 Master/Kryptografie/krypto1/.project create mode 100644 Master/Kryptografie/krypto1/IntVerifier.java create mode 100644 Master/Kryptografie/krypto1/KryptArith.java create mode 100644 Master/Kryptografie/krypto1/KryptModel.java create mode 100644 Master/Kryptografie/krypto1/KryptTool.java (limited to 'Master/Kryptografie/krypto1') diff --git a/Master/Kryptografie/krypto1/.classpath b/Master/Kryptografie/krypto1/.classpath new file mode 100644 index 0000000..d736018 --- /dev/null +++ b/Master/Kryptografie/krypto1/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Master/Kryptografie/krypto1/.project b/Master/Kryptografie/krypto1/.project new file mode 100644 index 0000000..dcfc888 --- /dev/null +++ b/Master/Kryptografie/krypto1/.project @@ -0,0 +1,19 @@ + + + krypto1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.jdt.core.javanature + org.eclipse.jem.beaninfo.BeanInfoNature + + diff --git a/Master/Kryptografie/krypto1/IntVerifier.java b/Master/Kryptografie/krypto1/IntVerifier.java new file mode 100644 index 0000000..db9baeb --- /dev/null +++ b/Master/Kryptografie/krypto1/IntVerifier.java @@ -0,0 +1,33 @@ +import javax.swing.InputVerifier; +import javax.swing.JComponent; +import javax.swing.JTextField; +import java.awt.Toolkit; + +/** + * @author Andreas Spirka, Sven Eisenhauer + * InputVerifier zur Überprüfung auf Integer-Werte + */ +public class IntVerifier extends InputVerifier { + + /* (non-Javadoc) + * @see javax.swing.InputVerifier#verify(javax.swing.JComponent) + */ + public boolean verify(JComponent arg0) { + boolean retVal=false; + JTextField tf = (JTextField) arg0; + String regexp = "[0-9]+"; + retVal=tf.getText().matches(regexp); + if(!retVal) + { + Toolkit.getDefaultToolkit().beep(); + } + return retVal; + } + /* (non-Javadoc) + * @see javax.swing.InputVerifier#shouldYieldFocus(javax.swing.JComponent) + */ + public boolean shouldYieldFocus(JComponent input) + { + return verify(input); + } +} diff --git a/Master/Kryptografie/krypto1/KryptArith.java b/Master/Kryptografie/krypto1/KryptArith.java new file mode 100644 index 0000000..d8faa16 --- /dev/null +++ b/Master/Kryptografie/krypto1/KryptArith.java @@ -0,0 +1,97 @@ +/** + * + */ + +/** + * @author sven + * + */ + +import java.math.*; +import java.util.Random; + +public class KryptArith { + char bottomChar='~'; + char topChar='!'; + int bottomInt=(int) bottomChar; + int topInt=(int) topChar; + + public BigInteger modInv(BigInteger a, BigInteger b) + { + BigInteger r1 = new BigInteger("0"); + BigInteger r2 = new BigInteger("1"); + BigInteger a_new = new BigInteger("0"); + BigInteger b_new = new BigInteger("0"); + BigInteger r1_new = BigInteger.ZERO; + BigInteger r2_new = BigInteger.ZERO; + + while (b.compareTo(BigInteger.ONE) != 0) + { + a_new=b; + try + { + b_new=a.mod(b); + } + catch (Exception e) + { + System.out.println("No Inverse "+e); + //System.exit(1); + return BigInteger.ZERO.subtract(BigInteger.ONE); + } + r1_new = r2; + r2_new=r1.subtract(r2.multiply(a.divide(b))); + a=a_new; + b=b_new; + r1=r1_new; + r2=r2_new; + } + return r2; + } + public int modInv(int a, int b) throws Exception + { + int r1 = 1; + int r2 = 0; + int r1_new = 1; + int r2_new = 0; + int a_new = 0; + int b_new = 0; + + while (b_new != 1) + { + a_new=b; + /*try + { + b_new=a % b; + } + catch (Exception e) + { + //System.out.println("No Inverse "+e); + //System.exit(1); + return -1; + }*/ + b_new=a % b; + r1_new = r2; + r2_new = r1 - ((a / b) * r2); + a=a_new; + b=b_new; + r1=r1_new; + r2=r2_new; + } + + return r2; + } + public int gcd(int a, int b) + { + if (b==0) return a; + return gcd(b,a%b); + } + public BigInteger gcd(BigInteger a, BigInteger b) + { + if (b.compareTo(BigInteger.ZERO) == 0) return a; + return gcd(b,a.mod(b)); + } + public static int gen8bitPrim() { + Random rnd = new Random(); + return BigInteger.probablePrime(8, rnd).intValue(); + } +} 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