logo
down
shadow

PGP Encryption - PHP openssl


PGP Encryption - PHP openssl

By : aya H
Date : November 22 2020, 10:31 AM
it fixes the issue OpenSSL does not support OpenPGP, formats and protocols are different. Together with PHP, the most reasonable way to go is interfacing GnuPG using PHP's GnuPG module, I provided an example in the question Encrypt files using PGP in PHP?. Accessing GnuPG directly might be possible if you can execute arbitrary applications from PHP, but this will very likely be restricted (and is considerably more dangerous regarding exploitability).
If you're asked to send OpenPGP encrypted messages but have no access to GnuPG and this module, find another hosting company providing GnuPG, ask your current provider to do so or setup your own web server.
code :


Share : facebook icon twitter icon
openssl aes encryption bash command != ruby openssl aes encryption

openssl aes encryption bash command != ruby openssl aes encryption


By : Zahra
Date : March 29 2020, 07:55 AM
Any of those help The issue is that b.to_s will not return two characters per byte. This means that any byte value less than 16 will be converted into one byte, and if those bytes are concatenated then the hexadecimal digits of different bytes will be combined, resulting in an invalid IV or key. This fact is hidden by the cryptographic API, which may pad the keys and IV to a correct size, instead of rejecting them (as it should).
You can use unpack("H*") instead to perform the correct encoding of the bytes to hexadecimal. This is also nicely symmetric to pack("H*") used for decoding the hexadecimal values.
C# AES-256-CFB like PHP OpenSSL encryption

C# AES-256-CFB like PHP OpenSSL encryption


By : RMD
Date : March 29 2020, 07:55 AM
Hope this helps I think that the OpenSSL CFB mode isn't compatible with .NET: there is a difference in the last block. OpenSSL CFB simply truncates it if it isn't full, while .NET will pad it. This code WILL work if you set the Mode = CipherMode.CBC, but will produce different result if you use the Mode = CipherMode.CFB (you can see that the last characters generated by GenerateSecret() are different). I'm giving a GenerateSecret() and a ExtractSecret() written in C#.
If someone with more experience than me can explain this difference (I've seen the explanation here, but it isn't clear how I should "solve" it).
code :
public static string GenerateSecret(string username, string token)
{
    byte[] key;

    using (var md5 = MD5.Create())
    {
        key = md5.ComputeHash(Encoding.UTF8.GetBytes(username));
    }

    key = BytesToLowerHexBytes(key);

    var iv = Convert.FromBase64String(token);

    if (iv.Length != 16)
    {
        Array.Resize(ref iv, 16);
    }

    byte[] encrypted;

    using (var rijndael = new RijndaelManaged())
    {
        rijndael.Mode = CipherMode.CFB;
        rijndael.Padding = PaddingMode.PKCS7;
        rijndael.KeySize = 256;

        using (var msEncrypt = new MemoryStream())
        {
            using (ICryptoTransform encryptor = rijndael.CreateEncryptor(key, iv))
            using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                var buffer = Encoding.UTF8.GetBytes(token);
                csEncrypt.Write(buffer, 0, buffer.Length);
            }

            encrypted = msEncrypt.ToArray();
        }
    }

    var buffer2 = Encoding.UTF8.GetBytes(Convert.ToBase64String(encrypted));

    using (var ms = new MemoryStream(iv.Length + buffer2.Length))
    {
        ms.Write(iv, 0, iv.Length);
        ms.Write(buffer2, 0, buffer2.Length);

        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
    }
}

public static string ExtractSecret(string username, string encrypted)
{
    byte[] key;

    using (var md5 = MD5.Create())
    {
        key = md5.ComputeHash(Encoding.UTF8.GetBytes(username));
    }

    key = BytesToLowerHexBytes(key);

    byte[] bytes = Convert.FromBase64String(encrypted);

    byte[] iv = bytes;

    Array.Resize(ref iv, 16);

    byte[] decrypted;

    using (var rijndael = new RijndaelManaged())
    {
        rijndael.Mode = CipherMode.CFB;
        rijndael.Padding = PaddingMode.PKCS7;
        rijndael.KeySize = 256;

        using (var msDecrypt = new MemoryStream())
        {
            using (ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv))
            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
            {
                var buffer = Convert.FromBase64String(Encoding.UTF8.GetString(bytes, 16, bytes.Length - 16));
                csDecrypt.Write(buffer, 0, buffer.Length);
            }

            decrypted = msDecrypt.ToArray();
        }
    }

    return Encoding.UTF8.GetString(decrypted);
}

