0

I want to encrypt some data uisng SHA256 hashing algorithm

<?php
 $encryption_key = "aldskfje49345rgaefa";

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

?>
3
  • The Hash::make function creates and uses a 22-length random string as a salt to generate the password Commented Apr 20, 2022 at 4: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. Commented Apr 20, 2022 at 4: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 stackoverflow.com/a/48482456/6091308 Commented Apr 20, 2022 at 4:42

2 Answers 2

1

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

Read the Manual Here

Just a suggestion...

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.