0

I want to encrypt some data uisng SHA256 hashing algorithm

<?php
 $encryption_key = "aldskfje49345rgaefa";

 echo hash('sha256', "hello world");

?>
swapnil mane
  • 215
  • 5
  • 17
  • The Hash::make function creates and uses a 22-length random string as a salt to generate the password – Michael Mano Apr 20 '22 at 04:29
  • Sir, I have a problem where my friend is using C#. He has encrypted a string using SHA256 algorithm and he has also provided me an encryption key. The thing we have to achieve is I will encrypt a string from here and he will decrypt it using that particular encryption key. So I have to pass that encryption key to that crypt function. Hope you have understood my problem. – swapnil mane Apr 20 '22 at 04:35
  • This is not possible with SHA256, its not meant to be decrypted. Dont get me wrong. You can but only with brute force. its meant to be as a verification such as a password. You can use `aes` https://stackoverflow.com/a/48482456/6091308 – Michael Mano Apr 20 '22 at 04:42

2 Answers2

1

Try Using hash_hmac() Like this hash_hmac('sha256',);

Read the Manual Here

Just a suggestion...

Sublan Mustafa
  • 199
  • 1
  • 6
0

When you do this

echo hash('sha256', "hello world");

You are not encrypting your data but you are hashing them.

The difference between both ?

Hashing is one way. You generate an unique and irreversible hash.
Encrypting is two way. You can encrypt your data with a key and decrypt with the same key.

So in your situation, you should avoid using SHA256 algorithm. Instead, I recommand you to use Crypt facade provided by Laravel. The key used for encrypting your data is your APP_KEY environnement variable (which is generated when you install your project). If you have not this, you can simply generate it by execute this command :

php artisan key:generate

So, you will need to share the key with your friend, and use the same algorithm. I let you read the documentation about Crypt Facade.

  • Yes but I want to encrypt some data with the key a friend of mine provided to me. How can I do this ?? Note: He has used SHA256 algorithm – swapnil mane Apr 21 '22 at 04:39
  • You won't be able to encrypt your data with algorithm SHA256. As I explained, SHA256 will HASH your data and not ENCRYPT them. You and your friend need to change the process and use Encrypt algorithm. – Théo Champion Apr 21 '22 at 09:36