Skip to content

Instantly share code, notes, and snippets.

@lzghzr
Last active June 9, 2018 15:57
Show Gist options
  • Save lzghzr/7f873aab81f76594c74bf74ea9123633 to your computer and use it in GitHub Desktop.
Save lzghzr/7f873aab81f76594c74bf74ea9123633 to your computer and use it in GitHub Desktop.
ASF密码加密算法NodeJS实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ASFCryptoPW</title>
<script src="https://cdn.bootcss.com/crypto-js/3.1.9/crypto-js.min.js"></script>
</head>
<body>
<div>password:
<input id="password" type="text">
</div>
<div>cryptkey:
<input id="cryptkey" type="text">
</div>
<div>encrypt:
<input id="encrypt" type="text">
</div>
<input type="button" value="encrypt" onclick="encryptClick()">
<input type="button" value="decrypt" onclick="decryptClick()">
<script>
const inputPassword = document.querySelector('#password')
const inputCryptkey = document.querySelector('#cryptkey')
const inputEncrypt = document.querySelector('#encrypt')
function encryptClick() {
inputEncrypt.value = SymmetricEncrypt(inputPassword.value, inputCryptkey.value)
}
function decryptClick() {
inputPassword.value = SymmetricDecrypt(inputEncrypt.value, inputCryptkey.value)
}
/**
* Encrypt
*
* @param {string} input
* @param {string} key
* @returns {string}
*/
function SymmetricEncrypt(input, key) {
if (input === undefined) throw new TypeError('input')
if (key === undefined) throw new TypeError('key')
const parseKey = CryptoJS.SHA256(key)
const iv = CryptoJS.lib.WordArray.random(16)
const cryptedIV = CryptoJS.AES.encrypt(iv, parseKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding
})
const cryptedInput = CryptoJS.AES.encrypt(input, parseKey, { iv })
const cryptedIVnInput = CryptoJS.enc.Hex.parse(cryptedIV.ciphertext + cryptedInput.ciphertext)
return cryptedIVnInput.toString(CryptoJS.enc.Base64)
}
/**
* Decrypt
*
* @param {string} input
* @param {string} key
* @returns {string}
*/
function SymmetricDecrypt(input, key) {
if (input === undefined) throw new TypeError('input')
if (key === undefined) throw new TypeError('key')
const hexIVnInput = CryptoJS.enc.Base64.parse(input).toString()
const hexInput = hexIVnInput.slice(32)
const hexIV = hexIVnInput.slice(0, 32)
const cipherInput = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Hex.parse(hexInput) })
const parseKey = CryptoJS.SHA256(key)
const cipherIV = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Hex.parse(hexIV) })
const iv = CryptoJS.AES.decrypt(cipherIV, parseKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding
})
const decryptedInput = CryptoJS.AES.decrypt(cipherInput, parseKey, { iv })
return decryptedInput.toString(CryptoJS.enc.Utf8)
}
</script>
</body>
</html>
const crypto = require('crypto')
const password = 'password'
const cryptkey = 'cryptkey'
const encrypt = SymmetricEncrypt(password, cryptkey)
const decrypt = SymmetricDecrypt(encrypt, cryptkey)
console.log(encrypt)
console.log(decrypt)
/**
* 加密
*
* @param {string} input
* @param {string} key
* @returns {string}
*/
function SymmetricEncrypt(input, key) {
if (input === undefined) throw new TypeError('input')
if (key === undefined) throw new TypeError('key')
const bufferInput = Buffer.from(input)
const bufferKey = crypto.createHash('sha256').update(key).digest()
const bufferIV = crypto.randomBytes(16)
const aesIv = crypto.createCipheriv('aes-256-ecb', bufferKey, Buffer.alloc(0))
aesIv.setAutoPadding(false)
const cryptedIV = Buffer.concat([aesIv.update(bufferIV), aesIv.final()])
const aesInput = crypto.createCipheriv('aes-256-cbc', bufferKey, bufferIV)
const cryptedInput = Buffer.concat([aesInput.update(bufferInput), aesInput.final()])
const cryptedIVnInput = Buffer.concat([cryptedIV, cryptedInput])
return cryptedIVnInput.toString('base64')
}
/**
* 解密
*
* @param {string} input
* @param {string} key
* @returns {string}
*/
function SymmetricDecrypt(input, key) {
if (input === undefined) throw new TypeError('input')
if (key === undefined) throw new TypeError('key')
const bufferIVnInput = Buffer.from(input, 'base64')
const bufferInput = bufferIVnInput.slice(16)
const bufferKey = crypto.createHash('sha256').update(key).digest()
const bufferIV = bufferIVnInput.slice(0, 16)
const aesIv = crypto.createDecipheriv('aes-256-ecb', bufferKey, Buffer.alloc(0))
aesIv.setAutoPadding(false)
const decryptedIV = Buffer.concat([aesIv.update(bufferIV), aesIv.final()])
const aesInput = crypto.createDecipheriv('aes-256-cbc', bufferKey, decryptedIV)
const decryptedInput = Buffer.concat([aesInput.update(bufferInput), aesInput.final()])
return decryptedInput.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment