using System;
namespace TestProject
{
/// <summary>
/// HIGHT ECB
/// </summary>
public class HIGHTECB
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// HIGHT 델타 베열
/// </summary>
private readonly byte[] hightDeltaArray =
{
0x5a, 0x6d, 0x36, 0x1b, 0x0d, 0x06, 0x03, 0x41,
0x60, 0x30, 0x18, 0x4c, 0x66, 0x33, 0x59, 0x2c,
0x56, 0x2b, 0x15, 0x4a, 0x65, 0x72, 0x39, 0x1c,
0x4e, 0x67, 0x73, 0x79, 0x3c, 0x5e, 0x6f, 0x37,
0x5b, 0x2d, 0x16, 0x0b, 0x05, 0x42, 0x21, 0x50,
0x28, 0x54, 0x2a, 0x55, 0x6a, 0x75, 0x7a, 0x7d,
0x3e, 0x5f, 0x2f, 0x17, 0x4b, 0x25, 0x52, 0x29,
0x14, 0x0a, 0x45, 0x62, 0x31, 0x58, 0x6c, 0x76,
0x3b, 0x1d, 0x0e, 0x47, 0x63, 0x71, 0x78, 0x7c,
0x7e, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x43, 0x61,
0x70, 0x38, 0x5c, 0x6e, 0x77, 0x7b, 0x3d, 0x1e,
0x4f, 0x27, 0x53, 0x69, 0x34, 0x1a, 0x4d, 0x26,
0x13, 0x49, 0x24, 0x12, 0x09, 0x04, 0x02, 0x01,
0x40, 0x20, 0x10, 0x08, 0x44, 0x22, 0x11, 0x48,
0x64, 0x32, 0x19, 0x0c, 0x46, 0x23, 0x51, 0x68,
0x74, 0x3a, 0x5d, 0x2e, 0x57, 0x6b, 0x35, 0x5a
};
/// <summary>
/// HIGHT F0 배열
/// </summary>
private readonly byte[] hightF0Array =
{
0x00, 0x86, 0x0d, 0x8b, 0x1a, 0x9c, 0x17, 0x91,
0x34, 0xb2, 0x39, 0xbf, 0x2e, 0xa8, 0x23, 0xa5,
0x68, 0xee, 0x65, 0xe3, 0x72, 0xf4, 0x7f, 0xf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0x8a, 0x0c, 0x87, 0x01, 0x90, 0x16, 0x9d, 0x1b,
0xbe, 0x38, 0xb3, 0x35, 0xa4, 0x22, 0xa9, 0x2f,
0x32, 0xb4, 0x3f, 0xb9, 0x28, 0xae, 0x25, 0xa3,
0x06, 0x80, 0x0b, 0x8d, 0x1c, 0x9a, 0x11, 0x97,
0x5a, 0xdc, 0x57, 0xd1, 0x40, 0xc6, 0x4d, 0xcb,
0x6e, 0xe8, 0x63, 0xe5, 0x74, 0xf2, 0x79, 0xff
};
/// <summary>
/// HIGHT F1 배열
/// </summary>
private readonly byte[] hightF1Array =
{
0x00, 0x58, 0xb0, 0xe8, 0x61, 0x39, 0xd1, 0x89,
0xc2, 0x9a, 0x72, 0x2a, 0xa3, 0xfb, 0x13, 0x4b,
0x85, 0xdd, 0x35, 0x6d, 0xe4, 0xbc, 0x54, 0x0c,
0x47, 0x1f, 0xf7, 0xaf, 0x26, 0x7e, 0x96, 0xce,
0x0b, 0x53, 0xbb, 0xe3, 0x6a, 0x32, 0xda, 0x82,
0xc9, 0x91, 0x79, 0x21, 0xa8, 0xf0, 0x18, 0x40,
0x8e, 0xd6, 0x3e, 0x66, 0xef, 0xb7, 0x5f, 0x07,
0x4c, 0x14, 0xfc, 0xa4, 0x2d, 0x75, 0x9d, 0xc5,
0x16, 0x4e, 0xa6, 0xfe, 0x77, 0x2f, 0xc7, 0x9f,
0xd4, 0x8c, 0x64, 0x3c, 0xb5, 0xed, 0x05, 0x5d,
0x93, 0xcb, 0x23, 0x7b, 0xf2, 0xaa, 0x42, 0x1a,
0x51, 0x09, 0xe1, 0xb9, 0x30, 0x68, 0x80, 0xd8,
0x1d, 0x45, 0xad, 0xf5, 0x7c, 0x24, 0xcc, 0x94,
0xdf, 0x87, 0x6f, 0x37, 0xbe, 0xe6, 0x0e, 0x56,
0x98, 0xc0, 0x28, 0x70, 0xf9, 0xa1, 0x49, 0x11,
0x5a, 0x02, 0xea, 0xb2, 0x3b, 0x63, 0x8b, 0xd3,
0x2c, 0x74, 0x9c, 0xc4, 0x4d, 0x15, 0xfd, 0xa5,
0xee, 0xb6, 0x5e, 0x06, 0x8f, 0xd7, 0x3f, 0x67,
0xa9, 0xf1, 0x19, 0x41, 0xc8, 0x90, 0x78, 0x20,
0x6b, 0x33, 0xdb, 0x83, 0x0a, 0x52, 0xba, 0xe2,
0x27, 0x7f, 0x97, 0xcf, 0x46, 0x1e, 0xf6, 0xae,
0xe5, 0xbd, 0x55, 0x0d, 0x84, 0xdc, 0x34, 0x6c,
0xa2, 0xfa, 0x12, 0x4a, 0xc3, 0x9b, 0x73, 0x2b,
0x60, 0x38, 0xd0, 0x88, 0x01, 0x59, 0xb1, 0xe9,
0x3a, 0x62, 0x8a, 0xd2, 0x5b, 0x03, 0xeb, 0xb3,
0xf8, 0xa0, 0x48, 0x10, 0x99, 0xc1, 0x29, 0x71,
0xbf, 0xe7, 0x0f, 0x57, 0xde, 0x86, 0x6e, 0x36,
0x7d, 0x25, 0xcd, 0x95, 0x1c, 0x44, 0xac, 0xf4,
0x31, 0x69, 0x81, 0xd9, 0x50, 0x08, 0xe0, 0xb8,
0xf3, 0xab, 0x43, 0x1b, 0x92, 0xca, 0x22, 0x7a,
0xb4, 0xec, 0x04, 0x5c, 0xd5, 0x8d, 0x65, 0x3d,
0x76, 0x2e, 0xc6, 0x9e, 0x17, 0x4f, 0xa7, 0xff
};
/// <summary>
/// 스케줄 키 배열
/// </summary>
private byte[] scheduleKeyArray = new byte[136];
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - HIGHTECB(userKeyArray)
/// <summary>
/// 생성자
/// </summary>
/// <param name="userKeyArray">사용자 키 배열</param>
public HIGHTECB(byte[] userKeyArray)
{
SetScheduleKey(userKeyArray);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 암호화하기 - Encrypt(sourceArray)
/// <summary>
/// 암호화하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <returns>암호화 배열</returns>
public byte[] Encrypt(byte[] sourceArray)
{
int length;
if(sourceArray.Length == 0)
{
return null;
}
if((sourceArray.Length % 8) != 0)
{
length = sourceArray.Length + (8 - (sourceArray.Length % 8));
}
else
{
length = sourceArray.Length;
}
byte[] targetArray = new byte[length];
byte[] sourceTemporaryArray = new byte[8];
byte[] targetTemporaryArray = new byte[8];
byte[] sourceArrayWithPadding = new byte[length];
Array.Copy(sourceArray, sourceArrayWithPadding, sourceArray.Length);
for(int i = 0; i < (int)(length / 8); i++)
{
Array.Copy(sourceArrayWithPadding, i * 8, sourceTemporaryArray, 0, 8);
EncryptBlock(sourceTemporaryArray, targetTemporaryArray);
Array.Copy(targetTemporaryArray, 0, targetArray, i * 8, 8);
}
return targetArray;
}
#endregion
#region 복호화하기 - Decrypt(sourceArray)
/// <summary>
/// 복호화하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <returns>복호화 배열</returns>
public byte[] Decrypt(byte[] sourceArray)
{
int length;
if(sourceArray.Length == 0)
{
return null;
}
if((sourceArray.Length % 8) != 0)
{
length = sourceArray.Length + (8 - (sourceArray.Length % 8));
}
else
{
length = sourceArray.Length;
}
byte[] targetArray = new byte[length];
byte[] sourceTemporaryArray = new byte[8];
byte[] targetTemporaryArray = new byte[8];
byte[] sourceArrayWithPadding = new byte[length];
Array.Copy(sourceArray, sourceArrayWithPadding, sourceArray.Length);
for(int i = 0; i < (int)(length / 8); i++)
{
Array.Copy(sourceArrayWithPadding, i * 8, sourceTemporaryArray, 0, 8);
DecryptBlock(sourceTemporaryArray, targetTemporaryArray);
Array.Copy(targetTemporaryArray, 0, targetArray, i * 8, 8);
}
return targetArray;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 스케줄 키 설정하기 - SetScheduleKey(userKeyArray)
/// <summary>
/// 스케줄 키 설정하기
/// </summary>
/// <param name="userKeyArray">사용자 키 배열</param>
private void SetScheduleKey(byte[] userKeyArray)
{
for(int i = 0; i < 4; i++)
{
this.scheduleKeyArray[i ] = userKeyArray[i + 12];
this.scheduleKeyArray[i + 4] = userKeyArray[i ];
}
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
this.scheduleKeyArray[8 + 16 * i + j] = (byte)((userKeyArray[(j - i) & 7] + this.hightDeltaArray[(16 * i + j)]) & 0xff);
}
for (int j = 0; j < 8; j++)
{
this.scheduleKeyArray[8 + 16 * i + j + 8] = (byte)((userKeyArray[((j - i) & 7) + 8] + this.hightDeltaArray[16 * i + j + 8]) & 0xff);
}
}
}
#endregion
#region 블럭 암호화하기 - EncryptBlock(sourceArray, targetArray)
/// <summary>
/// 블럭 암호화하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <param name="targetArray">타겟 배열</param>
private void EncryptBlock(byte[] sourceArray, byte[] targetArray)
{
byte[] temporaryArray = new byte[8];
temporaryArray[1] = sourceArray[1];
temporaryArray[3] = sourceArray[3];
temporaryArray[5] = sourceArray[5];
temporaryArray[7] = sourceArray[7];
temporaryArray[0] = (byte)((sourceArray[0] + this.scheduleKeyArray[0]) & 0xff);
temporaryArray[2] = (byte)((sourceArray[2] ^ this.scheduleKeyArray[1]) );
temporaryArray[4] = (byte)((sourceArray[4] + this.scheduleKeyArray[2]) & 0xff);
temporaryArray[6] = (byte)((sourceArray[6] ^ this.scheduleKeyArray[3]) );
void HIGHT_ENC(int k, int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
{
temporaryArray[i0] = (byte)((temporaryArray[i0] ^ (this.hightF0Array[temporaryArray[i1]] + this.scheduleKeyArray[4 * k + 3])) & 0xff);
temporaryArray[i2] = (byte)((temporaryArray[i2] + (this.hightF1Array[temporaryArray[i3]] ^ this.scheduleKeyArray[4 * k + 2])) & 0xff);
temporaryArray[i4] = (byte)((temporaryArray[i4] ^ (this.hightF0Array[temporaryArray[i5]] + this.scheduleKeyArray[4 * k + 1])) & 0xff);
temporary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, 5, 4, 3, 2, 1, 0, 7);
HIGHT_ENC(28, 5, 4, 3, 2, 1, 0, 7, 6);
HIGHT_ENC(29, 4, 3, 2, 1, 0, 7, 6, 5);
HIGHT_ENC(30, 3, 2, 1, 0, 7, 6, 5, 4);
HIGHT_ENC(31, 2, 1, 0, 7, 6, 5, 4, 3);
HIGHT_ENC(32, 1, 0, 7, 6, 5, 4, 3, 2);
HIGHT_ENC(33, 0, 7, 6, 5, 4, 3, 2, 1);
targetArray[1] = (byte)(temporaryArray[2] & 0xff);
targetArray[3] = (byte)(temporaryArray[4] & 0xff);
targetArray[5] = (byte)(temporaryArray[6] & 0xff);
targetArray[7] = (byte)(temporaryArray[0] & 0xff);
targetArray[0] = (byte)((temporaryArray[1] + this.scheduleKeyArray[4]) & 0xff);
targetArray[2] = (byte)((temporaryArray[3] ^ this.scheduleKeyArray[5]) & 0xff);
targetArray[4] = (byte)((temporaryArray[5] + this.scheduleKeyArray[6]) & 0xff);
targetArray[6] = (byte)((temporaryArray[7] ^ this.scheduleKeyArray[7]) & 0xff);
}
#endregion
#region 블럭 복호화하기 - DecryptBlock(sourceArray, targetArray)
/// <summary>
/// 블럭 복호화하기
/// </summary>
/// <param name="sourceArray">소스 배열</param>
/// <param name="targetArray">타겟 배열</param>
private void DecryptBlock(byte[] sourceArray, byte[] targetArray)
{
byte[] temporaryArray = new byte[8];
temporaryArray[2] = sourceArray[1];
temporaryArray[4] = sourceArray[3];
temporaryArray[6] = sourceArray[5];
temporaryArray[0] = sourceArray[7];
void HIGHT_DEC(int k, int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
{
temporaryArray[i1] = (byte)((temporaryArray[i1] - (this.hightF1Array[temporaryArray[i2]] ^ this.scheduleKeyArray[4 * k + 2])) & 0xff);
temporaryArray[i3] = (byte)((temporaryArray[i3] ^ (this.hightF0Array[temporaryArray[i4]] + this.scheduleKeyArray[4 * k + 1])) & 0xff);
temporaryArray[i5] = (byte)((temporaryArray[i5] - (this.hightF1Array[temporaryArray[i6]] ^ this.scheduleKeyArray[4 * k + 0])) & 0xff);
temporaryArray[i7] = (byte)((temporaryArray[i7] ^ (this.hightF0Array[temporaryArray[i0]] + this.scheduleKeyArray[4 * k + 3])) & 0xff);
}
temporaryArray[1] = (byte)(sourceArray[0] - this.scheduleKeyArray[4]);
temporaryArray[3] = (byte)(sourceArray[2] ^ this.scheduleKeyArray[5]);
temporaryArray[5] = (byte)(sourceArray[4] - this.scheduleKeyArray[6]);
temporaryArray[7] = (byte)(sourceArray[6] ^ this.scheduleKeyArray[7]);
HIGHT_DEC(33, 7, 6, 5, 4, 3, 2, 1, 0);
HIGHT_DEC(32, 0, 7, 6, 5, 4, 3, 2, 1);
HIGHT_DEC(31, 1, 0, 7, 6, 5, 4, 3, 2);
HIGHT_DEC(30, 2, 1, 0, 7, 6, 5, 4, 3);
HIGHT_DEC(29, 3, 2, 1, 0, 7, 6, 5, 4);
HIGHT_DEC(28, 4, 3, 2, 1, 0, 7, 6, 5);
HIGHT_DEC(27, 5, 4, 3, 2, 1, 0, 7, 6);
HIGHT_DEC(26, 6, 5, 4, 3, 2, 1, 0, 7);
HIGHT_DEC(25, 7, 6, 5, 4, 3, 2, 1, 0);
HIGHT_DEC(24, 0, 7, 6, 5, 4, 3, 2, 1);
HIGHT_DEC(23, 1, 0, 7, 6, 5, 4, 3, 2);
HIGHT_DEC(22, 2, 1, 0, 7, 6, 5, 4, 3);
HIGHT_DEC(21, 3, 2, 1, 0, 7, 6, 5, 4);
HIGHT_DEC(20, 4, 3, 2, 1, 0, 7, 6, 5);
HIGHT_DEC(19, 5, 4, 3, 2, 1, 0, 7, 6);
HIGHT_DEC(18, 6, 5, 4, 3, 2, 1, 0, 7);
HIGHT_DEC(17, 7, 6, 5, 4, 3, 2, 1, 0);
HIGHT_DEC(16, 0, 7, 6, 5, 4, 3, 2, 1);
HIGHT_DEC(15, 1, 0, 7, 6, 5, 4, 3, 2);
HIGHT_DEC(14, 2, 1, 0, 7, 6, 5, 4, 3);
HIGHT_DEC(13, 3, 2, 1, 0, 7, 6, 5, 4);
HIGHT_DEC(12, 4, 3, 2, 1, 0, 7, 6, 5);
HIGHT_DEC(11, 5, 4, 3, 2, 1, 0, 7, 6);
HIGHT_DEC(10, 6, 5, 4, 3, 2, 1, 0, 7);
HIGHT_DEC( 9, 7, 6, 5, 4, 3, 2, 1, 0);
HIGHT_DEC( 8, 0, 7, 6, 5, 4, 3, 2, 1);
HIGHT_DEC( 7, 1, 0, 7, 6, 5, 4, 3, 2);
HIGHT_DEC( 6, 2, 1, 0, 7, 6, 5, 4, 3);
HIGHT_DEC( 5, 3, 2, 1, 0, 7, 6, 5, 4);
HIGHT_DEC( 4, 4, 3, 2, 1, 0, 7, 6, 5);
HIGHT_DEC( 3, 5, 4, 3, 2, 1, 0, 7, 6);
HIGHT_DEC( 2, 6, 5, 4, 3, 2, 1, 0, 7);
targetArray[1] = (byte)(temporaryArray[1] & 0xff);
targetArray[3] = (byte)(temporaryArray[3] & 0xff);
targetArray[5] = (byte)(temporaryArray[5] & 0xff);
targetArray[7] = (byte)(temporaryArray[7] & 0xff);
targetArray[0] = (byte)((temporaryArray[0] - this.scheduleKeyArray[0]) & 0xff);
targetArray[2] = (byte)((temporaryArray[2] ^ this.scheduleKeyArray[1]) & 0xff);
targetArray[4] = (byte)((temporaryArray[4] - this.scheduleKeyArray[2]) & 0xff);
targetArray[6] = (byte)((temporaryArray[6] ^ this.scheduleKeyArray[3]) & 0xff);
}
#endregion
}
}