Ejemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            string ivHex = textBox2.Text;

            // The secret key must equal the size of the key.  For
            // 256-bit encryption, the binary secret key is 32 bytes.
            // For 128-bit encryption, the binary secret key is 16 bytes.
            string keyHex = textBox1.Text;

            // Encrypt a string...
            // The input string is 44 ANSI characters (i.e. 44 bytes), so
            // the output should be 48 bytes (a multiple of 16).
            // Because the output is a hex string, it should
            // be 96 characters long (2 chars per byte).

            Chilkat.Crypt2 decrypt = new Chilkat.Crypt2();

            decrypt.CryptAlgorithm = "aes";
            decrypt.CipherMode     = "ctr";
            decrypt.KeyLength      = 256;
            decrypt.EncodingMode   = "base64";
            decrypt.SetEncodedIV(ivHex, "ascii");
            decrypt.SetEncodedKey(keyHex, "ascii");

            // Now decrypt:
            string decStr = decrypt.DecryptStringENC(richTextBox1.Text);

            richTextBox2.Text = decStr;
        }
        public static string EncryptPassword(string _password)
        {
            Chilkat.Crypt2 _encrypt = new Chilkat.Crypt2();
            bool isUnlocked = _encrypt.UnlockComponent("SCOTTGCrypt_FdXsYk2WWPjN");
            string password;
            password = "******";

            _encrypt.CryptAlgorithm = "aes";
            _encrypt.CipherMode = "cbc";
            _encrypt.KeyLength = 128;

            //  Generate a binary secret key from a password string
            //  of any length.  For 128-bit encryption, GenEncodedSecretKey
            //  generates the MD5 hash of the password and returns it
            //  in the encoded form requested.  The 2nd param can be
            //  "hex", "base64", "url", "quoted-printable", etc.
            string hexKey;
            hexKey = _encrypt.GenEncodedSecretKey(password, "hex");
            _encrypt.SetEncodedKey(hexKey, "hex");

            _encrypt.EncodingMode = "base64";

            //  Encrypt a string and return the binary encrypted data
            //  in a base-64 encoded string.
            string s1 = _encrypt.DecryptStringENC(_password);
            return _encrypt.EncryptStringENC(_password);
        }
Ejemplo n.º 3
0
        public static void performChaChaEncryption(string text)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
            //  Set the encryption algorithm to chacha20
            //  chacha20 is a stream cipher, and therefore no cipher mode applies.

            //  Set the encryption algorithm to chacha20
            //  chacha20 is a stream cipher, and therefore no cipher mode applies.
            crypt.CryptAlgorithm = "chacha20";

            //  The key length for chacha20 is always 256-bits.
            crypt.KeyLength = 256;

            //  Note: "padding" only applies to block encryption algorithmns.
            //  Since chacha20 is a stream cipher, there is no padding and the output
            //  number of bytes is exactly equal to the input.

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  Valid modes are (case insensitive) "Base64", "modBase64", "Base32", "Base58", "UU",
            //  "QP" (for quoted-printable), "URL" (for url-encoding), "Hex",
            //  "Q", "B", "url_oauth", "url_rfc1738", "url_rfc2396", and "url_rfc3986".
            crypt.EncodingMode = "hex";

            //  The inputs to ChaCha20 encryption, specified by RFC 7539, are:
            //  1) A 256-bit secret key.
            //  2) A 96-bit nonce.
            //  3) A 32-bit initial count.
            //  The IV property is used to specify the chacha20 nonce.
            //  For a 96-bit nonce, the IV should be 12 bytes in length.
            //
            //  Note: Some implementations of chacha20, such as that used internally by SSH,
            //  use a 64-bit nonce and 64-bit count.  To do chacha20 encryption in this way,
            //  simply provide 8 bytes for the IV instead of 12 bytes.  Chilkat will then automatically
            //  use 8 bytes (64-bits) for the count.

            //  This example duplicates Test Vector #3 (for ChaCha20 encryption) from RFC 7539.
            string ivHex = "000000000000000000000002";

            crypt.SetEncodedIV(ivHex, "hex");

            crypt.InitialCount = 42;

            string keyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0";

            crypt.SetEncodedKey(keyHex, "hex");

            // string plainText = "'Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.";


            string encStr = crypt.EncryptStringENC(text);
            //Console.WriteLine(encStr);
            //Console.WriteLine("I am wor");

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);
            //Console.WriteLine(decStr);
        }
