public static byte[] CreateSignature(byte[] data, CngKey key) { // 创建签名 byte[] signature; using(var signingAlg = new ECDsaCng(key)) { signature = signingAlg.SignData(data); signingAlg.Clear(); } return signature; }
static void Main(string[] args) { if (args.Length == 1) { // Got Signature (License)? Then Verify it byte[] signature = Convert.FromBase64String(args[0]); // Public key must be hard-coded! const string publicKeyString = "RUNTMSAAAACOUGMbADUgo3dDq42gwv+uCPsI8jjQm61r0CUPVioibLfzskpsGmUAJD29rt3FzS5qb28gS5Ed85jDPwQoesBJ"; byte[] publicKey = Convert.FromBase64String(publicKeyString); CngKey cngKey = CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob); ECDsaCng dsa = new ECDsaCng(cngKey); dsa.HashAlgorithm = CngAlgorithm.MD5; if (dsa.VerifyData(MachineID.ComputeEasyMachineID(), signature)) { Console.WriteLine("License is valid. Thank you!"); } else { Console.WriteLine("License is invalid!"); } dsa.Clear(); } else { // No Signature (License). Display InstallationID Console.WriteLine("No license found. Contact your software vendor and provide the following code:"); foreach (byte b in MachineID.ComputeEasyMachineID()) { Console.Write(b); } } Console.WriteLine("Press any key to continue ..."); Console.ReadKey(); }
public static bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey) { // 验证签名 bool result = false; using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob)) { using(var signingAlg = new ECDsaCng(key)) { result = signingAlg.VerifyData(data, signature); signingAlg.Clear(); } } return result; }
private bool VerifySignature(byte[] data, byte[] signature, byte[] pubKey) { bool retValue = false; using (CngKey key = CngKey.Import(pubKey, CngKeyBlobFormat.GenericPublicBlob)) using (var signingAlg = new ECDsaCng(key)) { #if NET46 retValue = signingAlg.VerifyData(data, signature); signingAlg.Clear(); #else retValue = signingAlg.VerifyData(data, signature, HashAlgorithmName.SHA512); #endif } return retValue; }
private byte[] CreateSignature(byte[] data, CngKey key) { byte[] signature; using (var signingAlg = new ECDsaCng(key)) { #if NET46 signature = signingAlg.SignData(data); signingAlg.Clear(); #else signature = signingAlg.SignData(data, HashAlgorithmName.SHA512); #endif } return signature; }
static void Main(string[] args) { if (args.Length != 1 && args[0].Length != 8) { Console.WriteLine("Use a valid MachineID as the argument."); return; } else { Console.WriteLine("MachineID: " + args[0]); } // The data to sign. byte[] data = new byte[8]; // Convert MachineID to byte array for (int i = 0; i < 8; i++) { data[i] = Byte.Parse(args[0][i].ToString()); } // Load the key from system store // This key was created by key gen CngKey cngKey = CngKey.Open("PA ECDSA Key"); using (ECDsaCng dsa = new ECDsaCng(cngKey)) { dsa.HashAlgorithm = CngAlgorithm.MD5; byte[] signature = dsa.SignData(data); dsa.Clear(); // Print signature Console.WriteLine("\nSignature:\n" + Debugger.BytesToString(signature)); // Base64 Console.WriteLine("\nSignature in Base64:\n" + CodeConverter.ToBase64(signature)); // Big numbers Console.WriteLine("\nSignature in Codes:"); ulong[] a = CodeConverter.ToInt64Array(signature); foreach (ulong i in a) Console.WriteLine(i.ToString()); // Formated Base32 Console.WriteLine("\nSignature in Formated Base32:\n" + CodeConverter.ToFormatedBase32Code(signature)); Console.WriteLine("\nPress any key to continue"); Console.ReadKey(); } }