{"flag":true,"single":true,"pageTitle":"Securely encode \/ encrypt and decode \/ decrypt a string using PHP.","post":{"id":240,"user_id":"1","slug":"securely-encode-encrypt-and-decode-decrypt-a-string-using-php--avjs","title":"Securely encode \/ encrypt and decode \/ decrypt a string using PHP.","body":"<p>First, you'll need to set up two functions&mdash;one for encryption and another for decryption. We'll use AES-256-CBC for encryption, which is a strong cipher, and you'll need to specify a <strong>secret key<\/strong> and an <strong>initialization vector.<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\n\r\n\r\nfunction encrypt_data($data, $key='rizi_farhan_web404', $iv='1234567890123456') { \r\n\/* $key = ;  \/\/ Ensure this key is kept secret $iv =   \/\/ 16 bytes initialization vector *\/\r\n    $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv);\r\n    return str_replace(array('+', '\/'), array('-', '_'), base64_encode($encrypted));  \/\/ URL-safe encoding\r\n}\r\n\r\nfunction decrypt_data($data, $key='rizi_farhan_web404', $iv='1234567890123456') {\r\n    $data = base64_decode(str_replace(array('-', '_'), array('+', '\/'), $data));  \/\/ URL-safe decoding\r\n    return openssl_decrypt($data, 'aes-256-cbc', $key, 0, $iv);\r\n}\r\n\r\n\/\/ Testing the functions\r\n$originalID = 'Solmate';\r\n$encryptedID = encrypt_data($originalID);\r\n$decryptedID = decrypt_data($encryptedID);\r\n\r\necho \"Original ID: $originalID\\n\";\r\necho \"Encrypted ID: $encryptedID\\n\";\r\necho \"Decrypted ID: $decryptedID\\n\";\r\n?&gt;\r\n<\/code><\/pre>\r\n<p><strong>Output:<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>Original ID: Solmate Encrypted ID: ajVFV09UMmRoV2tvc0xXQnk5dVQ0dz09 Decrypted ID: Solmate<\/code><\/pre>\r\n<p>Key and IV: The key should be a 256-bit key for AES-256 encryption, and the initialization vector (IV) should be 16 bytes. Make sure to keep the key secure and consistent across your application to be able to decrypt the data.<\/p>","category_id":"1","is_private":"0","created_at":"2024-04-16T12:20:38.000000Z","updated_at":"2024-04-16T12:20:38.000000Z","category":{"id":1,"user_id":"1","name":"PHP","slug":"php-3ius","parent_id":null,"created_at":"2023-03-14T03:58:19.000000Z","updated_at":"2023-03-14T03:58:19.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"First, you'll need to set up two functions&mdash;one for encryption and another for decryption. We'll use AES-256-CBC for encryption, which  - Securely encode \/ encrypt and decode \/ decrypt a string using PHP. (Updated: April 16, 2024) - Read more about Securely encode \/ encrypt and decode \/ decrypt a string using PHP. at my programming site [SITE]","categories":[]}