Reputation: 191
i have two forms the first produce hard disk serial number and encrypt it with public key, the second is like a key generator it receive the encrypted hard disk serial number, then it decrypt it with private key, and sign the plain text hard disk serial number with SHA512CryptoServiceProvider , and then in first form i verify the signed data with SHA512CryptoServiceProvider but always return false here is code of first form:
string PCSerialNumber = HardwareIdentifier.identifier("Win32_DiskDrive", "SerialNumber");
string Encrypted_PCSerialNumber = DoSecure.Encrypt_PCSerialNumber(PCSerialNumber);
//Encrypted_PCSerialNumber will send to second form
....
//we get ProductKey from user, it is a signed data with private key and SHA512CryptoServiceProvider
bool IsActivated = DoSecure.Verify_PCSerialNumber(ProductKey, PCSerialNumber);
here is class DoSecure :
public class DoSecure
{
public const string RSA_CurrentPublicKey_XML = "";
public const string RSA_CurrentPrivateKey_XML = "";
// Create byte array for additional entropy when using Protect method.
private static byte[] s_aditionalEntropy = { 9, 8, 7, 6, 5 };
//get public key as rsa obejct
private static RSACryptoServiceProvider GetPublicKey()
{
try
{
//define rsa object
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
//import public key from xml string
cipher.FromXmlString(RSA_CurrentPublicKey_XML);
return cipher;
}
catch (Exception ex)
{
FRM_MSG f = new FRM_MSG();
f.ShowDLG(" ",
ex.Message + "\n" + ex.StackTrace.ToString(),
FRM_MSG.MSGIcon.Error,
FRM_MSG.BTNS.One,
new string[] { "Ok" });
throw ex;
}
}
}
private static RSACryptoServiceProvider GetPrivateKey()
{
try
{
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
cipher.FromXmlString(DoSecure.RSA_CurrentPrivateKey_XML);
return cipher;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace.ToString(),
"",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
throw ex;
}
}
public static string Sign_PCSerialNumber_WithPrivateKey(string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = DoSecure.GetPrivateKey();
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
byte[] cipherText = cipher.SignData(data, new SHA512CryptoServiceProvider());
return Convert.ToBase64String(cipherText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace.ToString(),
"",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
throw ex;
}
}
public static string Decrypt_PCSerialNumber_WithPrivateKey(string Cipher_PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPrivateKey();
byte[] data = Convert.FromBase64String(Cipher_PCSerialNumber);
byte[] original = cipher.Decrypt(data, false);
return Encoding.Unicode.GetString(original);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace.ToString(),
"",
MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
throw ex;
}
}
public static string Encrypt_PCSerialNumber(string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPublicKey();
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
byte[] cipherText = cipher.Encrypt(data, false);
return Convert.ToBase64String(cipherText);
}
catch (Exception ex)
{
FRM_MSG f = new FRM_MSG();
f.ShowDLG(AssemblyInfo.AssemblyTitle,
ex.Message + "\n" + ex.StackTrace.ToString(),
FRM_MSG.MSGIcon.Error,
FRM_MSG.BTNS.One,
new string[] { "Ok" });
throw ex;
}
}
public static bool Verify_PCSerialNumber(string Cipher_PCSerialNumber, string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPublicKey();
byte[] SignedData;
try
{
SignedData = Convert.FromBase64String(Cipher_PCSerialNumber);
}
catch
{
SignedData = Encoding.Unicode.GetBytes(Cipher_PCSerialNumber);
}
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
return cipher.VerifyData(data, new SHA512CryptoServiceProvider(), SignedData);
}
catch (Exception ex)
{
FRM_MSG f = new FRM_MSG();
f.ShowDLG(AssemblyInfo.AssemblyTitle,
ex.Message + "\n" + ex.StackTrace.ToString(),
FRM_MSG.MSGIcon.Error,
FRM_MSG.BTNS.One,
new string[] { "Ok" });
return false;
}
}
}
here is second form :
string PCSerialNumber = DoSecure.Decrypt_PCSerialNumber_WithPrivateKey(TXT_EncryptedPCSerialNumber.Text);
TXT_DecryptedPCSerial.Text = PCSerialNumber;
TXT_ProductKey.Text = DoSecure. Sign_PCSerialNumber_WithPrivateKey(PCSerialNumber);
//the TXT_ProductKey is send to first form as ProductKey
but the :
if(DoSecure.Verify_PCSerialNumber(TXT_ActivationCode.Text, TXT_PCSerialNumber.Text))
always return false, what the wrong and how fix it ,i hope you help me. Thanks
Upvotes: 0
Views: 373
Reputation: 4340
I test your code and its working fine, maybe you have an issue when loading keys? or the text you get from UI elements (TXT_ActivationCode.Text, TXT_PCSerialNumber.Text) having an issue (not filled or having space at begining or end)?
Try this, it should give you True:
static void Main(string[] args)
{
var doSecure = new DoSecure();
string PCSerialNumber = Guid.NewGuid().ToString();
// Encryption
string Encrypted_PCSerialNumber = doSecure.Encrypt_PCSerialNumber(PCSerialNumber);
string clearSerialNumber = doSecure.Decrypt_PCSerialNumber_WithPrivateKey(Encrypted_PCSerialNumber);
// Sign
string productKey = doSecure.Sign_PCSerialNumber_WithPrivateKey(clearSerialNumber);
bool IsActivated = doSecure.Verify_PCSerialNumber(productKey, clearSerialNumber);
Console.WriteLine(IsActivated);
Console.ReadKey();
}
And this is your class:
public class DoSecure
{
public string RSA_CurrentPublicKey_XML = "";
public string RSA_CurrentPrivateKey_XML = "";
public DoSecure()
{
RSA_CurrentPublicKey_XML = File.ReadAllText(@"C:/Keys/public.xml");
RSA_CurrentPrivateKey_XML = File.ReadAllText(@"C:/Keys/private.xml");
}
private RSACryptoServiceProvider GetPublicKey()
{
try
{
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
cipher.FromXmlString(RSA_CurrentPublicKey_XML);
return cipher;
}
catch (Exception ex)
{
throw ex;
}
}
private RSACryptoServiceProvider GetPrivateKey()
{
try
{
RSACryptoServiceProvider cipher = new RSACryptoServiceProvider();
cipher.FromXmlString(RSA_CurrentPrivateKey_XML);
return cipher;
}
catch (Exception ex)
{
throw ex;
}
}
public string Sign_PCSerialNumber_WithPrivateKey(string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPrivateKey();
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
byte[] cipherText = cipher.SignData(data, new SHA512CryptoServiceProvider());
return Convert.ToBase64String(cipherText);
}
catch (Exception ex)
{
throw ex;
}
}
public string Decrypt_PCSerialNumber_WithPrivateKey(string Cipher_PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPrivateKey();
byte[] data = Convert.FromBase64String(Cipher_PCSerialNumber);
byte[] original = cipher.Decrypt(data, false);
return Encoding.Unicode.GetString(original);
}
catch (Exception ex)
{
throw ex;
}
}
public string Encrypt_PCSerialNumber(string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPublicKey();
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
byte[] cipherText = cipher.Encrypt(data, false);
return Convert.ToBase64String(cipherText);
}
catch (Exception ex)
{
throw ex;
}
}
public bool Verify_PCSerialNumber(string Cipher_PCSerialNumber, string PCSerialNumber)
{
try
{
RSACryptoServiceProvider cipher = GetPublicKey();
byte[] SignedData;
SignedData = Convert.FromBase64String(Cipher_PCSerialNumber);
byte[] data = Encoding.Unicode.GetBytes(PCSerialNumber);
return cipher.VerifyData(data, new SHA512CryptoServiceProvider(), SignedData);
}
catch (Exception ex)
{
return false;
}
}
}
Upvotes: 1