diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Kryptografie/prakt3/prakt3WS/p3/src/otp | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Master/Kryptografie/prakt3/prakt3WS/p3/src/otp')
| -rw-r--r-- | Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/LinReg.java | 100 | ||||
| -rw-r--r-- | Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java | 167 |
2 files changed, 267 insertions, 0 deletions
diff --git a/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/LinReg.java b/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/LinReg.java new file mode 100644 index 0000000..1e82b0e --- /dev/null +++ b/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/LinReg.java @@ -0,0 +1,100 @@ +package otp;
+
+public class LinReg {
+ private int config;
+ private int status;
+
+ public static final int bitMask = 0xff;
+ public static final int bitLength = 8;
+
+ public LinReg (int config, int init)
+ {
+ this.config = 0;
+ this.status = 0;
+ this.config = config & bitMask;
+ this.status = init & bitMask;
+ //printOut();
+ }
+
+ public int getNextKeyBit()
+ {
+ return this.getNextBit();
+ }
+
+ public int getNextKey()
+ {
+ int ret=0;
+ int newBit;
+ for (int i=7;i>=0;i--)
+ {
+ newBit = getNextBit();
+ //System.out.println(i+". key bit is: "+Integer.toBinaryString(newBit));
+ ret += ( newBit << i);
+ //System.out.println("act key is: "+LinReg.to8BitString(ret));
+ }
+ //System.out.println("New key is: "+LinReg.to8BitString(ret));
+ return ret & bitMask;
+ }
+
+ private int getNextBit()
+ {
+ //printOut();
+ int ret=0;
+ //boolean tmp=false;
+ int cnt=0;
+ // left shift status to get next bit at bitLength + 1;
+ this.status = this.status << 1;
+ //System.out.println("Left shifted status: "+Integer.toBinaryString(this.status));
+ // get bit at bitLength + 1;
+ ret = this.status & (~bitMask);
+ //System.out.println("key bit unshifted is: "+Integer.toBinaryString(ret));
+ ret >>= bitLength;
+ //System.out.println("New key bit is: "+Integer.toBinaryString(ret));
+ // calculate next bit from status and config
+ for (int i = 7; i >= 0; i--)
+ {
+ // test if actual status and config bit are
+ // set at position i
+ //if ( (this.status & i) && (this.config & i) )
+ int statusBit = ((this.status & (1<<i) ) >> i);
+ int configBit = ((this.config & (1<<i) ) >> i);
+
+ //System.out.println("Bit in Status: "+Integer.toBinaryString(statusBit));
+ //System.out.println("Bit in Config: "+Integer.toBinaryString(configBit));
+
+ if ( (statusBit & configBit) == 1 )
+ {
+ //System.out.println("Bit "+i+" is set in C and S");
+ //tmp=!tmp;
+ cnt++;
+ }
+ }
+ if ( (cnt%2) == 1 )
+ {
+ this.status |= 1;
+ //System.out.println("Appending 1 to status");
+ }
+ else
+ {
+ //System.out.println("Appending 0 to status");
+ }
+ this.status &= bitMask;
+ //printOut();
+ return ret;
+ }
+ public void printOut()
+ {
+ System.out.println("Config is: "+to8BitString(this.config));
+ System.out.println("Status is: "+to8BitString(this.status));
+ }
+
+ public static final String to8BitString(int input)
+ {
+ String res = Integer.toBinaryString(input);
+ for (int i = res.length();i<bitLength;i++)
+ {
+ res="0"+res;
+ }
+ return res;
+ }
+}
diff --git a/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java b/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java new file mode 100644 index 0000000..b020aad --- /dev/null +++ b/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java @@ -0,0 +1,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());*/
+ }
+
+}
|
