js aes加密demo-通义灵码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线字符串加密工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .input-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        textarea, input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #f9f9f9;
            border-left: 4px solid #4CAF50;
        }
        .error {
            color: red;
            margin-top: 10px;
        }
        .info {
            background-color: #e3f2fd;
            padding: 15px;
            border-radius: 5px;
            margin-top: 20px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>在线字符串加密工具-通义灵码生成</h1>
        <a href="https://www.sojson.com/encrypt.html">参考的工具网站</a>
        <div class="input-group">
            <label for="inputText">输入要加密的字符串:</label>
            <textarea id="inputText" placeholder="请输入要加密的字符串..."></textarea>
        </div>
        
        <div class="input-group">
            <label for="key">密钥 (长度任意):</label>
            <input type="text" id="key" placeholder="请输入密钥...">
        </div>
        
        <button onclick="encrypt()">加密</button>
        <button onclick="decrypt()">解密</button>
        <button onclick="clearAll()">清空</button>
        
        <div class="result">
            <label for="outputText">结果:</label>
            <textarea id="outputText" readonly></textarea>
        </div>
        
        <div id="error" class="error"></div>
        
        <div class="info">
            <h3>技术说明</h3>
            <p><strong>算法:</strong> AES-GCM (256位密钥)</p>
            <p><strong>密钥处理:</strong> 使用PBKDF2算法将任意长度的密钥转换为256位AES密钥</p>
            <p><strong>初始化向量:</strong> 每次加密都使用随机生成的12字节IV</p>
            <p><strong>输出格式:</strong> 十六进制字符串 (包含IV + 加密数据)</p>
        </div>
    </div>

    <script>
        // 使用AES算法进行加密
        async function encrypt() {
            const inputText = document.getElementById('inputText').value;
            const keyInput = document.getElementById('key').value;
            const errorDiv = document.getElementById('error');
            
            if (!inputText) {
                errorDiv.textContent = '请输入要加密的字符串';
                return;
            }
            
            if (!keyInput) {
                errorDiv.textContent = '请输入密钥';
                return;
            }
            
            errorDiv.textContent = '';
            
            try {
                const encrypted = await aesEncrypt(inputText, keyInput);
                document.getElementById('outputText').value = encrypted;
            } catch (e) {
                errorDiv.textContent = '加密失败: ' + e.message;
            }
        }
        
        // 使用AES算法进行解密
        async function decrypt() {
            const inputText = document.getElementById('inputText').value;
            const keyInput = document.getElementById('key').value;
            const errorDiv = document.getElementById('error');
            
            if (!inputText) {
                errorDiv.textContent = '请输入要解密的字符串';
                return;
            }
            
            if (!keyInput) {
                errorDiv.textContent = '请输入密钥';
                return;
            }
            
            errorDiv.textContent = '';
            
            try {
                const decrypted = await aesDecrypt(inputText, keyInput);
                document.getElementById('outputText').value = decrypted;
            } catch (e) {
                errorDiv.textContent = '解密失败: ' + e.message;
            }
        }
        
        // AES加密函数 - 使用PBKDF2处理任意长度密钥
        async function aesEncrypt(text, password) {
            // 将文本和密码转换为ArrayBuffer
            const encoder = new TextEncoder();
            const dataBuffer = encoder.encode(text);
            const passwordBuffer = encoder.encode(password);
            
            // 生成随机盐和初始化向量
            const salt = window.crypto.getRandomValues(new Uint8Array(16));
            const iv = window.crypto.getRandomValues(new Uint8Array(12));
            
            // 使用PBKDF2从密码派生密钥
            const keyMaterial = await window.crypto.subtle.importKey(
                'raw',
                passwordBuffer,
                { name: 'PBKDF2' },
                false,
                ['deriveKey']
            );
            
            // 派生256位AES密钥
            const cryptoKey = await window.crypto.subtle.deriveKey(
                {
                    name: 'PBKDF2',
                    salt: salt,
                    iterations: 100000, // 迭代次数,提高安全性
                    hash: 'SHA-256'
                },
                keyMaterial,
                { name: 'AES-GCM', length: 256 },
                false,
                ['encrypt']
            );
            
            // 执行AES-GCM加密
            const encryptedBuffer = await window.crypto.subtle.encrypt(
                { name: 'AES-GCM', iv: iv },
                cryptoKey,
                dataBuffer
            );
            
            // 组合盐、IV和加密数据
            const resultBuffer = new Uint8Array(
                salt.length + iv.length + encryptedBuffer.byteLength
            );
            resultBuffer.set(salt, 0);
            resultBuffer.set(iv, salt.length);
            resultBuffer.set(new Uint8Array(encryptedBuffer), salt.length + iv.length);
            
            // 转换为十六进制字符串
            return Array.from(resultBuffer)
                .map(b => b.toString(16).padStart(2, '0'))
                .join('');
        }
        
        // AES解密函数 - 使用PBKDF2处理任意长度密钥
        async function aesDecrypt(hexString, password) {
            try {
                // 将十六进制字符串转换为字节数组
                const bytes = [];
                for (let i = 0; i < hexString.length; i += 2) {
                    bytes.push(parseInt(hexString.substr(i, 2), 16));
                }
                const dataBuffer = new Uint8Array(bytes);
                
                // 分离盐、IV和加密数据
                const salt = dataBuffer.slice(0, 16);  // 16字节盐
                const iv = dataBuffer.slice(16, 28);   // 12字节IV
                const encryptedData = dataBuffer.slice(28); // 加密数据
                
                // 将密码转换为ArrayBuffer
                const encoder = new TextEncoder();
                const passwordBuffer = encoder.encode(password);
                
                // 使用PBKDF2从密码派生密钥
                const keyMaterial = await window.crypto.subtle.importKey(
                    'raw',
                    passwordBuffer,
                    { name: 'PBKDF2' },
                    false,
                    ['deriveKey']
                );
                
                // 派生相同的256位AES密钥
                const cryptoKey = await window.crypto.subtle.deriveKey(
                    {
                        name: 'PBKDF2',
                        salt: salt,
                        iterations: 100000, // 必须与加密时相同
                        hash: 'SHA-256'
                    },
                    keyMaterial,
                    { name: 'AES-GCM', length: 256 },
                    false,
                    ['decrypt']
                );
                
                // 执行AES-GCM解密
                const decryptedBuffer = await window.crypto.subtle.decrypt(
                    { name: 'AES-GCM', iv: iv },
                    cryptoKey,
                    encryptedData
                );
                
                // 将解密结果转换为文本
                const decoder = new TextDecoder();
                return decoder.decode(decryptedBuffer);
            } catch (e) {
                throw new Error('解密失败,可能是密钥错误或数据已损坏');
            }
        }
        
        // 清空所有输入
        function clearAll() {
            document.getElementById('inputText').value = '';
            document.getElementById('key').value = '';
            document.getElementById('outputText').value = '';
            document.getElementById('error').textContent = '';
        }
    </script>
</body>
</html>

 

posted on 2026-01-02 09:51  小沙盒工作室  阅读(0)  评论(0)    收藏  举报