Clean HIGHT.cs

This commit is contained in:
Namhyeon Go 2024-12-06 00:51:07 +09:00
parent a95e7e3e0d
commit 92b3907a8e

View File

@ -11,6 +11,7 @@
* - https://github.com/gnh1201/welsonjs * - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss * - https://catswords.social/@catswords_oss
* - https://teams.live.com/l/community/FEACHncAhq8ldnojAI * - https://teams.live.com/l/community/FEACHncAhq8ldnojAI
* - https://discord.gg/XKG5CjtXEj
* *
* author: * author:
* - Namhyeon Go <abuse@catswords.net> * - Namhyeon Go <abuse@catswords.net>
@ -23,6 +24,8 @@
* - https://blog.naver.com/chandong83/222198351602 * - https://blog.naver.com/chandong83/222198351602
* - https://seed.kisa.or.kr/kisa/algorithm/EgovHightInfo.do * - https://seed.kisa.or.kr/kisa/algorithm/EgovHightInfo.do
* - https://www.iso.org/standard/54531.html * - https://www.iso.org/standard/54531.html
* - https://ics.catswords.net/HIGHT-algorithm-specification-english.pdf
* - https://ics.catswords.net/HIGHT-algorithm-specification-korean.pdf
* *
* license: * license:
* GPLv3 or MS-RL(Microsoft Reciprocal License) * GPLv3 or MS-RL(Microsoft Reciprocal License)
@ -33,7 +36,7 @@ using System;
namespace WelsonJS.Cryptography namespace WelsonJS.Cryptography
{ {
public static class HIGHT public class HIGHT
{ {
private static readonly byte[] hightDelta = { private static readonly byte[] hightDelta = {
0x5A,0x6D,0x36,0x1B,0x0D,0x06,0x03,0x41, 0x5A,0x6D,0x36,0x1B,0x0D,0x06,0x03,0x41,
@ -236,24 +239,6 @@ namespace WelsonJS.Cryptography
dataOut[6] = (byte)((xx[6] ^ scheduleKey[3]) & 0xFF); dataOut[6] = (byte)((xx[6] ^ scheduleKey[3]) & 0xFF);
} }
void debugPrintHex(byte[] dat)
{
for (int i = 0; i < dat.Length; i++)
{
Console.Write(string.Format("0x{0:x2}, ", dat[i]));
if (((i + 1) % 16) == 0)
{
Console.WriteLine("");
}
}
Console.WriteLine("");
}
void debugPrintLine(string str)
{
Console.WriteLine(str);
}
void EncryptBlock(byte[] dataIn, byte[] dataOut) void EncryptBlock(byte[] dataIn, byte[] dataOut)
{ {
byte[] xx = new byte[8]; byte[] xx = new byte[8];
@ -318,6 +303,7 @@ namespace WelsonJS.Cryptography
dataOut[4] = (byte)((xx[5] + scheduleKey[6]) & 0xFF); dataOut[4] = (byte)((xx[5] + scheduleKey[6]) & 0xFF);
dataOut[6] = (byte)((xx[7] ^ scheduleKey[7]) & 0xFF); dataOut[6] = (byte)((xx[7] ^ scheduleKey[7]) & 0xFF);
} }
public byte[] Encrypt(byte[] dataIn) public byte[] Encrypt(byte[] dataIn)
{ {
int length; int length;
@ -347,6 +333,7 @@ namespace WelsonJS.Cryptography
} }
return dataOut; return dataOut;
} }
public byte[] Decrypt(byte[] dataIn) public byte[] Decrypt(byte[] dataIn)
{ {
int length; int length;
@ -376,7 +363,7 @@ namespace WelsonJS.Cryptography
return dataOut; return dataOut;
} }
public void test() public void Test()
{ {
byte[] dataIn = new byte[15]; byte[] dataIn = new byte[15];
byte[] dataOut = new byte[15]; byte[] dataOut = new byte[15];
@ -385,17 +372,17 @@ namespace WelsonJS.Cryptography
{ {
dataIn[i] = (byte)i; dataIn[i] = (byte)i;
} }
debugPrintLine("schedule key data"); Console.WriteLine("schedule key data");
debugPrintHex(scheduleKey); Console.WriteLine(string.Format("0x{0:x2}", scheduleKey));
dataOut = Encrypt(dataIn); dataOut = Encrypt(dataIn);
dataOut2 = Decrypt(dataOut); dataOut2 = Decrypt(dataOut);
debugPrintLine("origin data"); Console.WriteLine("origin data");
debugPrintHex(dataIn); Console.WriteLine(string.Format("0x{0:x2}", dataIn));
debugPrintLine("encryption data"); Console.WriteLine("encryption data");
debugPrintHex(dataOut); Console.WriteLine(string.Format("0x{0:x2}", dataOut));
debugPrintLine("decryption data"); Console.WriteLine("decryption data");
debugPrintHex(dataOut2); Console.WriteLine(string.Format("0x{0:x2}", dataOut2));
} }
} }
} }