commit e06972e58c4dfd4acd96dbe49af720f4a877bc36
parent d7a0c46a15555dc8c3409e3c525c1e618aa0e4f8
Author: Minerva_juppiter <94231606+minerva-jupiter@users.noreply.github.com>
Date: Thu, 30 Jun 2022 23:21:39 +0900
Diffstat:
| A | Key.cs | | | 85 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | Program.cs | | | 37 | ++----------------------------------- |
| M | test.cs | | | 3 | +-- |
3 files changed, 88 insertions(+), 37 deletions(-)
diff --git a/Key.cs b/Key.cs
@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Cypher
+{
+ internal class Key
+ {
+ public void createKey()
+ {
+ double[] ints = new double[100];
+ for(int i = 0; i < 100; i++)
+ {
+ ints[i] = i;
+ }
+ double[] order = new double[100];
+ order = GeneratKey();
+
+ Console.WriteLine("Now genarating key...");
+ //相関係数が0.5より小さくなるまで鍵ファイルを生成し続ける。。
+ while (ComputeCoeff(order.ToArray(), ints.ToArray()) < 0.3)
+ {
+ order = GeneratKey();
+ }
+
+ int count = 1;
+ Question question = new Question();
+ string whereKeyFile = question.Questions("Where will you want to create the keyFile?", false);
+
+
+ //keyFileに書き出し
+ StreamWriter streamWriter = new StreamWriter(whereKeyFile);
+ Console.WriteLine("Created StreamWriter");
+ for (count = 0; count < order.Length; count++)
+ {
+ streamWriter.WriteLine(order[count]);
+ Console.WriteLine(count + order[count]);
+ }
+ streamWriter.Close();
+ }
+
+
+ public double[] GeneratKey()
+ {
+ //順序ファイルの生成
+ int random;
+ int count = 1;
+ double[] order = new double[100];
+ //百個数字が埋められるまで繰り返す。
+ while (count < 100)
+ {
+ var randomer = new Random();
+ random = randomer.Next(minValue: 0, maxValue: 100);
+
+ //今までにない数かどうかを評価
+ if (Array.IndexOf(order, random) < 0)
+ {
+ //生成した変数を"order"に代入
+ order[count] = random;
+ count++;
+ }
+ }
+ return order;
+ }
+ public double ComputeCoeff(double[] values1, double[] values2)
+ {
+ if (values1.Length != values2.Length)
+ throw new ArgumentException("values must be the same length");
+
+ var avg1 = values1.Average();
+ var avg2 = values2.Average();
+
+ var sum1 = values1.Zip(values2, (x1, y1) => (x1 - avg1) * (y1 - avg2)).Sum();
+
+ var sumSqr1 = values1.Sum(x => Math.Pow((x - avg1), 2.0));
+ var sumSqr2 = values2.Sum(y => Math.Pow((y - avg2), 2.0));
+
+ var result = sum1 / Math.Sqrt(sumSqr1 * sumSqr2);
+
+ return result;
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
@@ -23,7 +23,8 @@ static string Control()
if(answer == "createKey")
{
- createKey();
+ Key key = new Key();
+ key.createKey();
}
else
{
@@ -52,38 +53,4 @@ static string Control()
return answer;
}
-static void createKey()
-{
- Question question = new Question();
- string whereKeyFile = question.Questions("Where will you want to create the keyFile?",false);
- //順序ファイルの生成
- int random;
- int count = 1;
- int[] order = new int[100];
- //百個数字が埋められるまで繰り返す。
- while (count < 100)
- {
- var randomer = new Random();
- random = randomer.Next(minValue: 0, maxValue: 100);
-
- //今までにない数かどうかを評価
- if (Array.IndexOf(order, random) < 0)
- {
- //生成した変数を"order"に代入
- order[count] = random;
- count++;
- Console.WriteLine(random);
- }
- }
-
- //keyFileに書き出し
- StreamWriter streamWriter = new StreamWriter(whereKeyFile);
- Console.WriteLine("Created StreamWriter");
- for(count= 0; count < order.Length; count++)
- {
- streamWriter.WriteLine(order[count]);
- Console.WriteLine(count + order[count]);
- }
- streamWriter.Close();
-}
diff --git a/test.cs b/test.cs
@@ -12,8 +12,7 @@ namespace Cypher
public void test()
{
-
- }
+ }
public void Writedate(byte[] date)
{