Cleanups
This commit is contained in:
parent
df56eae9a0
commit
b97863a47e
@ -4,10 +4,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace CorrelationsAttacker
|
||||
{
|
||||
class Program
|
||||
{
|
||||
namespace CorrelationsAttacker {
|
||||
class Program {
|
||||
static int[] sbox = {
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
|
||||
0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
@ -41,10 +39,8 @@ namespace CorrelationsAttacker
|
||||
0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
|
||||
0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16};
|
||||
static void Main(string[] args)
|
||||
{
|
||||
if(args.Length != 1)
|
||||
{
|
||||
static void Main(string[] args) {
|
||||
if (args.Length != 1) {
|
||||
Console.WriteLine("programm.exe [in.txt]");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
@ -55,24 +51,21 @@ namespace CorrelationsAttacker
|
||||
int[][] eingabe = einlesen(args[0]); //Daten einklesen
|
||||
int[][] xored = xOrSBox(eingabe[0]); //S-Boxing inputs
|
||||
int[][] hamming = getHammingDistance(xored); //Calcs Hamming distance of S-Box Results (amount of Bits set 1)
|
||||
float[] koof = getCorrelation(hamming,eingabe[1]); //Starts the Calculation incl. output
|
||||
Console.WriteLine("Schlüsselkandidat: " + (int)koof[1] + "\nZugehöriger Korrelationskoeffizient: "+ koof[0]);
|
||||
float[] koof = getCorrelation(hamming, eingabe[1]); //Starts the Calculation incl. output
|
||||
Console.WriteLine("Schlüsselkandidat: " + (int)koof[1] + "\nZugehöriger Korrelationskoeffizient: " + koof[0]);
|
||||
Console.WriteLine("_____________________________________________________\n" +
|
||||
"Verbrauchte Zeit: "+(TimeSpan)(DateTime.Now-t)+"ms");
|
||||
"Verbrauchte Zeit: " + (TimeSpan)(DateTime.Now - t) + "ms");
|
||||
Console.ReadKey();
|
||||
}
|
||||
private static int[][] einlesen(String filename)
|
||||
{
|
||||
if(!File.Exists(filename))
|
||||
{
|
||||
Console.WriteLine("Datei "+filename+" nicht vorhanden");
|
||||
private static int[][] einlesen(String filename) {
|
||||
if (!File.Exists(filename)) {
|
||||
Console.WriteLine("Datei " + filename + " nicht vorhanden");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
StreamReader buffer = new StreamReader(filename, System.Text.Encoding.ASCII);
|
||||
int zeilen = 0;
|
||||
while(!buffer.EndOfStream)
|
||||
{
|
||||
if(buffer.ReadLine().Length == 0)
|
||||
while (!buffer.EndOfStream) {
|
||||
if (buffer.ReadLine().Length == 0)
|
||||
continue;
|
||||
zeilen++;
|
||||
}
|
||||
@ -81,66 +74,53 @@ namespace CorrelationsAttacker
|
||||
int[][] ret = new int[2][];
|
||||
ret[0] = new int[zeilen];
|
||||
ret[1] = new int[zeilen];
|
||||
for (int i = 0; i < zeilen; i++ )
|
||||
{
|
||||
for (int i = 0; i < zeilen; i++) {
|
||||
String dateiString = buffer.ReadLine();
|
||||
ret[0][i] = Convert.ToInt32(dateiString.Substring(0, 2), 16);
|
||||
ret[1][i] = Convert.ToInt32(dateiString.Substring(4, 2));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
private static int[][] xOrSBox(int[] inputX)
|
||||
{
|
||||
private static int[][] xOrSBox(int[] inputX) {
|
||||
int[][] xored = new int[inputX.Length][];
|
||||
for(int i=0;i<inputX.Length;i++)
|
||||
{
|
||||
for (int i = 0; i < inputX.Length; i++) {
|
||||
xored[i] = new int[256];
|
||||
for(int k=0;k<256;k++)
|
||||
{
|
||||
xored[i][k]=sbox[inputX[i]^k];
|
||||
for (int k = 0; k < 256; k++) {
|
||||
xored[i][k] = sbox[inputX[i] ^ k];
|
||||
}
|
||||
}
|
||||
return xored;
|
||||
}
|
||||
private static int[][] getHammingDistance(int[][] xored)
|
||||
{
|
||||
private static int[][] getHammingDistance(int[][] xored) {
|
||||
int[][] hamming = new int[xored.Length][];
|
||||
for(int i=0;i<xored.Length;i++)
|
||||
{
|
||||
for (int i = 0; i < xored.Length; i++) {
|
||||
hamming[i] = new int[xored[0].Length];
|
||||
for(int j=0;j<xored[0].Length;j++)
|
||||
{
|
||||
hamming[i][j]=countBits(xored[i][j]);
|
||||
for (int j = 0; j < xored[0].Length; j++) {
|
||||
hamming[i][j] = countBits(xored[i][j]);
|
||||
}
|
||||
}
|
||||
return hamming;
|
||||
}
|
||||
private static int countBits(int x)
|
||||
{
|
||||
private static int countBits(int x) {
|
||||
int count = 0;
|
||||
while (x != 0)
|
||||
{
|
||||
while (x != 0) {
|
||||
x &= x - 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
private static float[] getCorrelation(int[][] hamming, int[] eingabe)
|
||||
{
|
||||
private static float[] getCorrelation(int[][] hamming, int[] eingabe) {
|
||||
float[] ret = new float[2];
|
||||
for (int i = 0; i < hamming[0].Length; i++)
|
||||
{
|
||||
for (int i = 0; i < hamming[0].Length; i++) {
|
||||
int x = 0, y = 0;
|
||||
for (int j = 0; j < hamming.Length; j++)
|
||||
{
|
||||
for (int j = 0; j < hamming.Length; j++) {
|
||||
x += eingabe[j];
|
||||
y += hamming[j][i];
|
||||
}
|
||||
float mittel_x = x / hamming.Length;
|
||||
float mittel_y = y / hamming.Length;
|
||||
float summe1 = 0, summe2 = 0, summe3 = 0;
|
||||
for (int j = 0; j < hamming.Length; j++)
|
||||
{
|
||||
for (int j = 0; j < hamming.Length; j++) {
|
||||
summe1 += (eingabe[j] - mittel_x) * (hamming[j][i] - mittel_y);
|
||||
summe2 += (eingabe[j] - mittel_x) * (eingabe[j] - mittel_x);
|
||||
summe3 += (hamming[j][i] - mittel_y) * (hamming[j][i] - mittel_y);
|
||||
@ -148,8 +128,7 @@ namespace CorrelationsAttacker
|
||||
summe2 = (float)Math.Sqrt(summe2);
|
||||
summe3 = (float)Math.Sqrt(summe3);
|
||||
float c = Math.Abs(summe1 / (summe2 * summe3));
|
||||
if (c > ret[0])
|
||||
{
|
||||
if (c > ret[0]) {
|
||||
ret[0] = c;
|
||||
ret[1] = i;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user