commit d33c7d31d00fb47c0b1412dff2d1a2d47418615a Author: Paul Date: Sun Oct 18 13:48:50 2020 +0200 inital commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..4aa91ea --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/artifacts/AES_CTR_jar.xml b/.idea/artifacts/AES_CTR_jar.xml new file mode 100644 index 0000000..6dea945 --- /dev/null +++ b/.idea/artifacts/AES_CTR_jar.xml @@ -0,0 +1,11 @@ + + + $PROJECT_DIR$/out/artifacts/AES_CTR_jar + + + + + + + + \ No newline at end of file diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..ab2dc53 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..a81c43e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..48bd1d6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-template.xml b/.idea/project-template.xml new file mode 100644 index 0000000..d57a956 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/AES_CTR.iml b/AES_CTR.iml new file mode 100644 index 0000000..bcd2974 --- /dev/null +++ b/AES_CTR.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/AES_CTR.jar b/AES_CTR.jar new file mode 100644 index 0000000..6026378 Binary files /dev/null and b/AES_CTR.jar differ diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF new file mode 100644 index 0000000..74bc389 --- /dev/null +++ b/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: tech.loedige.Main + diff --git a/Readme.txt b/Readme.txt new file mode 100644 index 0000000..c76d688 --- /dev/null +++ b/Readme.txt @@ -0,0 +1,4 @@ +Name: Paul Lödige +Matrikel Nr.: 15405036 + +Projekt als ausführbare jar-Datei vorhanden \ No newline at end of file diff --git a/out/artifacts/AES_CTR_jar/AES_CTR.jar b/out/artifacts/AES_CTR_jar/AES_CTR.jar new file mode 100644 index 0000000..6026378 Binary files /dev/null and b/out/artifacts/AES_CTR_jar/AES_CTR.jar differ diff --git a/out/production/AES_CTR/tech/loedige/AdditiveStreamCipher.class b/out/production/AES_CTR/tech/loedige/AdditiveStreamCipher.class new file mode 100644 index 0000000..86b6bd9 Binary files /dev/null and b/out/production/AES_CTR/tech/loedige/AdditiveStreamCipher.class differ diff --git a/out/production/AES_CTR/tech/loedige/KeyStreamGenerator.class b/out/production/AES_CTR/tech/loedige/KeyStreamGenerator.class new file mode 100644 index 0000000..e7a589a Binary files /dev/null and b/out/production/AES_CTR/tech/loedige/KeyStreamGenerator.class differ diff --git a/out/production/AES_CTR/tech/loedige/KeyStreamGenerator_AES_CTR.class b/out/production/AES_CTR/tech/loedige/KeyStreamGenerator_AES_CTR.class new file mode 100644 index 0000000..9fd7c97 Binary files /dev/null and b/out/production/AES_CTR/tech/loedige/KeyStreamGenerator_AES_CTR.class differ diff --git a/out/production/AES_CTR/tech/loedige/Main.class b/out/production/AES_CTR/tech/loedige/Main.class new file mode 100644 index 0000000..1ac6fbb Binary files /dev/null and b/out/production/AES_CTR/tech/loedige/Main.class differ diff --git a/out/production/AES_CTR/util/Dump.class b/out/production/AES_CTR/util/Dump.class new file mode 100644 index 0000000..515d45c Binary files /dev/null and b/out/production/AES_CTR/util/Dump.class differ diff --git a/out/production/AES_CTR/util/IllegalHexDumpException.class b/out/production/AES_CTR/util/IllegalHexDumpException.class new file mode 100644 index 0000000..f6d1218 Binary files /dev/null and b/out/production/AES_CTR/util/IllegalHexDumpException.class differ diff --git a/src/tech/loedige/AdditiveStreamCipher.java b/src/tech/loedige/AdditiveStreamCipher.java new file mode 100644 index 0000000..f445b56 --- /dev/null +++ b/src/tech/loedige/AdditiveStreamCipher.java @@ -0,0 +1,10 @@ +package tech.loedige; + +public class AdditiveStreamCipher { + + static public void encrypt(KeyStreamGenerator ksg, byte[] m) { + for (int i = 0; i < m.length; ++i) { + m[i] ^= ksg.getNextKeyStreamByte(m[i]); + } + } +} diff --git a/src/tech/loedige/KeyStreamGenerator.java b/src/tech/loedige/KeyStreamGenerator.java new file mode 100644 index 0000000..6d813bc --- /dev/null +++ b/src/tech/loedige/KeyStreamGenerator.java @@ -0,0 +1,12 @@ +package tech.loedige; + +public abstract class KeyStreamGenerator { + /** + * Berechnung des nächsten Schlüsselstrombytes. + * (Entspricht einem einmaligen Aufruf der + * Update-Funktion s und der Extraktions-Funktion S.) + * + * @return Nächstes Schlüsselstrombyte + */ + abstract byte getNextKeyStreamByte(byte mByte); +} diff --git a/src/tech/loedige/KeyStreamGenerator_AES_CTR.java b/src/tech/loedige/KeyStreamGenerator_AES_CTR.java new file mode 100644 index 0000000..3d1bf53 --- /dev/null +++ b/src/tech/loedige/KeyStreamGenerator_AES_CTR.java @@ -0,0 +1,64 @@ +package tech.loedige; + +import javax.crypto.*; +import javax.crypto.spec.SecretKeySpec; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class KeyStreamGenerator_AES_CTR extends KeyStreamGenerator { + + private Cipher cipher; + private final byte[] state = new byte[16]; + private final byte[] ctr = new byte[16]; + private int currentByte=0; + + public void initState(byte[] key, byte[] iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + if((iv.length!=16)||(key.length!=16)){ + throw new IllegalArgumentException("Invalid input length"); + } + System.arraycopy(iv,0,ctr,0,16); + + SecretKey secretKey = new SecretKeySpec(key,"AES"); + cipher = Cipher.getInstance("AES/ECB/NOPADDING"); + cipher.init(Cipher.ENCRYPT_MODE,secretKey); + + System.arraycopy(cipher.doFinal(ctr),0,state,0,16); + } + + @Override + byte getNextKeyStreamByte(byte mByte) { + if(currentByte>=16){ + try { + updateStateBlock(); + } catch (BadPaddingException | IllegalBlockSizeException e) { + e.printStackTrace(); + } + currentByte=0; + } + return state[currentByte++]; + } + + private void updateStateBlock() + throws BadPaddingException, IllegalBlockSizeException { + incrementCtr(); + System.arraycopy(cipher.doFinal(ctr),0,state,0,16); + } + + /** + * inkrementiert den counter + */ + private void incrementCtr(){ + for(int i = 0; i<16; i++){ + //falls das Byte die volle Länge erreicht hat findet ein Übertrag statt + if(ctr[i]==0xff){ + ctr[i]=0x00; + } + else{ + //falls das Byte nicht die volle Länge hat endet die Inkrementierung + ctr[i]++; + return; + } + } + } +} diff --git a/src/tech/loedige/Main.java b/src/tech/loedige/Main.java new file mode 100644 index 0000000..1e7e567 --- /dev/null +++ b/src/tech/loedige/Main.java @@ -0,0 +1,33 @@ +package tech.loedige; + +import util.Dump; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +public class Main { + + public static void main(String[] args) + throws Exception { + byte[] key = Dump.hexString2byteArray("0102030405060708090A0B0C0D0E0F10"); + SecretKey secretKey = new SecretKeySpec(key, "AES"); + Cipher cipher= Cipher.getInstance("AES/CTR/NOPADDING"); + byte[] ctr = Dump.hexString2byteArray("FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE"); + IvParameterSpec iv = new IvParameterSpec(ctr); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); + byte[] m = new byte[35]; + byte[] c = cipher.doFinal(m); + System.out.println("AES-CTR-Verschlüsselung mit "+ + "Cipher-Instanz vom Typ AES/CTR/NOPADDING:"); + System.out.println(Dump.dump(c)); + System.out.println(); + KeyStreamGenerator_AES_CTR ksg= new KeyStreamGenerator_AES_CTR(); + ksg.initState(key, ctr); + AdditiveStreamCipher.encrypt(ksg, m); + System.out.println("AES-CTR-Verschlüsselung mit "+ "KeyStreamGenerator_AES_CTR-Implementierung:"); + System.out.println(Dump.dump(m)); + System.out.println(); + } +} \ No newline at end of file diff --git a/src/util/Dump.java b/src/util/Dump.java new file mode 100644 index 0000000..2aec87d --- /dev/null +++ b/src/util/Dump.java @@ -0,0 +1,215 @@ +/* + * Stefan Heiss + * TH Ostwestfalen-Lippe + * FB Elektrotechnik und Technische Informatik + * Quellcode zur Lehrveranstaltung Datensicherheit + */ +package util; + +import java.math.BigInteger; +import java.util.Formatter; + +public class Dump { + + public final static String NL + = System.getProperty("line.separator", "\r\n"); + + public static byte[] hexString2byteArray( + String hexString) { + // remove all whitespaces + hexString = hexString.replaceAll("\\s", ""); + hexString = hexString.replaceAll(":", ""); + + if ((hexString.length() & 0x01) != 0) { + throw new IllegalHexDumpException( + "Odd number of nibbles!"); + } + int byteArrayLength = hexString.length() >> 1; + byte[] ba = new byte[byteArrayLength]; + for (int i = 0; i < byteArrayLength; ++i) { + try { + ba[i] + = (byte) Integer.parseInt( + hexString.substring(2 * i, 2 * i + 2), 16); + } catch (NumberFormatException nfe) { + throw new IllegalHexDumpException( + "Invalid characters " + + hexString.substring(2 * i, 2 * i + 2) + + " at position " + 2 * i + + " (of streamlined string)!"); + } + } + return ba; + } + + // ------------------------------------------------------ + public static String dumpString(int i) { + String retString = Integer.toString(i, 16); + if ((retString.length() & 0x01) != 0) { + retString = "0" + retString; + } + + return retString.toUpperCase(); + } + + public static String dumpString(byte b) { + return dumpString(b & 0xff); + } + + public static String dumpString(byte[] data) { + return dumpString(data, 0, data.length); + } + + public static String dumpString( + byte[] data, int offset, int length) { + return dumpString(data, "", offset, length); + } + + public static String dumpString( + byte[] data, String interBytesString, + int offset, int length) { + if (offset < 0 || length < 0) { + throw new IllegalArgumentException( + "Values for offset and parameters must be " + + "non-negative!"); + } + if (data == null) { + if (offset > 0 || length > 0) { + throw new IllegalArgumentException( + "Specified range not available in data!"); + } + return ""; + } + if ((offset + length) > data.length) { + throw new IllegalArgumentException( + "Specified range not available in data!"); + } + + StringBuffer sb = new StringBuffer(length + * (2 + interBytesString.length())); + for (int i = offset; i < offset + length; ++i) { + if (i != 0) { + sb.append(interBytesString); + } + sb.append(dumpString(data[i])); + } + return new String(sb); + } + + // ------------------------------------------------------ + public static String dump(BigInteger b) { + byte[] data = b.toByteArray(); + int l = data.length; + if (l > 1 && data[0] == 0) { + System.arraycopy(data, 1, data, 0, --l); + } + return dump(data, l); + } + + public static String dump(byte[] data) { + return dump(data, data.length); + } + + public static String dump(byte[] data, int length) { + StringBuffer sb + = new StringBuffer(78 * ((length + 15) >> 4)); + char[] txt = new char[16]; + + int i = 0; + for (; i < length;) { + if ((i & 0x0f) == 0) { + sb.append(alignRight( + Integer.toHexString(i) + " ", 9, '0')); + } + + int temp = (0xff & data[i]); + sb.append(dumpString(temp)); + sb.append(" "); + + if (temp < 0x20 || temp > 0x7e) /// ??? + { + txt[i++ & 0x0f] = '.'; + } // if( temp == 0x0a || temp == 0x09 || temp == 0x0d ) /// ??? + // txt[i++ & 0x0f] = (char)0; + else { + txt[i++ & 0x0f] = (char) temp; + } + + if ((i & 0x0f) == 0) { + sb.append(" "); + sb.append(txt, 0, 16); + sb.append(NL); + } + } + + if (i == 0) // empty data + { + sb.append("000000 "); + } + + if ((i = (i & 0x0f)) != 0) { + sb.append(fillString(3 * (16 - i), ' ') + " "); + sb.append(txt, 0, i); + sb.append(NL); + } + + return new String(sb.toString().trim()); + } + + /** + * @return If str.length() < totalLength, a String of + * length totalLength is returned. (The original String + * extended by specified character from the left.) + */ + public static String alignRight( + String str, int totalLength, char fillChar) { + if (totalLength <= str.length()) { + return str; + } + return fillString(totalLength - str.length(), fillChar) + + str; + } + + /** + * @return If str.length() < totalLength, a String of + * length totalLength is returned. (The original String + * extended by space characters from the left.) + */ + public static String alignRight( + String str, int totalLength) { + return alignRight(str, totalLength, ' '); + } + + /** + * @return A String of repLength many repetitions of + * specified character. + */ + public static String fillString( + int spaceLength, char fillChar) { + if (spaceLength < 0) { + spaceLength = 0; + } + StringBuffer sb = new StringBuffer(spaceLength); + for (int i = 0; i < spaceLength; ++i) { + sb.append(fillChar); + } + return new String(sb); + } + + // ------------------------------------------------------ + public static String dumpIntArray(int[] array) { + int nosPerLine = 4; + StringBuffer sb = new StringBuffer(); + Formatter formatter = new Formatter(sb); + for (int i = 0; i < array.length; ++i) { + if (i != 0 && i % nosPerLine == 0) { + sb.append(NL); + } + formatter.format(" %08x", array[i]); + } + sb.append(NL); + return new String(sb); + } +} + + diff --git a/src/util/IllegalHexDumpException.java b/src/util/IllegalHexDumpException.java new file mode 100644 index 0000000..4e26cdf --- /dev/null +++ b/src/util/IllegalHexDumpException.java @@ -0,0 +1,15 @@ +/* + * Stefan Heiss + * TH Ostwestfalen-Lippe + * FB Elektrotechnik und Technische Informatik + * Quellcode zur Lehrveranstaltung Datensicherheit + */ +package util; + +public class IllegalHexDumpException + extends IllegalArgumentException { + + public IllegalHexDumpException(String s) { + super(s); + } +}