Last active
April 25, 2021 08:46
-
-
Save kaka2008/f1ce46575780221c4617 to your computer and use it in GitHub Desktop.
javascript des-ecb加密和解密
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 采用des-ecb的方式进行加密, | |
* @param text | |
*/ | |
function encryptText(text){ | |
var settings = { | |
des_ecb_key:"01234567", | |
des_ecb_iv:null | |
}; | |
var key = settings.des_ecb_key; | |
var iv = new Buffer(settings.des_ecb_iv ? settings.des_ecb_iv : 0) | |
var plaintext = text; | |
var alg = "des-ecb"; | |
var autoPad = true; | |
var crypto = require('crypto'); | |
//encrypt | |
var cipher = crypto.createCipheriv(alg, key, iv); | |
cipher.setAutoPadding(autoPad) //default true | |
var ciph = cipher.update(plaintext, 'utf8', 'hex'); | |
ciph += cipher.final('hex'); | |
return ciph; | |
} | |
/** | |
* 采用des-ecb的方式进行解密 | |
* @param text | |
*/ | |
function decryptText(text) { | |
try { | |
var settings = { | |
des_ecb_key:"01234567", | |
des_ecb_iv:null | |
}; | |
var key = settings.des_ecb_key; | |
var iv = new Buffer(settings.des_ecb_iv ? settings.des_ecb_iv : 0) | |
var alg = "des-ecb"; | |
var crypto = require('crypto'); | |
var decipher = crypto.createDecipheriv(alg, key, iv); | |
var txt = decipher.update(text, 'hex', 'utf8'); | |
txt += decipher.final('utf8'); | |
return txt; | |
} catch (e) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment