Encrypt string based on a keyword in php

0

I'd like to know if there is a way to encrypt a string reversibly based on a password. To be able to decrypt, you must know the password. Like base64_encode() and base64_decode() but use a keyword.

    
asked by anonymous 19.02.2016 / 12:58

4 answers

2

Yes, look for the functions openssl_encrypt and openssl_decrypt .

    
19.02.2016 / 13:55
2

If you want something like a message system in which only the user and the one with whom you talk can read the message, type them having a secret code between them that would look like this:

function encrypt($mensagem, $a_chave_do_usuario){
$salt = SALT;
$key = substr(hash('sha256', $salt.$key.$salt), 0, 32);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv));
return $encrypted;
}

function decrypt($mensagem, $a_chave_do_usuario){
$salt = SALT;
$key = substr(hash('sha256', $salt.$key.$salt), 0, 32);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($data), MCRYPT_MODE_ECB, $iv);
return $decrypted;
}

The SALT would be a constant of the type system: 'A3jdh $ 4D8gkb # 2T7 & sM40bn4A7fvv35cf = b48On = 84c! 623Vvf6vi37vb34 @ 3kf%' Already the user key would be anything he wants when sending a message, for example if he matches someone a keyword between the two only the two could decode the message.

So you can get an idea,

If the user type 'I' and put as secret phrase 'is stealthy' the result that SALT there is exactly:

yKwhaIqvlh2cLgLe4TXXC1No5W8gVpUj6rRpNG6 / EME =

Hardly anyone would know that this is the word 'I'. Only so-and-so could read this, nor would the system administrator know what they are talking about, since the two agreed that they would use the 'secret' keyword among them. Unless the developer saves the keyword of each message somewhere, just to snoop the conversations from others kkkk.

Hope it helps, the possibilities are endless. My session uses something similar a little more complicated because it uses 2 keys. One of the system with plenty of 64-digit alphanumeric and special characters and a dynamics with 128 characters generated with sha512.

    
29.01.2017 / 17:19
-1

public static final String CHAVE = "PALAVRA_CHAVE";

public static String criptografia(String senha) {
    String aux = "";
    int tamanhoString = senha.length();
    int tamanhoChave = CHAVE.length();

    if (tamanhoString > tamanhoChave) {
        senha = senha.substring(0, CHAVE.length());
    }
    senha = Funcoes.rTrim(senha);

    for (int i = 0; i < senha.length(); i++) {
        int pos = i / tamanhoChave;
        if (pos == 0) {
            pos = tamanhoChave;
        }
        int posLetra = senha.charAt(i) ^ CHAVE.charAt(i);
        if (posLetra == 0) {
            posLetra = senha.charAt(i);
        }
        aux += (char) posLetra;
    }
    return aux;
}

public static String rTrim(String s) {
    int i = s.length() - 1;
    while (i > 0 && Character.isWhitespace(s.charAt(i))) {
        i--;
    }
    return s.substring(0, i + 1);
}

If you pass a 1234 password to the function, it will return you # $% $ for example, and if you pass the encryption, the function returns you the original password, then you have to adapt your logic and do php.

    
19.02.2016 / 13:21
-3

How to generate a key for discriptografa An email

    
30.06.2017 / 01:10