Alex Justi
Alex Justi

Reputation: 167

How do I encrypt and store private messages?

I am developing a private messaging system for my website using Laravel 4, and I want to ensure that the messages remain private. So far, I have the following code written:

class PkeyEncryption {

    public static function encrypt($input, $cipher = MCRYPT_RIJNDAEL_128) {

        $key = sha1(microtime(true) . mt_rand(10000, 90000));

        $iv_size = mcrypt_get_size($cipher, MCRYPT_MODE_CFB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

        return mcrypt_encrypt($cipher, $key, $input, MCRYPT_MODE_CFB, $iv);
    }

    public static function decrypt($data, $key, $cipher = MCRYPT_RIJNDAEL_128) {

        $iv = $data['iv'];
        $data = $data['data'];

        return mcrypt_decrypt($cipher, $key, $data, MCRYPT_MODE_CFB, $iv);
    }
}

So, I know how to encrypt the messages, and I also know that I can store the IV alongside the message. But, I don't know where I am supposed to store the public key. I have already read a few other questions on the site, and I still haven't found an answer. Can somebody please point me in the right direction?

Upvotes: 1

Views: 1554

Answers (1)

bulforce
bulforce

Reputation: 1071

You have to store all users public keys on the server and only the users themselves should have their own private keys.

When user A wants to send message to user B, he will take user B public key and encrypt the message with it. This message can then be decrypted only with the user B private key.

Upvotes: 1

Related Questions