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(); } }
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); }