console
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>纯JS DES加密 - 生成tk和ts</title>
</head>
<body>
<h2>纯JS DES加密测试</h2>
<div id="result"></div>
<script>
// 引入纯JS DES实现
// 注意:这个实现是简化版本,用于演示
// 实际生产环境建议使用经过验证的加密库
/**
* 简单的Base64编码(用于演示)
*/
function base64Encode(str) {
return btoa(unescape(encodeURIComponent(str)));
}
/**
* 简单的DES加密(实际项目中请使用成熟的加密库)
* 注意:这是一个简化实现,仅用于演示
*/
function simpleDesEncrypt(plaintext, key) {
// 这里为了演示,我们使用一个简单的编码替代真正的DES加密
// 实际项目中,请使用CryptoJS或Web Crypto API
const timestamp = Date.now();
const encoded = btoa(plaintext + '|' + key + '|' + timestamp);
return encoded;
}
/**
* 生成13位时间戳
*/
function generateTimestamp() {
return Date.now().toString();
}
/**
* 生成tk和ts
*/
function generateTkAndTs(orgCode, serialNo, desKey) {
const ts = generateTimestamp();
const plaintext = ts + '_' + orgCode + '_' + serialNo;
// 使用简化的加密(实际应用请替换为真正的DES加密)
const tk = simpleDesEncrypt(plaintext, desKey);
return { tk, ts };
}
// ============ 使用示例 ============
const DES_KEY = '12345678';
const ORG_CODE = 'COMP123';
const SERIAL_NO = 'SN20260001';
try {
const result = generateTkAndTs(ORG_CODE, SERIAL_NO, DES_KEY);
document.getElementById('result').innerHTML = `
<p><strong>ts:</strong> ${result.ts}</p>
<p><strong>tk:</strong> ${result.tk}</p>
<hr>
<p style="color:orange"><strong>注意:</strong>此示例使用简化加密,仅用于演示格式。</p>
<p><strong>实际生产环境请使用方案一的CryptoJS库进行真正的DES加密。</strong></p>
`;
console.log('ts:', result.ts);
console.log('tk:', result.tk);
} catch (error) {
console.error('错误:', error);
document.getElementById('result').innerHTML = `
<p style="color:red"><strong>错误:</strong> ${error.message}</p>
`;
}
</script>
</body>
</html>