Home > Web Front-end > JS Tutorial > body text

Simple encryption method for passwords

一个新手
Release: 2017-10-20 11:04:04
Original
2138 people have browsed it

General user passwords are also encrypted on the server and cannot be seen in plain text. If you want to remember to prevent users from logging in to the website again without entering their account and password, you can record it through cookies, sessionStorage, and localStorage.

Scenario: After the user successfully logs in once, he or she can log in again without losing the account number and password! Save it locally and don't want it to be displayed in plain text. It can only be encrypted and saved (general encryption is irreversible)

1. A simple encryption and decryption (there is no security if the code is completely exposed)

//加密
function encrypto( str, xor, hex ) {
    if ( typeof str !== 'string' || typeof xor !== 'number' || typeof hex !== 'number') {
      return;
    }
    let resultList = []; 
    hex = hex <= 25 ? hex : hex % 25;
    for ( let i=0; i<str.length; i++ ) {
      // 提取字符串每个字符的ascll码
      let charCode = str.charCodeAt(i);
      // 进行异或加密
      charCode = (charCode * 1) ^ xor;
      // 异或加密后的字符转成 hex 位数的字符串
      charCode = charCode.toString(hex);
      resultList.push(charCode);
    }
    let splitStr = String.fromCharCode(hex + 97);
    let resultStr = resultList.join( splitStr );
    return resultStr;
}
Copy after login
//解密
function decrypto( str, xor, hex ) { 
    if ( typeof str !== &#39;string&#39; || typeof xor !== &#39;number&#39; || typeof hex !== &#39;number&#39;) {
      return;
    }
    let strCharList = [];
    let resultList = []; 
    hex = hex <= 25 ? hex : hex % 25;
    // 解析出分割字符
    let splitStr = String.fromCharCode(hex + 97);
    // 分割出加密字符串的加密后的每个字符
    strCharList = str.split(splitStr);
    for ( let i=0; i<strCharList.length; i++ ) {
      // 将加密后的每个字符转成加密后的ascll码
      let charCode = parseInt(strCharList[i], hex);
      // 异或解密出原字符的ascll码
      charCode = (charCode * 1) ^ xor;
      let strChar = String.fromCharCode(charCode);
      resultList.push(strChar);
    }
    let resultStr = resultList.join(&#39;&#39;);
    return resultStr;
}
Copy after login

2. How to use

The above is the detailed content of Simple encryption method for passwords. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template