DES加密解密 与php后端加密解密互通

发表时间:2020-06-03 09:28:47
摘要信息

前端使用des或者aes加密,后端进行解密,或者相反

测试工具 apiPost 可以百度下载使用

DES加密解密

前端加密使用CryptoJS插件

function encrypt(str, key, iv) {
var encode_str = '';
var key = CryptoJS.MD5(key).toString();
var iv = CryptoJS.MD5(iv).toString();
var crypto_key = CryptoJS.enc.Utf8.parse(key);
var crypto_iv = CryptoJS.enc.Utf8.parse(iv.substr(0, 8));
if (typeof (word) == 'string') {
encode_str = CryptoJS.TripleDES.encrypt(str, crypto_key, {
iv: crypto_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
} else {
encode_str = CryptoJS.TripleDES.encrypt(JSON.stringify(str), crypto_key, {
iv: crypto_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
}
return encode_str.toString();
}

function decrypt(str, key, iv) {
var key = CryptoJS.MD5(key).toString();
var iv = CryptoJS.MD5(iv).toString();
var crypto_key = CryptoJS.enc.Utf8.parse(key);
var crypto_iv = CryptoJS.enc.Utf8.parse(iv.substr(0, 8));
var decrypt_str = CryptoJS.TripleDES.decrypt(str, crypto_key, {
iv: crypto_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decrypt_str.toString(CryptoJS.enc.Utf8);
}

前端加密解密示例

console.log(encrypt({data: 1,dasd:5}, '1234567887654321', '1234567887654321'))
console.log(decrypt('4IN6uvgqsIIgcjW0pZg2J8Ir3LLz1lBF','1234567887654321', '1234567887654321'))


密码与iv都是16位数的

后端php DES加密解密

<?php
namespace Des;


class Des
{

public static function decrypt($data, $key, $iv)
{
$data = base64_decode($data);
$key = md5($key);
$iv = substr(md5($iv), 0, 8); //取前8位

$decrypted = openssl_decrypt($data, 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}

public static function encrypt($str, $key, $iv)
{
$key = md5($key);
$iv = substr(md5($iv), 0, 8); //取前8位

$data = base64_encode(openssl_encrypt($str, 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, $iv));
return $data;
}

}

后端调用加密解密


接口测试

我们先用后端php加密字符串1,把加密的密文复制到js中看看是否可以解密?


结果是p0U/1VSUkLs=

我们将其复制粘贴到des加密解密的js中,看看


已复制好,注意密匙要与后端一致


成功解密 出来1

DES加密解密php  与js前端相同已经OK

AES加密解密

前端aes加密解密代码

function encrypt(data, key, iv) {
let encrypted = '';
let text = data;
text = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text)).toString();
key = CryptoJS.enc.Utf8.parse(key); /*为了避免补位,直接用16位的秘钥*/
iv = CryptoJS.enc.Utf8.parse(iv); /*16位初始向量*/
if (typeof (word) == 'string') {
encrypted = CryptoJS.AES.encrypt(text, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}).toString();
} else {
encrypted = CryptoJS.AES.encrypt(JSON.stringify(text), key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}).toString();
}
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encrypted)).toString();
}

function decrypt(data, key, iv) {
let encrypted = CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(data)).toString();
key = CryptoJS.enc.Utf8.parse(key); /*为了避免补位,直接用16位的秘钥*/
iv = CryptoJS.enc.Utf8.parse(iv); /*16位初始向量*/
let decrypted = CryptoJS.AES.decrypt(encrypted, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}).toString(CryptoJS.enc.Utf8);
return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(decrypted)).toString();
}

前端aes加密解密示例

console.log(encrypt({data: 2,json: 1},'1234567887654321','1234567887654321'))
console.log(decrypt('SlNnR0diZkxuUml1Uk5oQ0EwQUVYbVNTQmVSZ2dUUkhlTUd0VVlGWGlCeGE5SmxRdm5zblJDUkRiNVJYNkl4Mg==','1234567887654321','1234567887654321'))

结果


后端aes加密解密代码

<?php

namespace Aes;

class Aes
{

public function encrypt($data, $key, $iv)
{
$base64_str = base64_encode(json_encode($data));
$encrypted = openssl_encrypt($base64_str, "aes-128-cbc", $key, OPENSSL_ZERO_PADDING, $iv);
return base64_encode($encrypted);
}


public function decrypt($data, $key, $iv)
{
$encrypted = base64_decode($data);
$decrypted = openssl_decrypt($encrypted, 'aes-128-cbc', $key, OPENSSL_ZERO_PADDING, $iv);
return json_decode(base64_decode($decrypted), true);
}

}

调用示例

public function decryptAes(Request $request)
{
$aes = new Aes();
$post = $request->post();
$data = $aes->decrypt($post['data'], '1234567887654321', '1234567887654321');
return callJson::back(200, 'aes解密结果', $data);
}

public function encryptAes(Request $request)
{
$aes = new Aes();
$post = $request->post();
$str = $aes->encrypt(['username' => 'admin', 'pwd' => 'admin'], '1234567887654321', '1234567887654321');
return callJson::back(200, 'aes加密结果', $str);
}

结果



同des一样,我们将后端接口加密的密文复制到js中看看是否可以解密?

console.log(decrypt('SlNnR0diZkxuUml1Uk5oQ0EwQUVYbVNTQmVSZ2dUUkhlTUd0VVlGWGlCeGE5SmxRdm5zblJDUkRiNVJYNkl4Mg==','1234567887654321','1234567887654321'))

查看结果


解密是相通的至此,des与aes加密解密前端后端的统一就都OK了,本篇文章中所有的密匙都是一样的,位数是做过处理的,比如des加密,会将密匙 md5,然后截取前8位,这样aes与des可以用同一对密码和iv,如果项目中有需要用到aes和des加密的,可以看看这篇文章参考参考。

40% Complete (success)