Skip to content

Instantly share code, notes, and snippets.

@sabpprook
Created December 23, 2017 18:22
Show Gist options
  • Save sabpprook/5772f41c78fdcd5a847312ad6f5c3b68 to your computer and use it in GitHub Desktop.
Save sabpprook/5772f41c78fdcd5a847312ad6f5c3b68 to your computer and use it in GitHub Desktop.
Navicat Premium 12 KeyGen for Trad.Chinese only
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Navicat_Keygen
{
class Navicat
{
public static void PatchNavicatFile(string fileName)
{
if (File.Exists(fileName + ".bak"))
{
File.Delete(fileName);
File.Move(fileName + ".bak", fileName);
}
byte[] pattern = Encoding.UTF8.GetBytes("-----BEGIN");
byte[] buff = File.ReadAllBytes(fileName);
int index = FirstIndexOfPattern(buff, pattern);
GenerateRSAKeyPair();
byte[] pem = File.ReadAllBytes("public.pem");
File.Delete("public.pem");
Buffer.BlockCopy(pem, 0, buff, index, pem.Length);
File.Move(fileName, fileName + ".bak");
File.WriteAllBytes(fileName, buff);
}
static int FirstIndexOfPattern(byte[] source, byte[] pattern)
{
for (int i = 0; i < source.Length - pattern.Length; i++)
{
bool vaildate = true;
for (int j = 0; j < pattern.Length & vaildate; j++)
{
vaildate = source[i + j] == pattern[j];
}
if (vaildate)
{
return i;
}
}
return -1;
}
static void GenerateRSAKeyPair()
{
var gen = new RsaKeyPairGenerator();
gen.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
var keypair = gen.GenerateKeyPair();
using (var sw = new StreamWriter("private.pem"))
{
var pemWriter = new PemWriter(sw);
pemWriter.WriteObject(keypair.Private);
}
using (var sw = new StreamWriter("public.pem"))
{
var pemWriter = new PemWriter(sw);
pemWriter.WriteObject(keypair.Public);
}
}
public static string GenerateSNKey()
{
byte rnd = (byte)new Random((int)DateTime.Now.Ticks).Next();
byte[] s = new byte[10];
// NAV
s[0] = 0x68;
s[1] = 0x2A;
// Random
s[2] = s[3] = s[4] = rnd;
// tChinese
s[5] = 0xAA;
s[6] = 0x99;
// Commercial
s[7] = 0x65;
// Version 12
s[8] = 0xC0;
// Site License
s[9] = 0xFF;
byte[] buff = new byte[8];
Buffer.BlockCopy(s, 2, buff, 0, 8);
DES_Encrypt_ECB_None(ref buff);
Buffer.BlockCopy(buff, 0, s, 2, 8);
string T = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
byte[] SN = new byte[16];
SN[0] = (byte)T[s[0] >> 3];
SN[1] = (byte)T[(s[0] & 0x7) << 5 | s[1] >> 6];
SN[2] = (byte)T[(s[1] >> 1) & 0x1F];
SN[3] = (byte)T[(s[1] & 0x1) << 4 | s[2] >> 4];
SN[4] = (byte)T[(s[2] & 0xF) << 1 | s[3] >> 7];
SN[5] = (byte)T[s[3] >> 2 & 0x1F];
SN[6] = (byte)T[s[3] << 3 & 0x1F | s[4] >> 5];
SN[7] = (byte)T[s[4] & 0x1F];
SN[8] = (byte)T[s[5] >> 3];
SN[9] = (byte)T[(s[5] & 0x7) << 2 | s[6] >> 6];
SN[10] = (byte)T[s[6] >> 1 & 0x1F];
SN[11] = (byte)T[(s[6] & 0x1) << 4 | s[7] >> 4];
SN[12] = (byte)T[(s[7] & 0xF) << 1 | s[8] >> 7];
SN[13] = (byte)T[s[8] >> 2 & 0x1F];
SN[14] = (byte)T[s[8] << 3 & 0x1F | s[9] >> 5];
SN[15] = (byte)T[s[9] & 0x1F];
return Encoding.UTF8.GetString(SN).Insert(12, "-").Insert(8, "-").Insert(4, "-");
}
private static void DES_Encrypt_ECB_None(ref byte[] buff)
{
var des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.None;
des.Key = new byte[8] { 0x64, 0xAD, 0xF3, 0x2F, 0xAE, 0xF2, 0x1A, 0x27 };
using (var ms = new MemoryStream())
using (var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(buff, 0, buff.Length);
cs.FlushFinalBlock();
buff = ms.ToArray();
}
}
public static string GenerateActivateCode(string code, string name, string org)
{
string json = RSADecrypt2String(code, "private.pem");
string insert = $", \"N\":\"{name}\", \"O\":\"{org}\"";
json = json.Insert(json.IndexOf(", \"DI\""), insert);
json = json.Substring(0, json.IndexOf(", \"P\"")) + "}";
return RSAEncrypt2Base64(json, "private.pem");
}
private static string RSADecrypt2String(string base64, string keyfile)
{
var buff = Convert.FromBase64String(base64);
var engine = new Pkcs1Encoding(new RsaEngine());
using (var reader = new StringReader(File.ReadAllText(keyfile)))
{
var keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();
engine.Init(false, keyPair.Private);
}
return Encoding.UTF8.GetString(engine.ProcessBlock(buff, 0, buff.Length));
}
private static string RSAEncrypt2Base64(string text, string keyfile)
{
var buff = Encoding.UTF8.GetBytes(text);
var engine = new Pkcs1Encoding(new RsaEngine());
using (var reader = new StringReader(File.ReadAllText(keyfile)))
{
var keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();
engine.Init(true, keyPair.Private);
}
return Convert.ToBase64String(engine.ProcessBlock(buff, 0, buff.Length));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment