php加密ts,在nodejs中加密,在php中解密

你的方法不安全,也可能是中间人攻击的受害者,你应该始终使用静脉注射和HMAC。

你可以用这样的PHP加密

$key = substr('encyptionsec123342',0,32)

function encrypt ($message, $method, $secret, &$hmac) {

$iv = substr(bin2hex(openssl_random_pseudo_bytes(16)),0,16);

$encrypted = base64_encode($iv) . openssl_encrypt($message, $method, $secret, 0, $iv);

$hmac = hash_hmac('md5', $encrypted, $secret);

return $encrypted;

}

function decrypt ($encrypted, $method, $secret, $hmac) {

if (hash_hmac('md5', $encrypted, $secret) == $hmac) {

$iv = base64_decode(substr($encrypted, 0, 24));

return openssl_decrypt(substr($encrypted, 24), $method, $secret, 0, $iv);

}

}

function encryptWithTSValidation ($message, $method, $secret, &$hmac) {

date_default_timezone_set('UTC');

$message = substr(date('c'),0,19) . "$message";

return encrypt($message, $method, $secret, $hmac);

}

function decryptWithTSValidation ($encrypted, $method, $secret, $hmac, $intervalThreshold) {

$decrypted = decrypt($encrypted, $method, $secret, $hmac);

$now = new DateTime();

$msgDate = new DateTime(str_replace("T"," ",substr($decrypted,0,19)));

if (($now->getTimestamp() - $msgDate->getTimestamp()) <= $intervalThreshold) {

return substr($decrypted,19);

}

}

这是一种安全的AES-256-CBC方法,使用HMAC阻止中间人攻击。

J.N.

var secret = "encyptionsec123342";

secret = secret.substr(0, 32);

var method = 'AES-256-CBC';

var encrypt = function(message, method, secret, hmac) {

var iv = crypto.randomBytes(16).toString('hex').substr(0, 16);

var encryptor = crypto.createCipheriv(method, secret, iv);

var encrypted = new Buffer.alloc(iv).toString('base64') + encryptor.update(message, 'utf8', 'base64') + encryptor.final('base64');

hmac = crypto.createHmac('md5', secret).update(encrypted).digest('hex');

return encrypted;

};

var decrypt = function(encrypted, method, secret, hmac) {

if (crypto.createHmac('md5', secret).update(encrypted).digest('hex') == hmac) {

var iv = new Buffer.from(encrypted.substr(0, 24), 'base64').toString();

var decryptor = crypto.createDecipheriv(method, secret, iv);

return decryptor.update(encrypted.substr(24), 'base64', 'utf8') + decryptor.final('utf8');

}

};

var encryptWithTSValidation = function(message, method, secret, hmac) {

var messageTS = new Date().toISOString().substr(0, 19) + message;

return encrypt(messageTS, method, secret, hmac);

}

var decryptWithTSValidation = function(encrypted, method, secret, hmac, intervalThreshold) {

var decrypted = decrypt(encrypted, method, secret, hmac);

var now = new Date();

var year = parseInt(decrypted.substr(0, 4)),

month = parseInt(decrypted.substr(5, 2)) - 1,

day = parseInt(decrypted.substr(8, 2)),

hour = parseInt(decrypted.substr(11, 2)),

minute = parseInt(decrypted.substr(14, 2)),

second = parseInt(decrypted.substr(17, 2));

var msgDate = new Date(Date.UTC(year, month, day, hour, minute, second))

if (Math.round((now - msgDate) / 1000) <= intervalThreshold) {

return decrypted.substr(19);

}

}

要在PHP中执行加密和解密,

$encrypted = encryptWithTSValidation($recipent, $method, $key, $hmac);

$decrypted = decryptWithTSValidation($encrypted,$method,$key, $hmac, 60*60*12)//this is 12 hours

要生成HMAC,可以使用简单的MD5散列

$hmac = hash_hmac('md5', $recipent, $key);

在J.N.

var decrypted = decryptWithTSValidation(encString, method, secret, hmac, 60 * 60);

var encrypted = decryptWithTSValidation(string, method, secret, hmac);

注:

在nodejs和php中进行加密和解密时,请确保生成32位的唯一密钥。也要保证它的安全,不要将它存储在数据库中。

代码引用:

Encrypt string in PHP and decrypt in Node.js

凯撒密码是一种基于字母移位的代换密码,通过将字母移动一定的位数来进行加密解密。在Node.js,可以使用crypto模块的Cipher类和Decipher类来实现凯撒密码的加密解密操作。 首先,使用Cipher类进行加密操作。可以使用crypto模块的createCipher方法创建一个Cipher对象,并指定加密算法和密钥。然后,可以使用Cipher对象的update方法传入要加密的数据,并指定编码格式,最后使用final方法获取加密完成的数据。 例如,下面的代码演示了使用凯撒密码对明文进行加密: ``` const crypto = require('crypto'); function caesarCipherEncrypt(text, shift) { const cipher = crypto.createCipher('aes192', 'secret_key'); let encrypted = cipher.update(text, 'utf8', 'hex'); encrypted += cipher.final('hex'); return encrypted; } const plaintext = 'Hello, World!'; const shift = 3; const ciphertext = caesarCipherEncrypt(plaintext, shift); console.log(ciphertext); ``` 接下来,使用Decipher类进行解密操作。同样,可以使用crypto模块的createDecipher方法创建一个Decipher对象,并指定解密算法和密钥。然后,可以使用Decipher对象的update方法传入要解密的数据,并指定编码格式,最后使用final方法获取解密完成的数据。 例如,下面的代码演示了使用凯撒密码对密文进行解密: ``` function caesarCipherDecrypt(ciphertext, shift) { const decipher = crypto.createDecipher('aes192', 'secret_key'); let decrypted = decipher.update(ciphertext, 'hex', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; } const decryptedText = caesarCipherDecrypt(ciphertext, shift); console.log(decryptedText); ``` 请注意,这只是凯撒密码的一个简单示例,实际应用可能需要更复杂的加密算法和密钥管理方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值