private static byte[] BytesToLowerHexBytes(byte[] bytes)
{
    // The hash is a hex string
    var bytes2 = new byte[bytes.Length * 2];

    for (int i = 0, j = 0; i < bytes.Length; i++)
    {
        byte b1 = (byte)(bytes[i] >> 4);
        bytes2[j] = (byte)(b1 <= 9 ? '0' + b1 : 'a' + b1 - 10);
        j++;
        byte b2 = (byte)(bytes[i] & 15);
        bytes2[j] = (byte)(b2 <= 9 ? '0' + b2 : 'a' + b2 - 10);
        j++;
    }

    return bytes2;
}
    public static string GenerateSecret(string username, string token)
    {
        byte[] key;

        using (var md5 = MD5.Create())
        {
            key = md5.ComputeHash(Encoding.UTF8.GetBytes(username));
        }

        key = BytesToLowerHexBytes(key);

        var iv = Convert.FromBase64String(token);

        if (iv.Length != 16)
        {
            Array.Resize(ref iv, 16);
        }

        byte[] encrypted;
        int encryptedLength;

        using (var rijndael = new RijndaelManaged())
        {
            rijndael.Mode = CipherMode.CFB;
            rijndael.Padding = PaddingMode.Zeros;
            rijndael.KeySize = 256;

            using (var msEncrypt = new MemoryStream())
            {
                var buffer = Encoding.UTF8.GetBytes(token);

                using (ICryptoTransform encryptor = rijndael.CreateEncryptor(key, iv))
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(buffer, 0, buffer.Length);
                }

                // CFB is a stream cipher, where the length of the encrypted text should be
                // equal to the length of the original text... So we strip the last bytes
                encrypted = msEncrypt.GetBuffer();
                encryptedLength = buffer.Length;
            }
        }

        var buffer2 = Encoding.UTF8.GetBytes(Convert.ToBase64String(encrypted, 0, encryptedLength));

        using (var ms = new MemoryStream(iv.Length + buffer2.Length))
        {
            ms.Write(iv, 0, iv.Length);
            ms.Write(buffer2, 0, buffer2.Length);

            return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
        }
    }

    public static string ExtractSecret(string username, string encrypted)
    {
        byte[] key;

        using (var md5 = MD5.Create())
        {
            key = md5.ComputeHash(Encoding.UTF8.GetBytes(username));
        }

        key = BytesToLowerHexBytes(key);

        byte[] bytes = Convert.FromBase64String(encrypted);

        byte[] iv = bytes;

        Array.Resize(ref iv, 16);

        byte[] decrypted;
        int decryptedLength;

        using (var rijndael = new RijndaelManaged())
        {
            rijndael.Mode = CipherMode.CFB;
            rijndael.Padding = PaddingMode.Zeros;
            rijndael.KeySize = 256;

            using (var msDecrypt = new MemoryStream())
            {
                var buffer = Convert.FromBase64String(Encoding.UTF8.GetString(bytes, 16, bytes.Length - 16));

                using (ICryptoTransform decryptor = rijndael.CreateDecryptor(key, iv))
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
                {
                    csDecrypt.Write(buffer, 0, buffer.Length);

                    // We have to add the remaining bytes of the block. They aren't
                    // important, so we add zeroes
                    int remaining = 16 - (buffer.Length % 16);

                    if (remaining != 0)
                    {
                        csDecrypt.Write(new byte[remaining], 0, remaining);
                    }
                }

                // CFB is a stream cipher, where the length of the encrypted text should be
                // equal to the length of the original text... So we strip the last bytes
                decrypted = msDecrypt.GetBuffer();
                decryptedLength = buffer.Length;
            }
        }

        return Encoding.UTF8.GetString(decrypted, 0, decryptedLength);
    }
openssl -aes-128-ecb encryption doesn't match python Crypto.Cipher AES encryption

openssl -aes-128-ecb encryption doesn't match python Crypto.Cipher AES encryption


