RSA signing in C# -
I need to create a software activation system. So I ended up with this plan: A unique computer based on the computer's hardware ID creates Buyer Email I sign this ID with my private key and I send back the signed string. The app verifies that the string (decodes it with the public key and compares it with the hardware ID).
So far I have been with the hardware ID and I have created keys (1024bit) with openssl, Pem and public.pem
I have worked with some codes provided by MSDN, but I do not understand how to import my key into RSA algorithms. Some code is as follows, any help would be appreciated. ...
try {// Create a Unicode encoder to convert between the byte array and the string. ASCIIEncoding ByteConverter = New ASCIIEncoding (); String Data String = CompID.Text; // Create byte arrays to keep original, encrypted and decrypted data. Byte [] Basic Data = Byte Converter. Gatebights (datestring); Byte [] signed data; // Create a new instance of the RSACryptoServiceProvider class // Automatically create a new key-pair. RSACryptoServiceProvider RSAalg = New RSACryptoServiceProvider (); // Export important information for an RSAParameters object // To export the private key to sign, you must pass the correct. // However, you do not need to export the private key // for verification. RSAParameters key = RSAalg.ExportParameters (true); // hash and sign the data. Signed data = hash and signbets (original data, key); ActCode.Text = encoding. UTF8.GetString (signed data, 0, signed data. Lang); Do // confirm the data and results can be found in the // console if (VerifySignedHash (original data, signed data, key)) was verified (MessageBox.Show ( "data.");} Else {MessageBox.Show ( "the data does not match the signature.");}} catch (Arglm tap Akspshn) {message box. show ( "was not signed or verified the data");} Methods:
public stable byte [] hash endbins (byte [] datatocyan, rsapmeter Key) {try {// a new instance create RSACryptoServiceProvider the // key RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider (RSAParameters using); RSAalg.ImportParameters (key); // hash and sign data. Using SHA1 for hashing Give the new instance of SHA1CryptoServiceProvider // to specify. return RSAalg.SignData (DataToSign, new SHA1CryptoServiceProvider ());} catch (cryptographic Aksposn e) {message box. show (e. message); return null;}} Public static Bool VerifySignedHash (Byte [] DataToVerify, Byte [] Signed Data, RSAPAM Tearce key) Create a new instance of Arssiarpiti service provider using // key {try {// Arsapiarmmitr. RSACryptoServiceProvider RSAalg = New RSACryptoServiceProvider (); RSAalg.ImportParameters (key); Do // verify data using signatures. Give a new example of SHA1CryptoServiceProvider // to specify the use of SHA1 for hashing. Return RSAalg.VerifyData (DataToVerify, new SHA1CryptoServiceProvider (), signed data); } Catch (Cryptographic Exposure E) {Message Box. Show (E. Message); return false; }}
Comments
Post a Comment