Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Oranzh/2520823f9d1cea603e60b8e8f3fe1d36 to your computer and use it in GitHub Desktop.
Save Oranzh/2520823f9d1cea603e60b8e8f3fe1d36 to your computer and use it in GitHub Desktop.

Revisions

  1. @odan odan revised this gist Feb 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion with_bcrypt_password_hash.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ A more secure password should contain a cost factor. Here is the complete exampl
    ```php
    <?php

    $plaintext = 'My secret message 1234 Сейчас в СМИ';
    $plaintext = 'My secret message 1234';
    $password = '3sc3RLrpd17';
    $method = 'aes-256-cbc';

  2. @odan odan revised this gist Feb 22, 2018. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions with_bcrypt_password_hash.md
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,12 @@ In C# you have to install the [BCrypt-Official package](https://www.nuget.org/pa
    generate a hash password like this:

    ```csharp

    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
    using System;

    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
  3. @odan odan revised this gist Feb 22, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions with_bcrypt_password_hash.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    A more secure password should contain a cost factor.
    ## Version 2

    ```
    A more secure password should contain a cost factor. Here is the complete example code:

    ### PHP

    ```php
    <?php

    $plaintext = 'My secret message 1234 Сейчас в СМИ';
    @@ -26,6 +30,8 @@ echo 'decrypted to: ' . $decrypted . "\n\n";

    ```

    ### C#

    In C# you have to install the [BCrypt-Official package](https://www.nuget.org/packages/BCrypt-Official/) and
    generate a hash password like this:

  4. @odan odan revised this gist Feb 22, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions AES-256 encryption and decryption in PHP and C#.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # AES-256 encryption and decryption in PHP and C#

    **Update**: There is a more secure version available. [Details](#file-with_bcrypt_password_hash-md)

    ## PHP

    ```php
  5. @odan odan revised this gist Feb 22, 2018. 1 changed file with 156 additions and 0 deletions.
    156 changes: 156 additions & 0 deletions with_bcrypt_password_hash.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,156 @@
    A more secure password should contain a cost factor.

    ```
    <?php
    $plaintext = 'My secret message 1234 Сейчас в СМИ';
    $password = '3sc3RLrpd17';
    $method = 'aes-256-cbc';
    $key = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
    echo "Key:" . $key . "\n";
    // IV must be exact 16 chars (128 bit)
    $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
    // av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
    $encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));
    // My secret message 1234
    $decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);
    echo 'plaintext=' . $plaintext . "\n";
    echo 'cipher=' . $method . "\n";
    echo 'encrypted to: ' . $encrypted . "\n";
    echo 'decrypted to: ' . $decrypted . "\n\n";
    ```

    In C# you have to install the [BCrypt-Official package](https://www.nuget.org/packages/BCrypt-Official/) and
    generate a hash password like this:

    ```csharp
    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes.Create();

    encryptor.Mode = CipherMode.CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor.Key = key.Take(32).ToArray();
    encryptor.IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);

    // Convert the plainText string into a byte array
    byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

    // Encrypt the input plaintext string
    cryptoStream.Write(plainBytes, 0, plainBytes.Length);

    // Complete the encryption process
    cryptoStream.FlushFinalBlock();

    // Convert the encrypted data from a MemoryStream to a byte array
    byte[] cipherBytes = memoryStream.ToArray();

    // Close both the MemoryStream and the CryptoStream
    memoryStream.Close();
    cryptoStream.Close();

    // Convert the encrypted byte array to a base64 encoded string
    string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

    // Return the encrypted data as a string
    return cipherText;
    }

    public string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes.Create();

    encryptor.Mode = CipherMode.CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor.Key = key.Take(32).ToArray();
    encryptor.IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);

    // Will contain decrypted plaintext
    string plainText = String.Empty;

    try
    {
    // Convert the ciphertext string into a byte array
    byte[] cipherBytes = Convert.FromBase64String(cipherText);

    // Decrypt the input ciphertext string
    cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);

    // Complete the decryption process
    cryptoStream.FlushFinalBlock();

    // Convert the decrypted data from a MemoryStream to a byte array
    byte[] plainBytes = memoryStream.ToArray();

    // Convert the decrypted byte array to string
    plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
    }
    finally
    {
    // Close both the MemoryStream and the CryptoStream
    memoryStream.Close();
    cryptoStream.Close();
    }

    // Return the decrypted data as a string
    return plainText;
    }
    ```

    ### Usage

    ```csharp
    string message = "My secret message 1234";
    string password = "3sc3RLrpd17";

    // hash the password with BCrypt
    string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password, 12);

    // Convert hashed password to array
    byte[] key = Encoding.ASCII.GetBytes(hashedPassword);

    // Create secret IV
    byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

    string encrypted = this.EncryptString(message, key, iv);
    string decrypted = this.DecryptString(encrypted, key, iv);

    Console.WriteLine(encrypted);
    Console.WriteLine(decrypted);
    ```
  6. @odan odan revised this gist Feb 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AES-256 encryption and decryption in PHP and C#.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # AES-256 encryption and decryption in PHP and C#
    # AES-256 encryption and decryption in PHP and C#

    ## PHP

  7. @odan odan revised this gist Feb 22, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions AES-256 encryption and decryption in PHP and C#.md
    Original file line number Diff line number Diff line change
    @@ -123,15 +123,15 @@ public string DecryptString(string cipherText, byte[] key, byte[] iv)
    // Convert the decrypted data from a MemoryStream to a byte array
    byte[] plainBytes = memoryStream.ToArray();

    // Convert the encrypted byte array to string
    // Convert the decrypted byte array to string
    plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
    } finally {
    // Close both the MemoryStream and the CryptoStream
    memoryStream.Close();
    cryptoStream.Close();
    }

    // Return the encrypted data as a string
    // Return the decrypted data as a string
    return plainText;
    }
    ```
  8. @odan odan revised this gist Feb 22, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion AES-256 encryption and decryption in PHP and C#.md
    Original file line number Diff line number Diff line change
    @@ -123,7 +123,7 @@ public string DecryptString(string cipherText, byte[] key, byte[] iv)
    // Convert the decrypted data from a MemoryStream to a byte array
    byte[] plainBytes = memoryStream.ToArray();

    // Convert the encrypted byte array to a base64 encoded string
    // Convert the encrypted byte array to string
    plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
    } finally {
    // Close both the MemoryStream and the CryptoStream
  9. @odan odan revised this gist Feb 22, 2018. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion AES-256 encryption and decryption in PHP and C#.md
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,11 @@ echo 'decrypted to: ' . $decrypted . "\n\n";
    ## C#

    ```csharp
    using System.Security.Cryptography;
    using System.IO;
    using System.Text;
    using System;

    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    @@ -62,7 +67,7 @@ public string EncryptString(string plainText, byte[] key, byte[] iv)
    cryptoStream.Write(plainBytes, 0, plainBytes . Length);

    // Complete the encryption process
    cryptoStream . FlushFinalBlock();
    cryptoStream.FlushFinalBlock();

    // Convert the encrypted data from a MemoryStream to a byte array
    byte[] cipherBytes = memoryStream.ToArray();
  10. @odan odan renamed this gist Aug 10, 2017. 1 changed file with 0 additions and 0 deletions.
  11. @odan odan revised this gist Aug 10, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Encrypt & Decrypt C# function to PHP function.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Encrypt & Decrypt C# function to PHP function
    # AES-256 encryption and decryption in PHP and C#

    ## PHP

    @@ -138,7 +138,7 @@ string message = "My secret message 1234";
    string password = "3sc3RLrpd17";

    // Create sha256 hash
    SHA256 mySHA256 = SHA256Managed . Create();
    SHA256 mySHA256 = SHA256Managed.Create();
    byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));

    // Create secret IV
  12. @odan odan revised this gist Aug 10, 2017. 1 changed file with 30 additions and 31 deletions.
    61 changes: 30 additions & 31 deletions Encrypt & Decrypt C# function to PHP function.md
    Original file line number Diff line number Diff line change
    @@ -34,45 +34,45 @@ echo 'decrypted to: ' . $decrypted . "\n\n";
    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes . Create();
    Aes encryptor = Aes.Create();

    encryptor . Mode = CipherMode . CBC;
    encryptor.Mode = CipherMode.CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor . Key = key;
    encryptor . IV = iv;
    encryptor.Key = key;
    encryptor.IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesEncryptor = encryptor . CreateEncryptor();
    ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode . Write);

    // Convert the plainText string into a byte array
    byte[] plainBytes = Encoding . ASCII . GetBytes(plainText);
    byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);

    // Encrypt the input plaintext string
    cryptoStream . Write(plainBytes, 0, plainBytes . Length);
    cryptoStream.Write(plainBytes, 0, plainBytes . Length);

    // Complete the encryption process
    cryptoStream . FlushFinalBlock();

    // Convert the encrypted data from a MemoryStream to a byte array
    byte[] cipherBytes = memoryStream . ToArray();
    byte[] cipherBytes = memoryStream.ToArray();

    // Close both the MemoryStream and the CryptoStream
    memoryStream . Close();
    cryptoStream . Close();
    memoryStream.Close();
    cryptoStream.Close();

    // Convert the encrypted byte array to a base64 encoded string
    string cipherText = Convert . ToBase64String(cipherBytes, 0, cipherBytes . Length);
    string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);

    // Return the encrypted data as a string
    return cipherText;
    @@ -81,49 +81,49 @@ public string EncryptString(string plainText, byte[] key, byte[] iv)
    public string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes . Create();
    Aes encryptor = Aes.Create();

    encryptor . Mode = CipherMode . CBC;
    encryptor.Mode = CipherMode.CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor . Key = key;
    encryptor . IV = iv;
    encryptor.Key = key;
    encryptor.IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesDecryptor = encryptor . CreateDecryptor();
    ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode . Write);

    // Will contain decrypted plaintext
    string plainText = String . Empty;
    string plainText = String.Empty;

    try {
    // Convert the ciphertext string into a byte array
    byte[] cipherBytes = Convert . FromBase64String(cipherText);
    byte[] cipherBytes = Convert.FromBase64String(cipherText);

    // Decrypt the input ciphertext string
    cryptoStream . Write(cipherBytes, 0, cipherBytes . Length);
    cryptoStream.Write(cipherBytes, 0, cipherBytes . Length);

    // Complete the decryption process
    cryptoStream . FlushFinalBlock();
    cryptoStream.FlushFinalBlock();

    // Convert the decrypted data from a MemoryStream to a byte array
    byte[] plainBytes = memoryStream . ToArray();
    byte[] plainBytes = memoryStream.ToArray();

    // Convert the encrypted byte array to a base64 encoded string
    plainText = Encoding . ASCII . GetString(plainBytes, 0, plainBytes . Length);
    plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
    } finally {
    // Close both the MemoryStream and the CryptoStream
    memoryStream . Close();
    cryptoStream . Close();
    memoryStream.Close();
    cryptoStream.Close();
    }

    // Return the encrypted data as a string
    @@ -139,17 +139,16 @@ string password = "3sc3RLrpd17";

    // Create sha256 hash
    SHA256 mySHA256 = SHA256Managed . Create();
    byte[] key = mySHA256 . ComputeHash(Encoding . ASCII . GetBytes(password));
    byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password));

    // Create secret IV
    byte[] iv = new byte[16] {
    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
    byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

    string encrypted = this . EncryptString(message, key, iv);
    string decrypted = this . DecryptString(encrypted, key, iv);
    string encrypted = this.EncryptString(message, key, iv);
    string decrypted = this.DecryptString(encrypted, key, iv);

    Console . WriteLine(encrypted);
    Console . WriteLine(decrypted);
    Console.WriteLine(encrypted);
    Console.WriteLine(decrypted);
    ```


  13. @odan odan created this gist Aug 10, 2017.
    156 changes: 156 additions & 0 deletions Encrypt & Decrypt C# function to PHP function.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,156 @@
    # Encrypt & Decrypt C# function to PHP function

    ## PHP

    ```php
    <?php

    $plaintext = 'My secret message 1234';
    $password = '3sc3RLrpd17';
    $method = 'aes-256-cbc';

    // Must be exact 32 chars (256 bit)
    $password = substr(hash('sha256', $password, true), 0, 32);
    echo "Password:" . $password . "\n";

    // IV must be exact 16 chars (128 bit)
    $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);

    // av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU=
    $encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv));

    // My secret message 1234
    $decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv);

    echo 'plaintext=' . $plaintext . "\n";
    echo 'cipher=' . $method . "\n";
    echo 'encrypted to: ' . $encrypted . "\n";
    echo 'decrypted to: ' . $decrypted . "\n\n";
    ```

    ## C#

    ```csharp
    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes . Create();

    encryptor . Mode = CipherMode . CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor . Key = key;
    encryptor . IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesEncryptor = encryptor . CreateEncryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode . Write);

    // Convert the plainText string into a byte array
    byte[] plainBytes = Encoding . ASCII . GetBytes(plainText);

    // Encrypt the input plaintext string
    cryptoStream . Write(plainBytes, 0, plainBytes . Length);

    // Complete the encryption process
    cryptoStream . FlushFinalBlock();

    // Convert the encrypted data from a MemoryStream to a byte array
    byte[] cipherBytes = memoryStream . ToArray();

    // Close both the MemoryStream and the CryptoStream
    memoryStream . Close();
    cryptoStream . Close();

    // Convert the encrypted byte array to a base64 encoded string
    string cipherText = Convert . ToBase64String(cipherBytes, 0, cipherBytes . Length);

    // Return the encrypted data as a string
    return cipherText;
    }

    public string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
    // Instantiate a new Aes object to perform string symmetric encryption
    Aes encryptor = Aes . Create();

    encryptor . Mode = CipherMode . CBC;
    //encryptor.KeySize = 256;
    //encryptor.BlockSize = 128;
    //encryptor.Padding = PaddingMode.Zeros;
    // Set key and IV
    encryptor . Key = key;
    encryptor . IV = iv;

    // Instantiate a new MemoryStream object to contain the encrypted bytes
    MemoryStream memoryStream = new MemoryStream();

    // Instantiate a new encryptor from our Aes object
    ICryptoTransform aesDecryptor = encryptor . CreateDecryptor();

    // Instantiate a new CryptoStream object to process the data and write it to the
    // memory stream
    CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode . Write);

    // Will contain decrypted plaintext
    string plainText = String . Empty;

    try {
    // Convert the ciphertext string into a byte array
    byte[] cipherBytes = Convert . FromBase64String(cipherText);

    // Decrypt the input ciphertext string
    cryptoStream . Write(cipherBytes, 0, cipherBytes . Length);

    // Complete the decryption process
    cryptoStream . FlushFinalBlock();

    // Convert the decrypted data from a MemoryStream to a byte array
    byte[] plainBytes = memoryStream . ToArray();

    // Convert the encrypted byte array to a base64 encoded string
    plainText = Encoding . ASCII . GetString(plainBytes, 0, plainBytes . Length);
    } finally {
    // Close both the MemoryStream and the CryptoStream
    memoryStream . Close();
    cryptoStream . Close();
    }

    // Return the encrypted data as a string
    return plainText;
    }
    ```

    Usage

    ```csharp
    string message = "My secret message 1234";
    string password = "3sc3RLrpd17";

    // Create sha256 hash
    SHA256 mySHA256 = SHA256Managed . Create();
    byte[] key = mySHA256 . ComputeHash(Encoding . ASCII . GetBytes(password));

    // Create secret IV
    byte[] iv = new byte[16] {
    0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };

    string encrypted = this . EncryptString(message, key, iv);
    string decrypted = this . DecryptString(encrypted, key, iv);

    Console . WriteLine(encrypted);
    Console . WriteLine(decrypted);
    ```


    Source: https://stackoverflow.com/a/45574121/1461181