commit 2f8c04134fcab0f2e3f47b5d2a27cae474e2fc86 Author: Paul Date: Sun Oct 18 21:53:47 2020 +0200 inital commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /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/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..97626ba --- /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..935a154 --- /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..a331603 --- /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..1f08b88 --- /dev/null +++ b/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/ModularArithmaticCalculator.iml b/ModularArithmaticCalculator.iml new file mode 100644 index 0000000..d5c0743 --- /dev/null +++ b/ModularArithmaticCalculator.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/out/production/ModularArithmaticCalculator/tech/loedige/Main.class b/out/production/ModularArithmaticCalculator/tech/loedige/Main.class new file mode 100644 index 0000000..9c80beb Binary files /dev/null and b/out/production/ModularArithmaticCalculator/tech/loedige/Main.class differ diff --git a/out/production/ModularArithmaticCalculator/util/Dump.class b/out/production/ModularArithmaticCalculator/util/Dump.class new file mode 100644 index 0000000..515d45c Binary files /dev/null and b/out/production/ModularArithmaticCalculator/util/Dump.class differ diff --git a/out/production/ModularArithmaticCalculator/util/IllegalHexDumpException.class b/out/production/ModularArithmaticCalculator/util/IllegalHexDumpException.class new file mode 100644 index 0000000..f6d1218 Binary files /dev/null and b/out/production/ModularArithmaticCalculator/util/IllegalHexDumpException.class differ diff --git a/src/tech/loedige/Main.java b/src/tech/loedige/Main.java new file mode 100644 index 0000000..06bafa2 --- /dev/null +++ b/src/tech/loedige/Main.java @@ -0,0 +1,146 @@ +package tech.loedige; + +import util.Dump; + +import java.math.BigInteger; +import java.util.List; + +public class Main { + + public static void main(String[] args) { + // write your code here + int Z = 13; + /*getOrderAndInverse(Z); + + getPrimitiveElement(2); + getPrimitiveElement(3); + getPrimitiveElement(5); + getPrimitiveElement(7); + getPrimitiveElement(11); + getPrimitiveElement(13); + getPrimitiveElement(17); + getPrimitiveElement(19); + getPrimitiveElement(23);*/ + + + /*getGroupAndMaxOrder(4); + getGroupAndMaxOrder(6); + getGroupAndMaxOrder(8); + getGroupAndMaxOrder(9); + getGroupAndMaxOrder(10); + getGroupAndMaxOrder(12); + getGroupAndMaxOrder(14); + getGroupAndMaxOrder(15); + getGroupAndMaxOrder(16);*/ + + /*getOrderRFC7919(2); + getOrderRFC7919(3); + getOrderRFC7919(4); + getOrderRFC7919(5); + getOrderRFC7919(6);*/ + getOrderRFC7919(7); + /*getOrderRFC7919(8); + getOrderRFC7919(9);*/ + + getSharedSecret(); + } + + private static void getSharedSecret(){ + BigInteger kA_pub = new BigInteger("27282667746838411939143241666723046694527264637007559423952796008903977634324023737753124358327044498235695067321378479835998035654034644517179759788901352737738466214712414616949468433629993070414307680308271213886171008791853033810348102610871852298273695732098461918442112517731219977692798111421155148916640251520848785936004364583451306844563558619725135844767334446174867008412854680440175554752338253195095533723949367607823291954964390988919344648377988215096163955782620638461956467357105200224013185118543681273714672276506212170327933806962609375007526608015272079508124813944474288170004694821130997859203"); + BigInteger kB_priv = new BigInteger("7532919400457445663303700830973523590423263864724159934023391416574147324621933536449637606282131103918883626115855343351325277070984876733129265301205702642333354707816589145340968726389894044688024166108036474255111533732948913177796353881817402947686860467714661832933242570411627309386297780537824324832627012380856381669126724451049618946281819268817314740486708353781450544470119941814532875098325894703744849701616223792195949010930775348252418316672068234315909190760678573146228014902673906821408531294133290046052716296787531205413964871695349574817105255572494804346109933903949904551005148642787422867446"); + BigInteger n = new BigInteger("32317006071311007300153513477825163362488057133489075174588434139269806834136210002792056362640164685458556357935330816928829023080573472625273554742461245741026202527916572972862706300325263428213145766931414223654220941111348629991657478268034230553086349050635557712219187890332729569696129743856241741236237225197346402691855797767976823014625397933058015226858730761197532436467475855460715043896844940366130497697812854295958659597567051283852132784468522925504568272879113720098931873959143374175837826000278034973198552060607533234122603254684088120031105907484281003994966956119696956248629032338072839127039"); + BigInteger g = BigInteger.TWO; + + BigInteger kA_B = kA_pub.modPow(kB_priv,n); + System.out.println("k_A,B= "+kA_B.toString()); + } + + private static void getOrderRFC7919(int i){ + BigInteger p = new BigInteger("323170060713110073001535134778251633624880571334890751745884341392698068341" + + "362100027920563626401646854585563579353308169288290230805734726252735547424612457410262025279165729728" + + "627063003252634282131457669314142236542209411113486299916574782680342305530863490506355577122191878903" + + "327295696961297438562417412362372251973464026918557977679768230146253979330580152268587307611975324364" + + "674758554607150438968449403661304976978128542959586595975670512838521327844685229255045682728791137200" + + "989318739591433741758378260002780349731985520606075332341226032546840881200311059074842810039949669561" + + "19696956248629032338072839127039"); + BigInteger bi = BigInteger.valueOf(i); + BigInteger o =p.divide(bi); + o= o.add(BigInteger.ONE); + BigInteger r = bi.multiply(o); + if(r.mod(p).compareTo(BigInteger.ONE)==0){ + System.out.println(i + ":\t" +o.subtract(BigInteger.ONE).toString()); + } + else{ + int c = 2; + while(true){ + bi = BigInteger.valueOf(i); + o =p.divide(bi); + o =o.multiply(BigInteger.valueOf(c)); + o= o.add(BigInteger.ONE); + System.out.println(c); + if(r.mod(p).compareTo(BigInteger.ONE)==0){ + System.out.println(i + ":\t" +o.subtract(BigInteger.ONE).toString()); + return; + } + c++; + } + } + + } + + private static void getGroupAndMaxOrder(int Z){ + int max = 0; + int groupOrder=0; + for(int i=1; i> 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); + } +}