Ejemplo n.º 4
0
        public static void perform3DES(string text)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

            bool success = crypt.UnlockComponent("Anything for 30-day trial");

            if (success != true)
            {
                Console.WriteLine(crypt.LastErrorText);
                return;
            }

            //  Specify 3DES for the encryption algorithm:
            crypt.CryptAlgorithm = "3des";

            //  CipherMode may be "ecb" or "cbc"
            crypt.CipherMode = "cbc";

            //  KeyLength must be 192.  3DES is technically 168-bits;
            //  the most-significant bit of each key byte is a parity bit,
            //  so we must indicate a KeyLength of 192, which includes
            //  the parity bits.
            crypt.KeyLength = 192;

            //  The padding scheme determines the contents of the bytes
            //  that are added to pad the result to a multiple of the
            //  encryption algorithm's block size.  3DES has a block
            //  size of 8 bytes, so encrypted output is always
            //  a multiple of 8.
            crypt.PaddingScheme = 0;

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  It may be "hex", "url", "base64", or "quoted-printable".
            crypt.EncodingMode = "hex";

            //  An initialization vector is required if using CBC or CFB modes.
            //  ECB mode does not use an IV.
            //  The length of the IV is equal to the algorithm's block size.
            //  It is NOT equal to the length of the key.
            string ivHex = "0001020304050607";

            crypt.SetEncodedIV(ivHex, "hex");

            //  The secret key must equal the size of the key.  For
            //  3DES, the key must be 24 bytes (i.e. 192-bits).
            string keyHex = "000102030405060708090A0B0C0D0E0F0001020304050607";

            crypt.SetEncodedKey(keyHex, "hex");


            string encStr = crypt.EncryptStringENC(text);
            //Console.WriteLine(encStr);

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);
            // Console.WriteLine(decStr);
        }
Ejemplo n.º 5
0
        public static void performBlowfish2(string text)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

            //  Attention: use "blowfish2" for the algorithm name:
            crypt.CryptAlgorithm = "blowfish2";

            //  CipherMode may be "ecb", "cbc", or "cfb"
            crypt.CipherMode = "cbc";

            //  KeyLength (in bits) may be a number between 32 and 448.
            //  128-bits is usually sufficient.  The KeyLength must be a
            //  multiple of 8.
            crypt.KeyLength = 128;

            //  The padding scheme determines the contents of the bytes
            //  that are added to pad the result to a multiple of the
            //  encryption algorithm's block size.  Blowfish has a block
            //  size of 8 bytes, so encrypted output is always
            //  a multiple of 8.
            crypt.PaddingScheme = 0;

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  It may be "hex", "url", "base64", or "quoted-printable".
            crypt.EncodingMode = "hex";

            //  An initialization vector is required if using CBC or CFB modes.
            //  ECB mode does not use an IV.
            //  The length of the IV is equal to the algorithm's block size.
            //  It is NOT equal to the length of the key.
            string ivHex = "0001020304050607";

            crypt.SetEncodedIV(ivHex, "hex");

            //  The secret key must equal the size of the key.  For
            //  256-bit encryption, the binary secret key is 32 bytes.
            //  For 128-bit encryption, the binary secret key is 16 bytes.
            string keyHex = "000102030405060708090A0B0C0D0E0F";

            crypt.SetEncodedKey(keyHex, "hex");

            //  Encrypt a string...
            //  The input string is 44 ANSI characters (i.e. 44 bytes), so
            //  the output should be 48 bytes (a multiple of 8).
            //  Because the output is a hex string, it should
            //  be 96 characters long (2 chars per byte).
            string encStr = crypt.EncryptStringENC(text);
            //Console.WriteLine(encStr);

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);
            //Console.WriteLine(decStr);
        }
