static void testdecrypt() { //byte[] encryptedArr = {145, 110, 51, 179, 147, 38, 228, 145, 55, 179, 143, 45, 179, 239, 28, 251, 127, 202, 47, 73, 49, 31, 36, 232, 81, 219, 2, 180, 16, 104, 203, 148, 207, 36, 110, 184, 225, 133, 190, 185, 22, 75, 49, 69, 129, 101, 161, 215, 102, 66, 218, 127, 193, 201, 222, 181, 187, 251, 221, 205, 103, 188, 5, 77, 94, 236, 43, 121, 182, 233, 109, 123, 64, 93, 61, 61, 204, 157, 23, 17, 220, 187, 150, 187, 29, 230, 91, 89, 241, 27, 34, 18, 21, 195, 220, 231, 237, 47, 123, 247, 128, 107, 169, 115, 84, 103, 129, 126, 99, 231, 2, 23, 152, 183, 136, 70, 64, 116, 125, 198, 240, 128, 129, 133, 5, 144, 179, 255, 10, 14, 148, 216, 164, 78, 253, 190, 231, 153, 157, 64, 212, 78, 212, 191, 230, 120, 58, 223, 147, 241, 222, 191, 22, 99, 80, 126, 212, 172, 14, 43, 135, 43, 117, 47, 172, 161, 38, 67, 125, 205, 186, 91, 35, 89, 110, 243, 184, 200, 158, 220, 161, 222, 172, 53, 211, 90, 55, 126, 190, 183, 71, 101, 215, 218, 90, 68, 122, 226, 237, 119, 139, 176, 51, 129, 7, 71, 154, 196, 52, 16, 136, 104, 4, 108, 136, 112, 25, 45, 88, 232, 94, 159, 199, 221, 152, 88, 156, 73, 183, 158, 241, 10, 102, 50, 166, 183, 86, 252, 102, 4, 190, 144, 149, 136, 255, 115, 163, 177, 88, 67, 88, 85, 247, 0, 30, 159}; string encryptedStr = ""; encryptedStr = "YrOiK4Efq2oM4Ny0YW7VFnOtkWHpcFg1zHHfxpdEX1k/wbjbMHyBYn6Uo1qB6fcRNmokO1L48fQGfpeyErgWSt/D1pBlffV+QkGNLtApXIShPdOj8uqElP0T8skNTZCUNqsaB5MPwFsUDiuynimqikM6tBNdLss+z81LbxARUlao/yN112GoxQQ4wpnaDk/eOA51J+5aZb2jMssHwAFx3M3K8AJmUv4qBN8lOAnahd3QnDgaxk7gNydjKyVi1eRGHYxwQVNwMsV1we7EFDMBICG97i4GmWrP2BkrXs8J09osP2O++TIVZrxYpEGwwrRmtno1bBCfySWLNuxd33jwcg=="; encryptedStr = "SnGp1BBb0fU7poFpM5Z0oD0YtfYZizU97GxUF9pn58yGEhpTdJ2vWy/NrhYMPflf4yym77zUyNVhDaOZ+1Q2H4imkiXa5q9DsBRN+dl5dVvosEse3OSyOTEbxn4AcrreWGhKTJ+/3mEhRotUE/rUuWskXKzSA+WQ0nSGUxSG/Rw0yaCeWsa1MU3UM0ugxxxd2gExY+K39+4nXUzNMv+H4XniTvmmUI7k8TEGAsZujwDsU9Oh+MbMtH8nlTu+yvfS++/dDTX9bFmpbkkH7FgbVvKcoICXsI5UfTATfRL4LkzdipO8VJNiPKT8TkgZyIJC/m1daLGAmIs+fM98rWm9yg=="; //DECRYPT Chilkat.PrivateKey privkey1 = new Chilkat.PrivateKey(); bool success = privkey1.LoadPem(mykey); Chilkat.Rsa rsa4 = new Chilkat.Rsa(); success = rsa4.UnlockComponent("HAFSJORSA_K36nxU3n1Yui"); rsa4.EncodingMode = "base64"; rsa4.Charset = "ANSI"; rsa4.LittleEndian = true; rsa4.OaepPadding = false; success = rsa4.ImportPrivateKey(privkey1.GetXml()); bool usePrivateKey = true; //byte[] decryptedArr = rsa4.DecryptBytes(encryptedArr, usePrivateKey); string decryptedStr = rsa4.DecryptStringENC(encryptedStr, usePrivateKey); }
/// <summary> /// /// </summary> /// <param name="StrToDecrypt"></param> /// <param name="EncodingMode"></param> /// <returns></returns> public override string Decrypt(string StrToDecrypt, string EncodingMode, string RSAPrivateKey) { try { // Now decrypt: Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa(); /// bool for success bool success; /// unlock component success = rsaDecryptor.UnlockComponent("VIENTORSA_TbpfVVr01Or6"); /// we encode in 64 base bits rsaDecryptor.EncodingMode = EncodingMode; ///rsa import private rsaDecryptor.ImportPrivateKey(RSAPrivateKey); /// decrypted string string decryptedStr; ///we now decript the string decryptedStr = rsaDecryptor.DecryptStringENC(StrToDecrypt, true); /// return the string return decryptedStr; } catch { return null; } finally { GC.Collect(); } }
private string DecryptWithApplicationCertPrivateKey(string encryptedkey) { var rsaDecryptor = new Chilkat.Rsa { EncodingMode = "hex" }; rsaDecryptor.ImportPrivateKey(CertificatePrivateKey); var applicationKey = rsaDecryptor.DecryptStringENC(encryptedkey, true); return(applicationKey); }
public string GetSymmetricKeyForUser(string userReference) { var userKeys = GetUserKeyPair(userReference); var rsaDecryptor = new Chilkat.Rsa { EncodingMode = EncodingMode }; rsaDecryptor.ImportPublicKey(userKeys.PublicKey); var symmetricKey = rsaDecryptor.DecryptStringENC(userKeys.EncryptedSymetricKey, false); return(symmetricKey); }
public void EncryptApplicationKeyFromCryptoServiceCert() { RegisterChilKat(); X509Certificate2 theCert = null; var store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadOnly); foreach (var certificate in store.Certificates) { //TODO's Console.WriteLine(certificate.FriendlyName); if (certificate.FriendlyName == "CryptoServiceCert") { theCert = certificate; } } var privateKey = theCert?.PrivateKey?.ToXmlString(true); var publicKey = theCert?.PrivateKey?.ToXmlString(true); var sampleText = "+oPDU29Bv2hfUZ8fZozMGAVYUZjp1wdwDs42JY213tA="; var rsaEncryptor = new Chilkat.Rsa { EncodingMode = "hex" }; rsaEncryptor.ImportPublicKey(publicKey); var encryptedText = rsaEncryptor.EncryptStringENC(sampleText, false); Console.WriteLine($"Encrypted Value: [{encryptedText}]"); var rsaDecryptor = new Chilkat.Rsa { EncodingMode = "hex" }; rsaDecryptor.ImportPrivateKey(privateKey); var decryptedText = rsaDecryptor.DecryptStringENC(encryptedText, true); }
/// <summary> /// Decrypt using a Given Private Key /// </summary> /// <param name="StringToDecrypt">w to Decrypt</param> /// <param name="PrivateKey"></param> /// <param name="EncodingMode">base64, hex</param> /// <returns></returns> public static string Decrypt(string StrToDecrypt, string PrivateKey, string EncodingMode) { try { /// First we get the RSA Public Key string RSAPrivateKey = PrivateKey; /// rsa object Chilkat.Rsa rsa = new Chilkat.Rsa(); /// bool for success bool success; /// unlock component success = rsa.UnlockComponent("VIENTORSA_TbpfVVr01Or6"); /// private key string privateKey; ///we now export the private key privateKey = rsa.ExportPrivateKey(); // Now decrypt: Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa(); /// we encode in 64 base bits rsaDecryptor.EncodingMode = EncodingMode; ///rsa import private rsaDecryptor.ImportPrivateKey(RSAPrivateKey); /// decrypted string string decryptedStr; ///we now decript the string decryptedStr = rsaDecryptor.DecryptStringENC(StrToDecrypt, true); /// return the string return decryptedStr; } catch { return null; } finally { GC.Collect(); } }
public VerificationResult <string> VerifySignature(DigitalSignature <string> signature) { var verificationResults = new VerificationResult <string>(); var hashManager = new HashManager(); var envelope = DecryptSignedContent <string>(signature.SignedContent, signature.SignatoryReference); if (envelope == null) { verificationResults.SignatoryMatchedToSignature = false; return(verificationResults); } var hashCurrentBody = hashManager.HashContent(envelope.Body); var publicKey = KeyStoreAdapter.GetPublicKeyForUser(signature.SignatoryReference); // Decrypt hash with public key to ensure there is no tampering and content is still the same var rsaDecryptor = new Chilkat.Rsa { EncodingMode = EncodingMode }; rsaDecryptor.ImportPublicKey(publicKey); var decryptedoriginalHash = rsaDecryptor.DecryptStringENC(envelope.Header.EncryptedBodyHashSignature, false); var signitureMatch = hashCurrentBody == decryptedoriginalHash; verificationResults.IpAddress = envelope.Body?.IpAddress; verificationResults.SignatoryEmailAddress = envelope.Body?.EmailAddress; verificationResults.SignatoryMatchedToSignature = true; verificationResults.SignedContentMatchesToSignature = signitureMatch; verificationResults.ExpectedContent = signature.OriginalContent; verificationResults.SignedContent = envelope?.Body?.Content; return(verificationResults); }
public static void performRSA(string text) { Chilkat.Rsa rsa = new Chilkat.Rsa(); bool success = rsa.UnlockComponent("Anything for 30-day trial"); if (success != true) { Console.WriteLine("RSA component unlock failed"); return; } // This example also generates the public and private // keys to be used in the RSA encryption. // Normally, you would generate a key pair once, // and distribute the public key to your partner. // Anything encrypted with the public key can be // decrypted with the private key. The reverse is // also true: anything encrypted using the private // key can be decrypted using the public key. // Generate a 1024-bit key. Chilkat RSA supports // key sizes ranging from 512 bits to 4096 bits. success = rsa.GenerateKey(1024); if (success != true) { Console.WriteLine(rsa.LastErrorText); return; } // Keys are exported in XML format: string publicKey = rsa.ExportPublicKey(); string privateKey = rsa.ExportPrivateKey(); string plainText = "Encrypting and decrypting should be easy!"; plainText = text; // Start with a new RSA object to demonstrate that all we // need are the keys previously exported: Chilkat.Rsa rsaEncryptor = new Chilkat.Rsa(); // Encrypted output is always binary. In this case, we want // to encode the encrypted bytes in a printable string. // Our choices are "hex", "base64", "url", "quoted-printable". rsaEncryptor.EncodingMode = "hex"; // We'll encrypt with the public key and decrypt with the private // key. It's also possible to do the reverse. success = rsaEncryptor.ImportPublicKey(publicKey); bool usePrivateKey = false; string encryptedStr = rsaEncryptor.EncryptStringENC(plainText, usePrivateKey); //Console.WriteLine(encryptedStr); // Now decrypt: Chilkat.Rsa rsaDecryptor = new Chilkat.Rsa(); rsaDecryptor.EncodingMode = "hex"; success = rsaDecryptor.ImportPrivateKey(privateKey); usePrivateKey = true; string decryptedStr = rsaDecryptor.DecryptStringENC(encryptedStr, usePrivateKey); //Console.WriteLine(decryptedStr); }