Files
tulin-code/对称加密-2026-08-21/1. js环境实现des加解密.js
Whale-Fall-Code 418a0411d1 Initialize encryption and JavaScript reverse-engineering study repository
Document the project structure and setup while excluding local dependencies and IDE settings.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-09 23:25:05 +08:00

70 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var crypro_js = require("crypto-js");
var source_data = "你好,世界!"
var des_key = "12345678"
var iv = "12345678";
var byte_iv = crypro_js.enc.Utf8.parse(iv);
var key = crypro_js.enc.Utf8.parse(des_key);
var data = crypro_js.enc.Utf8.parse(source_data);
function des_ecb_encrypt() {
// 进行加密
/*
* 加密操作
* des对称加密,需要三个形参
* 1. 明文字节
* 2. 密钥字节
* 3. 配置对象
* (1) mode 工作模式 ECB, CBC, CFB, OFB, CTR
* (2) padding 填充模式, Pkcs7,
*
**/
var encrypted = crypro_js.DES.encrypt(data, key, {
mode: crypro_js.mode.ECB, // 工作模式
padding: crypro_js.pad.Pkcs7 // 填充模式
})
return encrypted.toString();
}
function des_ecb_decrypt() {
var encrypt_data = des_ecb_encrypt();
var decrypted = crypro_js.DES.decrypt(encrypt_data, key, {
mode: crypro_js.mode.ECB, // 工作模式
padding: crypro_js.pad.Pkcs7 // 填充模式
})
return decrypted.toString(crypro_js.enc.Utf8);
}
// console.log(des_ecb_encrypt())
// console.log(des_ecb_decrypt())
function des_cbc_encrypt() {
var encrypted = crypro_js.DES.encrypt(data, key, {
iv: byte_iv,
mode: crypro_js.mode.CBC, // 工作模式
padding: crypro_js.pad.Pkcs7 // 填充模式
})
return encrypted.toString();
}
function des_cbc_decrypt() {
var encrypt_data = des_cbc_encrypt();
var decrypted = crypro_js.DES.decrypt(encrypt_data, key, {
iv: byte_iv,
mode: crypro_js.mode.CBC, // 工作模式
padding: crypro_js.pad.Pkcs7 // 填充模式
})
return decrypted.toString(crypro_js.enc.Utf8);
}
console.log(des_cbc_decrypt());