/// <summary> /// Create trial license file and preserve some values to web.config too. /// </summary> private static bool CreateLicenseFile() { bool licenseCreated; try { // Generate keys var keyGenerator = KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); var privateKey = keyPair.ToEncryptedPrivateKeyString(PassPhrase); var publicKey = keyPair.ToPublicKeyString(); // Save license file licenseCreated = GenerateLicense(privateKey); if (licenseCreated) { // Save public key to web.config SaveToConfig("PublicKey", publicKey); // Save First use date SaveToConfig("FUD", Encrypt(CurrentDate)); } } catch (Exception) { // Exception will raise when user had not granted permission to NETWORK SERVICE // for creating license file to App_Data licenseCreated = false; } return(licenseCreated); }
static void Main(string[] args) { var config = new MaskConfig(instanceBits: 5, tenantBits: 14); Console.WriteLine("Max long : {0}\t{1}", long.MaxValue, ToBinary(long.MaxValue)); Console.WriteLine("Max sequenceId : {0}\t\t{1}", config.SequenceMask, ToBinary(config.SequenceMask)); Console.WriteLine("Max int : {0}\t\t{1}", int.MaxValue, ToBinary(int.MaxValue)); Console.WriteLine(); const byte instanceId = 1; const ushort tenantId = 10; const long sequenceId = 100; var generator = new KeyGenerator(instanceId, config); Console.WriteLine(generator.Config); Console.WriteLine(generator.Config.GetSummary()); var oryginal = generator.Create(tenantId, sequenceId); CompositeKey reconstructed = generator.Reconstruct(oryginal); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("For given values : InstanceId:{0}\tTenantId:{1}\tSequenceId:{2}", instanceId, tenantId, sequenceId); Console.WriteLine("Created key will be : {0}", oryginal); Console.WriteLine("Reconstructed from key : InstanceId:{0}\tTenantId:{1}\tSequenceId:{2}", reconstructed.InstanceId, reconstructed.TenantId, reconstructed.SequenceId); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
[Fact] // See Bug #135 public void Ensure_To_Not_Generate_Identical_Keys() { var passPhrase = "test"; var privKeySet = new HashSet <string>(); var pubKeySet = new HashSet <string>(); // add well known key privKeySet.Add( "MIICITAjBgoqhkiG9w0BDAEDMBUEEF5Fx1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for (int i = 0; i < 100; i++) { var keyGenerator = KeyGenerator.Create(); //default key size var pair = keyGenerator.GenerateKeyPair(); var privateKey = pair.ToEncryptedPrivateKeyString(passPhrase); var publicKey = pair.ToPublicKeyString(); Assert.True(privKeySet.Add(privateKey)); Assert.True(pubKeySet.Add(publicKey)); } privKeySet.Clear(); pubKeySet.Clear(); }
static void Main(string[] args) { KeyGenerator keyGenerator = KeyGenerator.Create(); KeyPair keyPair = keyGenerator.GenerateKeyPair(); string privateKey = keyPair.ToEncryptedPrivateKeyString("123123"); string publicKey = keyPair.ToPublicKeyString(); var license = License.New() .As(LicenseType.Trial) .ExpiresAt(new DateTime(2014, 3, 31)) .LicensedTo("Mileage Software GmbH", string.Empty) .WithProductFeatures(f => f.Add("Clients", "Desktop:123123;iOS:123")) .WithUniqueIdentifier(Guid.NewGuid()) .CreateAndSignWithPrivateKey(privateKey, "123123"); var valid = license.Validate() .ExpirationDate().When(f => f.Type == LicenseType.Trial) .And().Signature(publicKey) .AssertValidLicense(); var clients = license.ProductFeatures.Get("Clients"); var parsedClients = clients.Split(';').Select(f => new { Client = f.Split(':')[0], Secret = f.Split(':')[1] }).ToList(); if (parsedClients.Any(f => f.Client == "Desktop" && f.Secret == "123123")) { } }
public LicenseSignatureTests() { passPhrase = Guid.NewGuid().ToString(); var keyGenerator = KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); privateKey = keyPair.ToEncryptedPrivateKeyString(passPhrase); publicKey = keyPair.ToPublicKeyString(); }
private static KeyPair GenerateKeyPair(string privateKeyPassPhrase) { var keyGenerator = KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); var result = new KeyPair { PublicKey = keyPair.ToPublicKeyString(), EncryptedPrivateKey = keyPair.ToEncryptedPrivateKeyString(privateKeyPassPhrase) }; return(result); }
public void GeneratePublicPrivateKeyPair() { Directory.CreateDirectory(LicenseConstants.KeysDirectory); var keyGenerator = KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); if (File.Exists(LicenseConstants.PrivateKeyDirectory) || File.Exists(LicenseConstants.PublicKeyDirectory)) { return; } File.WriteAllText(LicenseConstants.PrivateKeyDirectory, keyPair.ToEncryptedPrivateKeyString(LicenseConstants.PassPhrase), Encoding.UTF8); File.WriteAllText(LicenseConstants.PublicKeyDirectory, keyPair.ToPublicKeyString(), Encoding.UTF8); }
private void btnCreaLicenza_Click(object sender, EventArgs e) { Guid id; if (!Guid.TryParse(txtClientId.Text, out id)) { MessageBox.Show("Id Cliente {txtClientId.Text} non valido", this.Text); return; } var keyGenerator = KeyGenerator.Create(); var keyPair = keyGenerator.GenerateKeyPair(); txtPrivateKey.Text = keyPair.ToEncryptedPrivateKeyString(PASS_PHRASE + "." + txtClientId.Text); txtPublicKey.Text = keyPair.ToPublicKeyString(); }