Security token Key Generagor in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace XSLT_Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string TokenKey = uniqueTokenKey();
            Response.Write("Unredable : " + TokenKey + "<br />");
            Response.Write("..Redable : " + ReaduniqueTokenKey(TokenKey) + "<br />");
        }


        private string uniqueTokenKey()
        {
            Random rnd = new Random();
            byte[] bytes = new byte[32];
            rnd.NextBytes(bytes);
            string myRndID = BitConverter.ToString(bytes);
            string[] myRndIDarr = myRndID.Split('-');

            string datekey = "";
            datekey = DateTime.Now.ToString("{.1}");
            string rekey = "";
            for (int i = datekey.Length; i > 0; i--)
            {
                rekey += datekey.Substring(i - 1, 1);
            }
            string[] Keyarr = rekey.Split('-');


            string finalkey = "";
            for (int i = 0; i < Keyarr.Length; i++)
            {
                finalkey += myRndIDarr[i] + "-" + myRndIDarr[i + 1] + "-" + Keyarr[i] + "" + "-";
            }

            return finalkey.Replace("-", "");
        }


        private string ReaduniqueTokenKey(string key)
        {
            string rekey = "";
            rekey = key.Substring(42, 3) + ":" + key.Substring(36, 2) + ":" + key.Substring(12, 2) + ":" + key.Substring(18, 2) + ":" + key.Substring(24, 2) + ":" + key.Substring(30, 2) + ":" + key.Substring(4, 4);
            string readablevalue = "";

            for (int i = rekey.Length; i > 0; i--)
            {
                readablevalue += rekey.Substring(i - 1, 1);
            }

            return readablevalue;
        }

    }
}






{.1}                         fff-ss-MM-dd-hh-mm-yyyy

0 comments:

Post a Comment