By : Diego Rodrigues
Date : March 29 2020, 07:55 AM
hop of those help? In python, encrypting using AES from Crypto.Cipher takes a key (string of 16 bytes) and a plaintext (16 bytes) and outputs a ciphertext (16 bytes).
To achieve the same with OpenSSL you need to first disable salting and padding with -nosalt and -nopad to ensure it takes 16 byte inputs and returns 16 byte outputs. Supplying a password results in OpenSSL deriving its own key. To override this, use the -K option (where the key needs to be given in hex). Alternatively, inputting a password and specifying -p will make OpenSSL spit out the key that it uses.
code :
from Crypto.Cipher import AES
from base64 import b64encode

plaintext = 'Attack at dawn!!'
obj = AES.new("yellow submarine", AES.MODE_ECB)
ciphertext = obj.encrypt(plaintext)

print(b64encode(ciphertext).decode())
enc -aes-128-ecb openssl enc -aes-128-ecb -nosalt -nopad -K 79656c6c6f77207375626d6172696e65 -in plain.txt -out cipher.txt -a
from Crypto.Cipher import AES
from base64 import b64encode

hex_key = 'A35EC217E15C1DD258201A184814897C'
key = bytes.fromhex(hex_key)

plaintext = 'Attack at dawn!!'
obj = AES.new(key, AES.MODE_ECB)
ciphertext = obj.encrypt(plaintext)

print(b64encode(ciphertext).decode())
How to do encryption using AES in Openssl

How to do encryption using AES in Openssl


By : dvterrizzi
Date : March 29 2020, 07:55 AM
Any of those help Check out this link it has a example code to encrypt/decrypt data using AES256CBC using EVP API.
https://github.com/saju/misc/blob/master/misc/openssl_aes.c
PHP encryption using OpenSSL

PHP encryption using OpenSSL


By : Yogesh s
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Your encryption key changes with each function call using openssl_random_pseudo_bytes
Make the key static such as $encryption_key = "XXXX"; or global the variable and only call it once.
Related Posts Related Posts :
  • Curl PHP cannot display amazon
  • Symfony, getters and setters vs magic methods
  • Using 'continue' PHP instruction outside the loop
  • AJAX POST return data not appearing
  • Can I query relations using an INNER JOIN instead of two queries in Eloquent?
  • Looping through dynamic form fields and inserting into database
  • My php code can't select mysql auto_increment value
  • Store Angularjs form data in database using php
  • I want to run my sh file continuously even if I close my Putty connection
  • file_get_contents equivalent for gzipped files
  • Include PHP file with jQuery
  • php curl headers do not return from website?
  • How to find out, if facebook ID is a user, group or page
  • Connect webhost database to android database
  • preg_match get div content with class
  • Upload multiple files in Laravel 4
  • Count array numbers in multidimensional array
  • PHP Date diff with a difference
  • Search Customer by custom field in Netsuite
  • Is it possible to hide/encode/encrypt php source code and let others have the system?
  • list items to be displayed using php code and array
  • check if row exists mysqli
  • PHP errors loading MySQL
  • setup PostgreSQL with Laravel in MAMP
  • PHP - CodeIgniter Notifications
  • Encrypting a password column in a SQL database
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • Error with the ereg_replace
  • Getting the fields attached to a bundle of an entity in Drupal
  • Php how to check multiple times (within `foreach`) if values from array exists in mysql?
  • MySQL and PHP parsing strange string
  • Pull specific data from multidimensional associative array
  • How to get the original URI extension using PHP Tonic?
  • Upload videos and images from Android to PHP server?
  • Unset a multidimensional array with another multidimensional array with values than key
  • Calling a user e-mail to add to a mailto link PHP
  • Why is override_function coming back as an undefined function
  • PHP mySQLi_fetch_all: iterate through each row
  • Using pow in php
  • What's wrong with my PHP Script? T_String issue
  • how to call a function of another file without including that file
  • is_rgb function in php
  • transform the numbers to letters using php
  • How to catch an exception from another class method PHP
  • PHP exec is blocked by ISP
  • PHP4 to PHP5 migration character encoding. latin1 vs utf8
  • Begin Transaction not defined
  • How much harm can a user do with xss on a page unique to them?
  • How to import data from a php file to javascript variable
  • PHP and Javascript - log complete workflow
  • When I echo !!! 0 it gives me a strange result
  • jquery ajax post dies after submit
  • How to upload file with curl on sftp server
  • Advanced search using a text input to search through only one category
  • Why do I get "Non-static method App\Models\Category::products() should not be called statically" in laravel
  • Stop notices from undefined indexes in arrays
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org