public static void CreateDigitalSignature_FromString(string plainText, string RSAprivateKeyFile, string outputFile, HashingMode mode) { var hash = SHA.Hash(plainText, mode); var privateKey = FileManager.Read_RSAKey(RSAprivateKeyFile); var signature = RSA.Encrypt(hash, privateKey.Modulus, privateKey.Exponent); var signatureBytes = Convert.FromBase64String(signature); signature = HelperFunctions.FromByteToHex(signatureBytes); FileManager.Write_Signature(outputFile, signature, privateKey.Modulus.Length * 4, mode); }
public static string Hash(string text, HashingMode mode) { var textBytes = Encoding.UTF8.GetBytes(text); byte[] hashBytes; switch (mode) { case HashingMode.SHA_1: var SHA1 = new SHA1CryptoServiceProvider(); hashBytes = SHA1.ComputeHash(textBytes); break; case HashingMode.SHA_2_256: var SHA2_256 = new SHA256CryptoServiceProvider(); hashBytes = SHA2_256.ComputeHash(textBytes); break; case HashingMode.SHA_2_512: var SHA2_512 = new SHA512CryptoServiceProvider(); hashBytes = SHA2_512.ComputeHash(textBytes); break; //case HashingMode.SHA_3_256: // var SHA3_256 = new Sha3Digest(256); // SHA3_256.Update(Convert.ToByte(textBytes)); // SHA3_256.DoFinal(hashBytes); // break; //case HashingMode.SHA_3_256: // var SHA3_512 = new Sha3Digest(512); // break; default: var SHA_default = new SHA1CryptoServiceProvider(); hashBytes = SHA_default.ComputeHash(textBytes); break; } var hash = HelperFunctions.FromByteToHex(hashBytes); return(hash); }
public static void CreateDigitalEnvelope(string textFile, string RSApublicKey, string outputFile, EncryptionMode mode, SymetricAlgorithm algorithm, KeySize keySize) { var envelope = new HelperClasses.Envelope(); var text = FileManager.ReadFile_Byte(textFile); var vector = FileManager.ReadFile_IVector("IVector.txt"); byte[] cryptedText; byte[] key; if (algorithm == SymetricAlgorithm.THREE_DES) { key = HelperFunctions.GenerateKey((int)keySize); cryptedText = THREE_DES.Encrypt(text, key, mode); } else { key = HelperFunctions.GenerateKey((int)keySize); cryptedText = AES.Encrypt(text, key, vector, mode); } var keyHex = HelperFunctions.FromByteToHex(key); var publicKey = FileManager.Read_RSAKey(RSApublicKey); var RSAcrypted = RSA.Encrypt(keyHex, publicKey.Modulus, publicKey.Exponent); var cryptedKey = Convert.FromBase64String(RSAcrypted); RSAcrypted = HelperFunctions.FromByteToHex(cryptedKey); envelope.Data = Convert.ToBase64String(cryptedText); envelope.Key = RSAcrypted; FileManager.Write_Envelope(outputFile, envelope, key.Length * 8, publicKey.Modulus.Length * 4, algorithm); }