Client/Desktop/KMBIM3.0/KMBIM3.0_23.08.16_수정최종/License/AuthKey.cs

344 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Policy;
using System.Management;
using System.Management.Instrumentation;
namespace KMBIM.Revit.Tools
{
public class AuthKey
{
static decimal AUTH_CODE = 122;
public decimal Cvt36To10(string s36)
{
var arrChar = s36.ToCharArray();
string hex = s36;
char ch;
decimal dec;
dec = 0;
int len = arrChar.Count();
for (int i = 1; i <= len; i++)
{
ch = arrChar[i - 1];
if (ch >= 97 && ch <= 122) ch = (char)(Convert.ToInt64(ch) - 36);
else if (ch > 122)
{
// 영문 대문자로 변환
Int64 i64 = (Convert.ToInt64(ch) / 2);
while (i64 > 25)
{
i64 = i64 / 2;
}
i64 = i64 + 65;
ch = (char)i64;
}
else if ((ch<48 && ch > 57) || ch > 90)
{
// 숫자로 변환
Int64 i64 = (Convert.ToInt64(ch) / 2);
while (i64 > 9)
{
i64 = i64 / 2;
}
i64 = i64 + 48;
ch = (char)i64;
}
dec = dec + array36(ch.ToString()) * (decimal)Math.Pow(36.0, (double)(len - i));
}//end-for
// s = string.Format("{0:f1}", dec);
return dec;
}
public int array36(string val)
{
int retval = 0;
string hexval = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 36; i++) if (hexval.Substring(i, 1) == val) { retval = i; break; }
return retval;
}
public string Cvt10To36(string s10, ref string s36)
{
string hexval = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string temp = s10;
double dec, mok;
int r, i;
string hex = string.Empty;
i = 0;
double.TryParse(temp, out dec);
do
{
mok = Math.Floor(dec / 36.0);
r = (int)(dec - mok * 36.0);
hex += hexval[r];
dec = mok;
i++;
} while (mok > 35);
hex += hexval[(int)mok];
//s36 = hex.ToCharArray().Reverse().ToArray().ToString();
//s36 = hex.ToCharArray().Reverse().ToString();
var charArray = hex.ToCharArray();
Array.Reverse(charArray);
var reversed = new string(charArray);
s36 = reversed.ToString();
return s36;
}
public string Cvt10To32(string s10, ref string s32)
{
string hexval = "0123456789ABCDEFGHIJKLMNOPQRSTUVW";
string temp = s10;
double dec, mok;
int r, i;
string hex = string.Empty;
i = 0;
double.TryParse(temp, out dec);
do
{
mok = Math.Floor(dec / 32.0);
r = (int)(dec - mok * 32.0);
hex += hexval[r];
dec = mok;
i++;
} while (mok > 31);
hex += hexval[(int)mok];
//s36 = hex.ToCharArray().Reverse().ToArray().ToString();
//s36 = hex.ToCharArray().Reverse().ToString();
var charArray = hex.ToCharArray();
Array.Reverse(charArray);
var reversed = new string(charArray);
s32 = reversed.ToString();
return s32;
}
public bool GetSerialNumber(ref string strSerialNumber)
{
string userName;
bool bRC = false;
string vvalue;
strSerialNumber = string.Empty;
vvalue = string.Empty;
userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string str1 = string.Empty;
string str2 = string.Empty;
string strVolume = GetVolume();
if (userName != null)
{
if (string.IsNullOrEmpty(strVolume) == false) userName += strVolume;
if (userName.Length > 1)
{
userName = userName.Trim();
userName = userName.Replace(" ", "");
userName = userName.Replace("-", "");
userName = userName.Replace("\\", "");
}
}
else return false; ;
string strTmpSn = string.Empty;
int j = 0;
string strSn2 = string.Empty;
string str32 = string.Empty;
string str10 = string.Empty;
// 일련번호를 8자리씩 끊어서 권한코드 생성
for (int i = 0; i < userName.Length; i++)
{
if (j < 4)
{
strSn2 += userName[i];
}
else
{
str10 = string.Format("{0}", (int)this.Cvt36To10(strSn2));
strSerialNumber += this.Cvt10To32(str10, ref str32);
j = 0;
strSn2 = string.Empty;
continue;
}
j += 1;
}
if (strSn2.Length > 0)
{
str10 = string.Format("{0}", (int)this.Cvt36To10(strSn2));
strSerialNumber += this.Cvt10To32(str10, ref str32);
}
bRC = true;
return bRC;
}
// adapter info가 성공적으로 얻어지면 True를 넘겨준다.
// 클래스를 이용하기 전에 이 함수를 호출하여야 한다.
public bool IsValid()
{
return NetworkInterface.GetAllNetworkInterfaces().Count() < 1 ? false : true;
}
private int FindChar(string str, char ch)
{
int nInx = -1;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ch)
{
nInx = i;
break;
}
}
return nInx;
}
/// <summary>
///
/// </summary>
/// <param name="authcodemeta">인증받은 권한코드(사용가능개월수포함)</param>
/// <param name="authtypecode">인증유형</param>
/// <param name="authcode">실제 사용되는 권한코드</param>
/// <param name="monthcode">사용일수 또는 개월수(36진수)</param>
public void ParseAuthCode(string authcodemeta, out char authtypecode, out string authcode, out string monthcode)
{
authtypecode = 'X';
string AC = string.Empty;
int nInx = FindChar(authcodemeta, 'Y');
authcode = string.Empty;
if (nInx != -1)
{
authtypecode = 'Y';
AC = authcodemeta.Substring(nInx + 1, authcodemeta.Length - (nInx + 1));
authcode = authcodemeta.Substring(0, nInx);
}
else
{
nInx = FindChar(authcodemeta, 'Z');
if (nInx != -1)
{
authtypecode = 'Z';
AC = authcodemeta.Substring(nInx + 1, authcodemeta.Length - (nInx + 1));
authcode = authcodemeta.Substring(0, nInx);
}
}
monthcode = AC;
}
public void GetAuthCode2(string sn, int nAuthType, out string strAuthCode)
{
string strSn1;
string strAuthTmp = string.Empty;
strAuthCode = string.Empty;
strSn1 = string.Format("{0}{1}", sn, nAuthType);
int j = 0;
string strSn2 = string.Empty;
sn = strSn1.Clone().ToString();
// 일련번호를 8자리씩 끊어서 권한코드 생성
for (int i = 0; i < sn.Length; i++)
{
if (j < 8)
{
strSn2 += sn[i];
}
else
{
strAuthTmp = this.GetAuthCode(strSn2);
strAuthCode += strAuthTmp;
j = 0;
strSn2 = string.Empty;
continue;
}
j += 1;
}
if (strSn2.Length > 0)
{
strAuthTmp = this.GetAuthCode(strSn2);
strAuthCode += strAuthTmp;
}
}
public string GetVolume()
{
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + "C:\"");
//bind our management object
disk.Get();
//return the serial number
return disk["VolumeSerialNumber"].ToString();
}
// 권한코드
public string GetAuthCode(string serial_number)
{
string strAuthCode = string.Empty;
string s10_1 = string.Empty, s10_2 = string.Empty;
string str36 = string.Empty;
int j = 0;
string strSn2 = string.Empty;
decimal decVal = 0;
// 권한코드를 4자리씩 끊어서 권한코드 생성
for (int i = 0; i < serial_number.Length; i++)
{
if (j < 4)
{
strSn2 += serial_number[i];
}
else
{
decVal = Cvt36To10(strSn2) * AUTH_CODE;
s10_1 = decVal.ToString();
strAuthCode += Cvt10To32(s10_1, ref str36);
j = 0;
strSn2 = string.Empty;
continue;
}
j += 1;
}
if (strSn2.Length > 0)
{
decVal = Cvt36To10(strSn2) * AUTH_CODE;
s10_1 = decVal.ToString();
strAuthCode += Cvt10To32(s10_1, ref str36);
}
return strAuthCode;
}
}
}