Exemplo n.º 1
0
        public static bool TokenStatus(string token)
        {
            Chilkat.Jwt jwt = new Chilkat.Jwt();
            //Chilkat.Jwt jwt = new Chilkat.Jwt();
            //string payload = jwt.GetPayload(token);
            //Console.WriteLine(payload);


            Chilkat.PublicKey pubKey = new Chilkat.PublicKey();
            bool success             = pubKey.LoadFromFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" +
                                                           GetTuser(token, false) + ".pub.pem");

            Console.WriteLine(GetTuser(token, false));
            bool sigVerified = jwt.VerifyJwtPk(token, pubKey);

            if (sigVerified)
            {
                // Console.WriteLine("valid " + Convert.ToString(sigVerified));
                int  leeway     = 60;
                bool bTimeValid = jwt.IsTimeValid(token, leeway);
                Console.WriteLine("time constraints valid: " + Convert.ToString(bTimeValid));
                if (bTimeValid)
                {
                    // Console.WriteLine(GetTuser(token, true));
                }
                return(true);
            }
            else
            {
                // Console.WriteLine("nuk eshte valid");
                return(false);
            }
        }
Exemplo n.º 2
0
        public static void Createuser(string user)
        {
            Connectimimedb.InsertStudent(user);
            Chilkat.Global glob = new Chilkat.Global();
            Chilkat.Rsa    rsa  = new Chilkat.Rsa();
            glob.UnlockBundle("hELLOW");



            // Generate a 1024-bit key.  Chilkat RSA supports
            // key sizes ranging from 512 bits to 4096 bits.
            bool success = rsa.GenerateKey(1024);

            if (success != true)
            {
                Console.WriteLine(rsa.LastErrorText);
                return;
            }

            // Keys are exported in XML format:
            string publicKeyXml = rsa.ExportPublicKey();


            string privateKeyXml = rsa.ExportPrivateKey();


            // Save the private key in PEM format:
            Chilkat.PrivateKey privKey = new Chilkat.PrivateKey();
            success = privKey.LoadXml(privateKeyXml);
            success = privKey.SaveRsaPemFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + user + ".pem");
            Console.WriteLine("Eshte krijuar qelesi privat " + "keys\\" + user + ".pem");
            // Save the public key in PEM format:
            Chilkat.PublicKey pubKey = new Chilkat.PublicKey();
            success = pubKey.LoadXml(publicKeyXml);
            success = pubKey.SaveOpenSslPemFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + user + ".pub.pem");
            Console.WriteLine("Eshte krijuar qelesi public " + "keys\\" + user + ".pub.pem");
        }
Exemplo n.º 3
0
        public static void exportKey(string type, string name, string path)
        {
            string path1 = "C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + name + ".pem";
            string path2 = "C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + name + ".pub.pem";

            if ((type == "private"))
            {
                if (!File.Exists(path1))
                {
                    Console.WriteLine("celesi nuk ekziston");
                }
                else if (path == "")
                {
                    //XElement file = XElement.Load(@path1);
                    Chilkat.PrivateKey pkey = new Chilkat.PrivateKey();
                    pkey.LoadPem(path1);
                    string pkeyXml;
                    // Get the private key in XML format:
                    pkeyXml = pkey.GetXml();
                    Console.WriteLine(pkeyXml);
                }
                else
                {
                    XElement file = XElement.Load(@path1);
                    //Console.WriteLine(file);
                    file.Save("keys1\\" + name + ".xml");
                    file.Save(path);
                    Console.WriteLine("celsi u ruajt ne folderin key1");
                }
            }
            else if (type == "public")
            {
                if (!File.Exists(path2))
                {
                    Console.WriteLine("celesi nuk ekziston");
                }
                else if (path == "")
                {
                    XElement file = XElement.Load(@path1);
                    Console.WriteLine(file);
                    //XElement file = XElement.Load(@path1);
                    Chilkat.PublicKey pukey = new Chilkat.PublicKey();
                    pukey.LoadOpenSslPemFile(path1);
                    string pkeyXml;
                    // Get the private key in XML format:
                    pkeyXml = pukey.GetXml();
                    Console.WriteLine(pkeyXml);
                }
                else
                {
                    XElement file = XElement.Load(@path1);
                    //Console.WriteLine(file);
                    file.Save("key1\\" + name + ".pub.xml");
                    file.Save(path);
                    Console.WriteLine("celsi u ruajt ne folderin key1");



                    //XmlDocument FILE = new XmlDocument();
                    //FILE.LoadXml(file);
                    //FILE.Save("C:\\keys1\\" + name + ".pub.xml");
                    //
                }
            }
            else
            {
                Console.WriteLine("Operacioni qe keni kerkuar nuk mund te mundesohet");
                shfaq();
            }
        }
Exemplo n.º 4
0
        public static string Enkriptimi(string name, string message, string user)
        {
            string s = "C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + name + ".pub.pem";

            byte[] bytePlaintext =
                Encoding.UTF8.GetBytes(message);

            RSACryptoServiceProvider rsaPublic = new RSACryptoServiceProvider();

            Chilkat.PublicKey pubKey2 = new Chilkat.PublicKey();
            pubKey2.LoadOpenSslPemFile(s);
            string pubKeyXml = pubKey2.GetXml();


            rsaPublic.FromXmlString(pubKeyXml);

            DESCryptoServiceProvider FjalaDES =
                new DESCryptoServiceProvider();

            byte[] keyRandom = GenerateKey();
            byte[] keyIV     = GenerateIv();
            FjalaDES.Key     = keyRandom;
            FjalaDES.IV      = keyIV;
            FjalaDES.Mode    = CipherMode.CBC;
            FjalaDES.Padding = PaddingMode.PKCS7;


            // FjalaDES.Key = Encoding.UTF8.GetBytes("12345678");
            // FjalaDES.IV = Encoding.UTF8.GetBytes("12345678");
            MemoryStream ms = new MemoryStream();

            CryptoStream cs = new CryptoStream(ms,
                                               FjalaDES.CreateEncryptor(),
                                               CryptoStreamMode.Write);

            cs.Write(bytePlaintext, 0, bytePlaintext.Length);
            cs.FlushFinalBlock();

            cs.Close();

            byte[] byteCiphertexti = ms.ToArray();



            // mesazhi i koduar nga celsi des
            string ciphertexti = Convert.ToBase64String(byteCiphertexti);

            // celsi des i koduar permes celsit rsa
            byte[] encryptedRSA = rsaPublic.Encrypt(keyRandom, false);
            string ciphertexti1 = Convert.ToBase64String(encryptedRSA);

            //Kodimi i emrit te celsit
            byte[] emriVarg     = Formovargun(name);
            string ciphertexti2 = Convert.ToBase64String(emriVarg);
            //kodimi i iv
            string ciphertexti3 = Convert.ToBase64String(FjalaDES.IV);

            //pjesa e 3
            byte[] signedData   = Sign.HashAndSignBytes(byteCiphertexti, user);
            string ciphertexti5 = Convert.ToBase64String(signedData);
            string ciphertexti4 = Convert.ToBase64String(Formovargun(user));


            string total = ciphertexti2 + "." + ciphertexti3 + "." + ciphertexti1 +
                           "." + ciphertexti + ciphertexti4 + ciphertexti5;

            // Console.WriteLine(ciphertexti);
            return(total);
        }