static void Main(string[] args) { #region .: Instanciando Objectos :. Encrypt encode = new Encrypt(); Decrypt decode = new Decrypt(); EncryptedTextModel text = new EncryptedTextModel(); #endregion Console.WriteLine("#############################################################"); Console.WriteLine("################### Programa de Segurança ###################"); Console.WriteLine("#############################################################"); Console.WriteLine(""); Console.ReadKey(); Console.WriteLine("########## Texto Inicial ##########\n"); Console.WriteLine($"{text.textToEncrypt}"); Console.ReadKey(); #region .: Codificando e Descodificando decode.Descodificar(encode.Codificar(text.textToEncrypt, text.secretToEncrypt), text.secretToEncrypt); #endregion Console.ReadLine(); }
public static string DecodedWith(this EncryptedTextModel model, X509Certificate2 certificate) { if (certificate is null) { return("ERROR: No key provided to decode EncryptedText"); } if (!certificate.HasPrivateKey) { return("ERROR: Certificate has no private key to be able to decode EncryptedText"); } string certKeyId = certificate.ToKeyId(); string pubKeyHash = certificate.ToPubKeyHash(); if (pubKeyHash is null) { return("ERROR: Non-RSA certificate is not currently supported"); } if (model.ReadingKeys.SkipNulls().None()) { return("ERROR: No reading keys able to decode EncryptedText"); } var authorizedKey = model.ReadingKeys.FirstOrDefault(rk => rk.PublicKeyHash == pubKeyHash && rk.ReaderId == certKeyId); if (authorizedKey is null) { return("ERROR: Your key does not match one of the authorized reading keys"); } string cipher = model.Cipher.WithDefault("AES256").ToUpperInvariant(); if (cipher != "AES256") { return($"ERROR: Cipher {cipher} is not currently supported"); } if (model.CipherText.None()) { return(null); } using var rsaAlgo = certificate.GetRSAPrivateKey(); var aesKey = RSADecrypt(rsaAlgo, authorizedKey.EncryptedKey); var aesIV = RSADecrypt(rsaAlgo, authorizedKey.EncryptedIV); var jsonBytes = AES256Decrypt(model.CipherText, aesKey, aesIV); if (jsonBytes[0] != 17) { return("ERROR: Something went wrong while decrypting the content. Unexpected initial bytes"); } var skipTagAndSize = jsonBytes[1..].ILIntDecode().ILIntSize() + 1;