Hi i have this PHP decryption and encryption code for login passwords and i need to check it with python directly from Mysql, so i need to decrypt passwords in python can anyone help me with? Here is php code:
function encryptIt($value) {
$encodeKey = 'MY FIXED KEY';
$encoded = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($encodeKey), $value, MCRYPT_MODE_CBC, md5(md5($encodeKey))));
return($encoded);
}
function decryptIt($value) {
$decodeKey = 'MY FIXED KEY';
$decoded = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($decodeKey), base64_decode($value), MCRYPT_MODE_CBC, md5(md5($decodeKey))), "\0");
return($decoded);
}
first function take password as ($value) and return me encoded one, that will save to Database, and second function take encoded password as ($value) and return me normal password. i need to code second function in python
Related
I am sorry for my English.
The project migrates from Node to Laravel.
I'm the junior, maybe someone will tell me the right direction in the search)))
How to decrypt in Laravel what is encrypted in cryptojs?
In Node, they use CryptoJS and worked with passwords like this:
Encrypt
password: cryptojs.encrypt(JSON.stringify(req.body.password), secretKey).toString(),
Decrypt
cryptojs.decrypt(user.password.toString(), secretKey);
I'm trying to implement a password-checking mechanism in Laravel.
I have secretKey, old password, db stored data.
Tried this
$newEncrypter = new \Illuminate\Encryption\Encrypter(config('app.secret_key'), config('app.cipher'));
$decrypted = $newEncrypter->decrypt( $encrypted );
tinker output: Illuminate\Contracts\Encryption\DecryptException The payload is invalid.
Tried this and it return false
public function check($value, $hashedValue, array $options = [])
{
$hashedBytes = base64_decode($hashedValue);
$iv = substr($hashedBytes, 0, openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = substr($hashedBytes, openssl_cipher_iv_length('aes-256-cbc'));
$decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $iv);
return $value === $decrypted;
}
the encryption in the front end works but the decryption in the backend does not work.
I am using crypto JS for the front end and PHP in the back end. And I want to use AES-256
<script>
function encrypt(){
passphrase= "123456";
encrypted = CryptoJS.AES.encrypt("decrypted soon", passphrase);
return encrypted;
}
</script>
in PHP
public function decrypt ($encrypted)
{
$passphrase= "123456";
$encrypted = base64_decode($encrypted);
$decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA);
return trim($decrypted);
}
Your help is much appreciated
I thought this would be simple but I can't find a simple solution to this..
Is there a way to encode a string and decode a string with a simple passphrase?
somesthing like this:
$encoded = stringEncrypt('someText','simpleKey');
echo stringDecrypt($encoded,'simpleKey'); //outputs someText;
I tried this but with no luck:
function lime_encrypt($data,$key)
{
return base_convert(bin2hex($data),16,10) * base_convert(bin2hex($key),16,10) ;
}
function lime_decrypt($data,$key)
{
return pack("H*",base_convert($data/base_convert(bin2hex($key),16,10),10,16));
}
I think this is because the multiplication doesn't returns a integer but a mathematical expression 6.28414834514E+25
any idea how I can implement this?
edit
I reused another code I found on stack overflow and ended up with this:
function lime_encrypt($data,$key)
{
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $data, MCRYPT_MODE_CBC, md5(md5($key))));
}
function lime_decrypt($data,$key)
{
return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($data), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
}
but I end up with a Fatal error: Call to undefined function mcrypt_encrypt() using php > 5 on linux centOS.
So I'd rather stay simple and movable to other hostings...
Solution?
At the end, I'm using this, found on a blog:
function lime_encrypt($data,$key)
{
return openssl_encrypt($data, 'AES-128-CBC', $key,0,'fgrgfvcfghtfdrfg');
}
function lime_decrypt($data,$key)
{
return openssl_decrypt($data, 'AES-128-CBC', $key,0,'fgrgfvcfghtfdrfg');
}
Is there a way to encode a string and decode a string with a simple passphrase?
Check out defuse/php-encryption. Specifically, the KeyProtectedByPassword feature.
If you're on PHP 7.2, you can also use a combination of sodium_crypto_pwhash() and sodium_crypto_secretbox() (with base64_encode()) to achieve a congruent (but not interoperable) result.
I need to be able to encrypt and decrypt a variable, however I also need to know if the decryption was successful. In my code, when I change the $string variable before decryption to something else, I receive random characters.
function encrypt($string) {
$key = 'password';
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
return $encrypted;
}
function decrypt($encrypted) {
$key = 'password';
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
return $decrypted;
}
$string = 'Hello world.';
if (encrypt($string)) {
$string = encrypt($string);
echo $string;
echo '<br>';
}
if (decrypt($string)) {
$string = decrypt($string);
echo $string;
echo '<br>';
}
Is it possible to detect failed decryption?
Thanks.
The best way to detect tampering of the ciphertext is to add a MAC (message authentication code) over the ciphertext. This is a signature generated by a secret key. The key used for this should be different from the one used to perform the encryption. One way to do this is to use a KDF (key derivation function) over a master key to generate the two keys.
Alternatively it is possible to use an authenticated cipher such as GCM (Galois Counter Mode).
The authentication data generated by the MAC or by the authenticated cipher is called an authentication tag.
A reasonable implementation of what you are trying to achieve seems to be embedded in the Zend framework:
http://www.zimuel.it/en/english-cryptography-made-easy-with-zend-framework/
Don't forget to retrieve and store the salt together with the ciphertext (call getSalt() after encryption, use setSalt() during decryption). Note that the author confuses the word salt and IV often, which is not a good sign.
Disclaimer: I haven't read through the symmetric cipher code deployed by this component, and I haven't tested the code in any way.
I am using a text
const ENCRYPTION_KEY = '3aa22e01c04c7059778c54d122b0273689fba00f4a166a66d15f7ba6a8ba8743';
$str = "1844427316My Name Is Dave1336407610774000000000000";
function encrypt($str){
trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,ENCRYPTION_KEY, $str,MCRYPT_MODE_CBC,mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC),MCRYPT_RAND))));
}
function decrypt($encryptedtext){
return trim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
ENCRYPTION_KEY,
base64_decode($encryptedtext),
MCRYPT_MODE_CBC,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_128,
MCRYPT_MODE_CBC
),
MCRYPT_RAND
)
)
);
}
But whenever I refresh the page calling these function with values mentioned above, I get different values encryption, but in decryption, the initial decrypted value changes everytime but rest gets decrypted correctly like wise:
F7…Ÿ{4©eŠQ9t¤e Is Dave1336407610774000000000000
I have also refered the SIMILAR QUESTION and used the "iv" function in decryption as well as answered in it
Could some one guide me whats getting wrong in here?
It was thoughtful But I found the Solution:
While decrypting, I used the same IV and key as when encrypting.
My encrypt function needs to return the IV as well as the encrypted data. That IV is sent to the decrypt function with the data and the key.
See the below Change in Code with complete code:
class Encypt{
const ENCRYPTION_KEY = '3aa22e01c04c7059778c54d122b0273689fba00f4a166a66d15f7ba6a8ba8743';
function createQueryString(){
$str = "1844427316My Name Is Dave1336407610774000000000000";
$encStr = $this->encrypt($str);
return $encStr;
}
function encrypt($strValue){
$iv =mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC),MCRYPT_RAND);
$encData = trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,self::ENCRYPTION_KEY, $strValue,MCRYPT_MODE_CBC,$iv)));
$data['iv'] = $iv;
$data['encdata'] = $encData;
return $data;
}
/**
* Function to decrypt data using AES Encryption Symmetric Algorithm 128 bytes
*/
function decrypt($strValue, $iv){
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,ENCRYPTION_KEY,base64_decode($strValue),MCRYPT_MODE_CBC,$iv));
}
}
$enc_obj = new Encypt();
$encstr = $enc_obj->createQueryString();
echo "Encrypted Str:-->".$encstr['encdata']."<br>";
$deCrypt = $enc_obj->decrypt($encstr['encdata'], $encstr['iv']);
echo "Decrypted Str:-->".$deCrypt;