Ejemplo n.º 6
0
        public static void performTwoFish(string text)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

            //  Set the encryption algorithm = "twofish"
            crypt.CryptAlgorithm = "twofish";

            //  CipherMode may be "ecb" or "cbc"
            crypt.CipherMode = "cbc";

            //  KeyLength may be 128, 192, 256
            crypt.KeyLength = 256;

            //  The padding scheme determines the contents of the bytes
            //  that are added to pad the result to a multiple of the
            //  encryption algorithm's block size.  Twofish has a block
            //  size of 16 bytes, so encrypted output is always
            //  a multiple of 16.
            crypt.PaddingScheme = 0;

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  It may be "hex", "url", "base64", or "quoted-printable".
            crypt.EncodingMode = "hex";

            //  An initialization vector is required if using CBC mode.
            //  ECB mode does not use an IV.
            //  The length of the IV is equal to the algorithm's block size.
            //  It is NOT equal to the length of the key.
            string ivHex = "000102030405060708090A0B0C0D0E0F";

            crypt.SetEncodedIV(ivHex, "hex");

            //  The secret key must equal the size of the key.  For
            //  256-bit encryption, the binary secret key is 32 bytes.
            //  For 128-bit encryption, the binary secret key is 16 bytes.
            string keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F";

            crypt.SetEncodedKey(keyHex, "hex");

            //  Encrypt a string...
            //  The input string is 44 ANSI characters (i.e. 44 bytes), so
            //  the output should be 48 bytes (a multiple of 16).
            //  Because the output is a hex string, it should
            //  be 96 characters long (2 chars per byte).
            string encStr = crypt.EncryptStringENC(text);
            //  Console.WriteLine(encStr);

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);
            //  Console.WriteLine(decStr);
        }
Ejemplo n.º 7
0
        public static void performARC4(string text)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

            //  Set the encryption algorithm = "arc4"
            crypt.CryptAlgorithm = "arc4";

            //  KeyLength may range from 1 byte to 256 bytes.
            //  (i.e. 8 bits to 2048 bits)
            //  ARC4 key sizes are typically in the range of
            //  40 to 128 bits.
            //  The KeyLength property is specified in bits:
            crypt.KeyLength = 128;

            //  Note: The PaddingScheme and CipherMode properties
            //  do not apply w/ ARC4.  ARC4 does not encrypt in blocks --
            //  it is a streaming encryption algorithm. The number of output bytes
            //  is exactly equal to the number of input bytes.

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  It may be "hex", "url", "base64", or "quoted-printable".
            crypt.EncodingMode = "hex";

            //  Note: ARC4 does not utilize initialization vectors.  IV's only
            //  apply to block encryption algorithms.

            //  The secret key must equal the size of the key.
            //  For 128-bit encryption, the binary secret key is 16 bytes.
            string keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F";

            crypt.SetEncodedKey(keyHex, "hex");

            //  Encrypt a string...
            //  The output length is exactly equal to the input.  In this
            //  example, the input string is 44 chars (ANSI bytes) so the
            //  output is 44 bytes -- and when hex encoded results in an
            //  88-char string (2 chars per byte for the hex encoding).
            string encStr = crypt.EncryptStringENC(text);
            //Console.WriteLine(encStr);

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);
            //Console.WriteLine(decStr);
        }
Ejemplo n.º 8
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();

            if (!checkUnlocked())
            {
                return;
            }

            crypt.CryptAlgorithm = "chacha20";

            //  The key length for chacha20 is always 256-bits.
            crypt.KeyLength    = 256;
            crypt.EncodingMode = "hex";

            string ivHex = "000000000000000000000002";

            crypt.SetEncodedIV(ivHex, "hex");

            crypt.InitialCount = 42;

            string keyHex = "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0";

            crypt.SetEncodedKey(keyHex, "hex");

            string plainText = textBox.Text;

            string encStr = crypt.EncryptStringENC(plainText);

            Debug.WriteLine(encStr);
            textBox1.Text = encStr;

            //  Now decrypt:
            string decStr = crypt.DecryptStringENC(encStr);

            Debug.WriteLine(decStr);
        }
        public static string DecryptRijndael(string _key, string _string)
        {
            Chilkat.Crypt2 _encrypt = new Chilkat.Crypt2();

            bool isUnlocked = _encrypt.UnlockComponent("SCOTTGCrypt_FdXsYk2WWPjN");

            //  AES is also known as Rijndael.
            _encrypt.CryptAlgorithm = "aes";

            //  CipherMode may be "ecb" or "cbc"
            _encrypt.CipherMode = "cbc";

            //  KeyLength may be 128, 192, 256
            _encrypt.KeyLength = 256;

            //  The padding scheme determines the contents of the bytes
            //  that are added to pad the result to a multiple of the
            //  encryption algorithm's block size.  AES has a block
            //  size of 16 bytes, so encrypted output is always
            //  a multiple of 16.
            _encrypt.PaddingScheme = 0;

            //  EncodingMode specifies the encoding of the output for
            //  encryption, and the input for decryption.
            //  It may be "hex", "url", "base64", or "quoted-printable".
            _encrypt.EncodingMode = "hex";

            //  An initialization vector is required if using CBC mode.
            //  ECB mode does not use an IV.
            //  The length of the IV is equal to the algorithm's block size.
            //  It is NOT equal to the length of the key.
            string ivHex;
            ivHex = "000102030405060708090A0B0C0D0E0F";
            _encrypt.SetEncodedIV(ivHex,"hex");

            //  The secret key must equal the size of the key.  For
            //  256-bit encryption, the binary secret key is 32 bytes.
            //  For 128-bit encryption, the binary secret key is 16 bytes.
            string keyHex;
            keyHex = _key;
            _encrypt.SetEncodedKey(keyHex,"hex");

            //  Now decrypt:
            return _encrypt.DecryptStringENC(_string);
        }
