Home > Article > Backend Development > javascript - Some issues related to aes128 encryption
class aes {
<code>const KEY = "625202f9149e061d"; const IV = "5efd3f6060e20330"; /**</code>
pkcs7 complement code
@param string $string plain text
@param int $blocksize Blocksize , in byte
@return String
*/
function addPkcs7Padding($string, $blocksize = 32) {
<code> $len = strlen($string); //取得字符串长度 $pad = $blocksize - ($len % $blocksize); //取得补码的长度 $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段 return $string;</code>
}
aes128cbcEncrypt($str, $iv = self::IV, $key = self::KEY) { // $this->addPkcs7Padding($str,16)
<code> $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv)); return $this->strToHex($base);</code>
}
strToHex($string) {//Convert string to hexadecimal
<code> $hex = ""; $tmp = ""; for ($i = 0; $i < strlen($string); $i++) { $tmp = dechex(ord($string[$i])); $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp; } $hex = strtoupper($hex); return $hex;</code>
}
Aes encryption codes found online include PHP and android. Can a kind person tell me about each step of this encryption process? $aes = new aes(); $aes->aes128cbcEncrypt('token'); or help. Write a corresponding js version = =
class aes {
<code>const KEY = "625202f9149e061d"; const IV = "5efd3f6060e20330"; /**</code>
pkcs7 complement code
@param string $string plain text
@param int $blocksize Blocksize , in byte
@return String
*/
function addPkcs7Padding($string, $blocksize = 32) {
<code> $len = strlen($string); //取得字符串长度 $pad = $blocksize - ($len % $blocksize); //取得补码的长度 $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段 return $string;</code>
}
aes128cbcEncrypt($str, $iv = self::IV, $key = self::KEY) { // $this->addPkcs7Padding($str,16)
<code> $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv)); return $this->strToHex($base);</code>
}
strToHex($string) {//Convert string to hexadecimal
<code> $hex = ""; $tmp = ""; for ($i = 0; $i < strlen($string); $i++) { $tmp = dechex(ord($string[$i])); $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp; } $hex = strtoupper($hex); return $hex;</code>
}
Aes encryption codes found online include PHP and android. Can a kind person tell me about each step of this encryption process? $aes = new aes(); $aes->aes128cbcEncrypt('token'); or help. Write a corresponding js version = =
It sounds quite complicated, just take a look at this link.
http://yinghuayuan8866.blog.163.com/blog/static/2245702720121225658625/