Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
friend i want expire the application after 60 days. so i want a function which accept the start date and end date (doesn't matter user machine current date) from the data base its calculate the remaining days and gives message to the user and application will be expire .

Thanks in advance. :)

What I have tried:

don't know what to do...

Thanks in advance. :)
Posted
Updated 14-Mar-18 3:40am

Use a DAT or similar file for this (or you could stuff in a DB).
When program starts check the DAT file or DB value.
- If it is blank populate with today's date
- If it isn't blank, you have a date to work with.

This is the simple process. A former company's software used a variant to expire the program on a set date.
 
Share this answer
 
Comments
ketan Ram Patil 15-Mar-18 5:21am    
do you have any sample code... :(
MadMyche 15-Mar-18 9:45am    
This is a rather simple solution; you should know how to open & read a data source (.CONFIG file, text file, DB etc) and know how to do simple Date functions.
Solution 1 will work, but the date would be visible to anyone, so it would be better to encrypt it. See example here: Encrypt and Decrypt Data with C#[Encrypt and Decrypt Data with C#]

Here is an example of using configuration files to store your data: Why, Where, and How of .NET Configuration Files[^]

Here is my example code, it's better / faster to use a form of encryption that is supported by hardware:
// Put these lines in your Form or Program:
string encryptedString = Encrypt.EncryptStringAes(DateTime.Now.ToString(), "My secret code");
Debug.Print(encryptedString);

string decryptedString = Encrypt.DecryptStringAes(encryptedString, "My secret code");
Debug.Print(decryptedString);

// Put this in a Class library file:
namespace Test
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// Encrypt or decrypt using AES Rijndael encryption.
    /// </summary>
    public class Encrypt
    {
        // Change this to your secret key code:
        private static byte[] salt   = Encoding.ASCII.GetBytes("0123456789");

        /// <summary>
        /// Encrypt the given string using AES.  
        /// The string can be decrypted using DecryptStringAES().
        /// The sharedSecret parameters must match.
        /// </summary>
        /// <param name="plainText">The text to encrypt.</param>
        /// <param name="sharedSecret">A password used to generate a key for encryption.</param>
        /// <returns>The encrypted string.</returns>
        public static string EncryptStringAes(string plainText, string sharedSecret)
        {
            if (string.IsNullOrEmpty(plainText))
            {
                throw new ArgumentNullException("plainText");
            }

            if (string.IsNullOrEmpty(sharedSecret))
            {
                throw new ArgumentNullException("sharedSecret");
            }

            string outStr;                              // Encrypted string to return
            RijndaelManaged aesAlg = null;              // RijndaelManaged object used to encrypt the data.

            try
            {
                // generate the key from the shared secret and the salt
                var key = new Rfc2898DeriveBytes(sharedSecret, salt);

                // Create a RijndaelManaged object
                aesAlg = new RijndaelManaged();
                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

                // Create a decryptor to perform the stream transform.
                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                using (var msEncrypt = new MemoryStream())
                {
                    // prepend the IV
                    msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
                    msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                    }

                    outStr = Convert.ToBase64String(msEncrypt.ToArray());
                }
            }
            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                {
                    aesAlg.Clear();
                }
            }

            // Return the encrypted bytes from the memory stream.
            return outStr;
        }

        /// <summary>
        /// Decrypt the given string, assumes the string was encrypted using EncryptStringAes() using an identical sharedSecret.
        /// </summary>
        /// <param name="cipherText">The text to decrypt.</param>
        /// <param name="sharedSecret">A password used to generate a key for decryption.</param>
        /// <returns>The decrypted string.</returns>
        public static string DecryptStringAes(string cipherText, string sharedSecret)
        {
            if (string.IsNullOrEmpty(cipherText))
            {
                throw new ArgumentNullException("cipherText");
            }

            if (string.IsNullOrEmpty(sharedSecret))
            {
                throw new ArgumentNullException("sharedSecret");
            }

            // Declare the RijndaelManaged object used to decrypt the data.
            RijndaelManaged aesAlg = null;

            // The decrypted text.
            string plaintext;

            try
            {
                // generate the key from the shared secret and the salt
                var key = new Rfc2898DeriveBytes(sharedSecret, salt);

                // Create the streams used for decryption.                
                byte[] bytes = Convert.FromBase64String(cipherText);
                using (var msDecrypt = new MemoryStream(bytes))
                {
                    // Create a RijndaelManaged object with the specified key and IV.
                    aesAlg = new RijndaelManaged();
                    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

                    // Get the initialization vector from the encrypted stream
                    aesAlg.IV = ReadByteArray(msDecrypt);

                    // Create a decryptor to perform the stream transform.
                    var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (var srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                {
                    aesAlg.Clear();
                }
            }

            return plaintext;
        }

        private static byte[] ReadByteArray(Stream s)
        {
            byte[] rawLength = new byte[sizeof(int)];

            if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
            {
                throw new SystemException("Stream did not contain properly formatted byte array");
            }

            byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];

            if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
            {
                throw new SystemException("Did not read byte array properly");
            }

            return buffer;
        }
    }
}
 
Share this answer
 
v3
Comments
RickZeeland 14-Mar-18 13:35pm    
To the person who downvoted my answer: I think it is very inpolite to downvote without leaving a comment !
ketan Ram Patil 15-Mar-18 5:29am    
do you have any sample code or link. so i can understand clearly
MadMyche 15-Mar-18 9:43am    
I do agree that the information would be visible; however, I feel that whomever would be implementing would have enough intelligence to know that.
The way we had implemented it was to multiply the yyyymmdd numeric by a certain factor and then encoded it.
RickZeeland 15-Mar-18 9:56am    
You would expect that, but I'm afraid that there are a lot of absolute beginners on CodeProject who really don't have a clue !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900