public static void Dekriptimi(string message, bool opa) { string[] words = message.Split('.'); var bytes = Convert.FromBase64String(words[0]); string user = Encoding.UTF8.GetString(bytes); // Console.WriteLine(user); byte[] bytes1 = Convert.FromBase64String(words[1]); //string IV = Encoding.UTF8.GetString(bytes1); // Console.WriteLine(IV); // bytes1 = Formovargun(IV); string path = "C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + user + ".pem"; //Console.WriteLine(path); if (!File.Exists(path)) { string s = "Celesi " + user + " nuk ekziston"; // Console.WriteLine("Celesi " + user + " nuk ekziston"); } else { RSACryptoServiceProvider rsaPriv = new RSACryptoServiceProvider(); Chilkat.PrivateKey privKey2 = new Chilkat.PrivateKey(); privKey2.LoadPemFile(path); string privKeyXml = privKey2.GetXml(); rsaPriv.FromXmlString(privKeyXml); byte[] dekodimi = Convert.FromBase64String(words[2]); byte[] decryptedRSA = rsaPriv.Decrypt(dekodimi, false); byte[] dekodimiDes = Convert.FromBase64String(words[3]); Console.WriteLine("Marresi: " + user); Console.WriteLine("Mesazhi: " + Encoding.UTF8.GetString(Desdekriptim(decryptedRSA, bytes1, dekodimiDes))); if (opa) { byte[] dataToverify = Convert.FromBase64String(words[5]); var Bytes12 = Convert.FromBase64String(words[4]); string perdoruesi = Encoding.UTF8.GetString(Bytes12); Console.WriteLine("Derguesi" + perdoruesi); if (Sign.VerifySignedHash(dataToverify, Desdekriptim(decryptedRSA, bytes1, dekodimiDes), perdoruesi)) { Console.WriteLine("Nenshkrami eshte valid"); } } } }
public static string GenjeroTokenin(string user) { Chilkat.PrivateKey privKey = new Chilkat.PrivateKey(); bool success = privKey.LoadEncryptedPemFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + user + ".pem", "passwd"); if (!success) { return("Nuk ekziston Celsi ose nuk mund te hapet"); } Chilkat.Jwt jwt = new Chilkat.Jwt(); Chilkat.JsonObject jose = new Chilkat.JsonObject(); // Use RS256. Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512. success = jose.AppendString("alg", "RS256"); success = jose.AppendString("typ", "JWT"); // Now build the JWT claims (also known as the payload) Chilkat.JsonObject claims = new Chilkat.JsonObject(); success = claims.AppendString("sub", user); DateTime aDay = DateTime.Now; TimeSpan a20min = new System.TimeSpan(0, 0, 20, 0); DateTime after20minutes = aDay.Add(a20min); string k = after20minutes.ToString(); success = claims.AppendString("Valid-till", k); //success = claims.AppendString("aud", "http://example.com"); // Set the timestamp of when the JWT was created to now. int curDateTime = jwt.GenNumericDate(0); success = claims.AddIntAt(-1, "iat", curDateTime); // Set the "not process before" timestamp to now. success = claims.AddIntAt(-1, "nbf", curDateTime); // Set the timestamp defining an expiration time (end time) for the token // to be now + 20 minutes (1200 seconds) success = claims.AddIntAt(-1, "exp", curDateTime + 1200); // adding the tme when its not valid // Produce the smallest possible JWT: jwt.AutoCompact = true; // Create the JWT token. This is where the RSA signature is created. string token = jwt.CreateJwtPk(jose.Emit(), claims.Emit(), privKey); //Console.WriteLine(token); return(token); }
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"); }
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(); } }