Ejemplo n.º 10
0
        public static void performDeffieHelman(string text)
        {
            //  Create two separate instances of the DH object.
            Chilkat.Dh dhBob   = new Chilkat.Dh();
            Chilkat.Dh dhAlice = new Chilkat.Dh();

            //  The DH algorithm begins with a large prime, P, and a generator, G.
            //  These don't have to be secret, and they may be transmitted over an insecure channel.
            //  The generator is a small integer and typically has the value 2 or 5.

            //  The Chilkat DH component provides the ability to use known
            //  "safe" primes, as well as a method to generate new safe primes.

            //  This example will use a known safe prime.  Generating
            //  new safe primes is a time-consuming CPU intensive task
            //  and is normally done offline.

            //  Bob will choose to use the 2nd of our 8 pre-chosen safe primes.
            //  It is the Prime for the 2nd Oakley Group (RFC 2409) --
            //  1024-bit MODP Group.  Generator is 2.
            //  The prime is: 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 }
            dhBob.UseKnownPrime(2);

            //  The computed shared secret will be equal to the size of the prime (in bits).
            //  In this case the prime is 1024 bits, so the shared secret will be 128 bytes (128 * 8 = 1024).
            //  However, the result is returned as an SSH1-encoded bignum in hex string format.
            //  The SSH1-encoding prepends a 2-byte count, so the result is going  to be 2 bytes
            //  longer: 130 bytes.  This results in a hex string that is 260 characters long (two chars
            //  per byte for the hex encoding).

            string p;
            int    g;

            //  Bob will now send P and G to Alice.
            p = dhBob.P;
            g = dhBob.G;

            //  Alice calls SetPG to set P and G.  SetPG checks
            //  the values to make sure it's a safe prime and will
            //  return false if not.


            //  Each side begins by generating an "E"
            //  value.  The CreateE method has one argument: numBits.
            //  It should be set to twice the size of the number of bits
            //  in the session key.

            //  Let's say we want to generate a 128-bit session key
            //  for AES encryption.  The shared secret generated by the Diffie-Hellman
            //  algorithm will be longer, so we'll hash the result to arrive at the
            //  desired session key length.  However, the length of the session
            //  key we'll utlimately produce determines the value that should be
            //  passed to the CreateE method.

            //  In this case, we'll be creating a 128-bit session key, so pass 256 to CreateE.
            //  This setting is for security purposes only -- the value
            //  passed to CreateE does not change the length of the shared secret
            //  that is produced by Diffie-Hellman.
            //  Also, there is no need to pass in a value larger
            //  than 2 times the expected session key length.  It suffices to
            //  pass exactly 2 times the session key length.

            //  Bob generates a random E (which has the mathematical
            //  properties required for DH).
            string eBob;

            eBob = dhBob.CreateE(256);

            //  Alice does the same:
            string eAlice;

            eAlice = dhAlice.CreateE(256);

            //  The "E" values are sent over the insecure channel.
            //  Bob sends his "E" to Alice, and Alice sends her "E" to Bob.

            //  Each side computes the shared secret by calling FindK.
            //  "K" is the shared-secret.

            string kBob;
            string kAlice;

            //  Bob computes the shared secret from Alice's "E":
            kBob = dhBob.FindK(eAlice);

            //  Alice computes the shared secret from Bob's "E":
            kAlice = dhAlice.FindK(eBob);

            //  Amazingly, kBob and kAlice are identical and the expected
            //  length (260 characters).  The strings contain the hex encoded bytes of
            //  our shared secret:
            // Console.WriteLine("Bob's shared secret:");
            // Console.WriteLine(kBob);
            // Console.WriteLine("Alice's shared secret (should be equal to Bob's)");
            // Console.WriteLine(kAlice);

            //  To arrive at a 128-bit session key for AES encryption, Bob and Alice should
            //  both transform the raw shared secret using a hash algorithm that produces
            //  the size of session key desired.   MD5 produces a 16-byte (128-bit) result, so
            //  this is a good choice for 128-bit AES.

            //  Here's how you would use Chilkat Crypt (a separate Chilkat component) to
            //  produce the session key:
            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();


            crypt.EncodingMode  = "hex";
            crypt.HashAlgorithm = "md5";

            string sessionKey;

            sessionKey = crypt.HashStringENC(kBob);

            // Console.WriteLine("128-bit Session Key:");
            // Console.WriteLine(sessionKey);

            //  Encrypt something...
            crypt.CryptAlgorithm = "aes";
            crypt.KeyLength      = 128;
            crypt.CipherMode     = "cbc";

            //  Use an IV that is the MD5 hash of the session key...
            string iv;

            iv = crypt.HashStringENC(sessionKey);

            //  AES uses a 16-byte IV:
            //Console.WriteLine("Initialization Vector:");
            //Console.WriteLine(iv);

            crypt.SetEncodedKey(sessionKey, "hex");
            crypt.SetEncodedIV(iv, "hex");

            //  Encrypt some text:
            string cipherText64;

            crypt.EncodingMode = "base64";
            cipherText64       = crypt.EncryptStringENC(text);
            //Console.WriteLine(cipherText64);

            string plainText;

            plainText = crypt.DecryptStringENC(cipherText64);

            //Console.WriteLine(plainText);
        }
        private void EncDecButton_Click(object sender, EventArgs e)
        {
            try{
                if (StringTextEditor.Text != "" && SecretKeyTextBox.Text != "")
                {
                    Chilkat.Crypt2 crypt = new Chilkat.Crypt2();



                    // AES
                    crypt.CryptAlgorithm = "aes";

                    // CipherMode may be "ecb", "cbc", "ofb", "cfb", "gcm", etc.
                    // Note: Check the online reference documentation to see the Chilkat versions
                    // when certain cipher modes were introduced.
                    crypt.CipherMode = cipherModeMenu.Text;

                    // KeyLength may be 128, 192, 256
                    crypt.KeyLength = Convert.ToInt32(keyLengthDropDownList.Text);

                    // The padding scheme determines the contents of the bytes
                    // that are added to pad the result to a multiple of the
                    // encryption algorithm's block size.  AES has a block
                    // size of 16 bytes, so encrypted output is always
                    // a multiple of 16.
                    crypt.PaddingScheme = 0;

                    // EncodingMode specifies the encoding of the output for
                    // encryption, and the input for decryption.
                    // It may be "hex", "url", "base64", or "quoted-printable".


                    crypt.EncodingMode = encodingModeDropDownList.Text;

                    // An initialization vector is required if using CBC mode.
                    // ECB mode does not use an IV.
                    // The length of the IV is equal to the algorithm's block size.
                    // It is NOT equal to the length of the key.
                    string ivHex = "000102030405060708090A0B0C0D0E0F";
                    crypt.SetEncodedIV(ivHex, "hex");

                    string keyHex = crypt.HashStringENC(SecretKeyTextBox.Text);

                    // The secret key must equal the size of the key.  For
                    // 256-bit encryption, the binary secret key is 32 bytes.
                    // For 128-bit encryption, the binary secret key is 16 bytes.
//                    string keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F";
                    crypt.SetEncodedKey(keyHex, "hex");
                    crypt.Charset = "utf-8";
                    string encStr = "";
                    string decStr = "";

                    if (EncryptToggleSwitch.Value == true)
                    {
                        // Encrypt
                        radProgressBar1.Value2 = 0;
                        encStr = crypt.EncryptStringENC(StringTextEditor.Text);
                        ResultTextEditor.Text  = encStr;
                        radProgressBar1.Value2 = 100;



                        this.radDesktopAlert1.CaptionText = "Abdal AES Encryption";
                        this.radDesktopAlert1.ContentText = "Encryption has been successful.";
                        this.radDesktopAlert1.Show();
                    }
                    else
                    {
                        // Now decrypt:
                        radProgressBar1.Value2 = 0;
                        decStr = crypt.DecryptStringENC(StringTextEditor.Text);
                        ResultTextEditor.Text             = decStr;
                        radProgressBar1.Value2            = 100;
                        this.radDesktopAlert1.CaptionText = "Abdal AES Encryption";
                        this.radDesktopAlert1.ContentText = "Decryption has been successful.";
                        this.radDesktopAlert1.Show();
                    }
                }
                else
                {
                    MessageBox.Show("The String and Secret Password fields must be filled");
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }