inital commit

This commit is contained in:
Paul 2020-10-18 21:47:24 +02:00
commit dd69cc22e5
13 changed files with 396 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -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/

1
.idea/description.html generated Normal file
View File

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

6
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

9
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="14" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/HashFunctions.iml" filepath="$PROJECT_DIR$/HashFunctions.iml" />
</modules>
</component>
</project>

3
.idea/project-template.xml generated Normal file
View File

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

12
HashFunctions.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

119
src/tech/loedige/Main.java Normal file
View File

@ -0,0 +1,119 @@
package tech.loedige;
import util.Dump;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws NoSuchAlgorithmException, InterruptedException {
//SHA1_Aufgabe();
//SHA256_Aufgabe();
//iterationCountAufgabe();
birthdayParadox(0.5d);
birthdayParadox(0.9d);
birthdayParadox(0.99d);
}
private static void birthdayParadox(double propability){
double counter=0;
double current=1;
while(1-current<propability){
counter++;
current*=1-(counter/365);
}
System.out.println((counter+1) + ": " + (1-current));
}
private static void iterationCountAufgabe() throws NoSuchAlgorithmException {
int c = 0;
byte[] alice = Dump.hexString2byteArray("31323334353622445CBCBDFD10E00EA6");
boolean finished = false;
var md = MessageDigest.getInstance("SHA-256");
while(!finished){
c++;
alice = md.digest(alice);
finished = testCurrent(alice,Dump.hexString2byteArray("DADCC5"),Dump.hexString2byteArray("786532"));
System.out.println(c);
}
}
private static boolean testCurrent(byte[] current, byte[] g_beginning, byte[] g_ending){
for(int i =0; i<g_beginning.length; i++){
if(current[i]!=g_beginning[i])
return false;
}
for(int i = 0; i<g_ending.length; i++){
if(current[current.length - 1 -i]!=g_ending[g_ending.length - 1 - i])
return false;
}
return true;
}
private static void SHA256_Aufgabe() throws NoSuchAlgorithmException, InterruptedException {
var md = MessageDigest.getInstance("SHA-256");
System.out.println(Dump.dumpString(md.digest(("123456").getBytes())));
//md = MessageDigest.getInstance("SHA-1");
System.out.println(Dump.dumpString(md.digest(("rj5gt7hog4m9gxtinokx").getBytes())));
System.out.println(Dump.dumpString(md.digest(("admin").getBytes())));
byte[] input = Dump.hexString2byteArray("ADDB0F5E7826C857D7376D1BD9BC33C0C544790A2EAC96144A8AF22B1298C940");
System.out.println(bruteForcePassword(input));
}
private static String bruteForcePassword(byte[] input) throws NoSuchAlgorithmException, InterruptedException {
System.out.println("Warning! This might take a while");
var md = MessageDigest.getInstance("SHA-256");
for(int i1= 0 ; i1<26;i1++){
for(int i2= 0 ; i2<26;i2++) {
System.out.println((((i1*26+i2)*100)/676) + "% of all possibilities tried");
for (int i3 = 0; i3 < 26; i3++) {
for (int i4 = 0; i4 < 26; i4++) {
for (int i5 = 0; i5 < 26; i5++) {
for (int i6 = 0; i6 < 26; i6++) {
byte[] pos = {'a','a','a','a','a','a'};
pos[0] += i1;
pos[1] += i2;
pos[2] += i3;
pos[3] += i4;
pos[4] += i5;
pos[5] += i6;
byte[] output = md.digest(pos);
if (Arrays.equals(output,input)) {
return new String(pos);
}
}
}
}
}
}
}
return "nothing found";
}
private static void SHA1_Aufgabe() throws NoSuchAlgorithmException {
byte[] data1 = util.Dump.hexString2byteArray(""
+ "D131DD02C5E6EEC4693D9A0698AFF95C2FCAB58712467EAB"
+ "4004583EB8FB7F8955AD340609F4B30283E488832571415A"
+ "085125E8F7CDC99FD91DBDF280373C5BD8823E3156348F5B"
+ "AE6DACD436C919C6DD53E2B487DA03FD02396306D248CDA0"
+ "E99F33420F577EE8CE54B67080A80D1EC69821BCB6A88393"
+ "96F9652B6FF72A70");
byte[] data2 = util.Dump.hexString2byteArray(""
+ "D131DD02C5E6EEC4693D9A0698AFF95C2FCAB50712467EAB"
+ "4004583EB8FB7F8955AD340609F4B30283E4888325F1415A"
+ "085125E8F7CDC99FD91DBD7280373C5BD8823E3156348F5B"
+ "AE6DACD436C919C6DD53E23487DA03FD02396306D248CDA0"
+ "E99F33420F577EE8CE54B67080280D1EC69821BCB6A88393"
+ "96F965AB6FF72A70");
var messageDigest = MessageDigest.getInstance("MD5");
System.out.println(Dump.dumpString(messageDigest.digest(data1)));
System.out.println(Dump.dumpString(messageDigest.digest(data2)));
for(int i = 0 ; i < data1.length; i++){
System.out.print((data1[i]^data2[i]) + " ");
}
}
}

215
src/util/Dump.java Normal file
View File

@ -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);
}
}

View File

@ -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);
}
}