summaryrefslogtreecommitdiffstats
path: root/Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java
diff options
context:
space:
mode:
Diffstat (limited to 'Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java')
-rw-r--r--Master/Kryptografie/prakt3/prakt3WS/p3/src/otp/OTP.java167
1 files changed, 167 insertions, 0 deletions
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());*/
+ }
+
+}