1,040
社区成员
发帖
与我相关
我的任务
分享这是我参加朝闻道知识分享大赛的第一篇文章
维吉尼亚密码是一种经典的加密技术,最早由法国外交官布莱斯·维吉尼亚(Blaise de Vigenère)于16世纪提出,尽管其概念可能源自更早的密码技术。它是基于多表代换的原理,通过一个可变长度的密钥对明文进行加密,增强了密码的复杂性和安全性。
维吉尼亚密码是在当时的密码学中引入了一种新的思维方式,取代了简单的替换密码。它利用字母表的循环性质,使得攻击者在进行频率分析时面临更大的挑战。这种加密方式在历史上曾广泛用于军事和外交通信,尽管随着计算机技术的发展,逐渐被更复杂的算法所取代。
用户可以输入明文(例如:woshidashuaige)和密钥(例如:nishuodedui),通过点击加密按钮生成密文(例如:jwkocrdwkoivow)。解密时,将密文放入输入框,保持密钥不变,点击解密即可恢复原明文。




维吉尼亚密码是一种经典的加密算法,以其多表代换的特性和抗频率分析能力而闻名。通过使用可变长度的密钥,它将明文转换为密文,并在解密时通过相同的密钥恢复原文。这种密码技术在16世纪由布莱斯·维吉尼亚提出,历史上被广泛用于军事和外交通信。尽管现代加密技术已更为复杂,维吉尼亚密码仍然是学习密码学的重要基础,帮助理解加密原理和信息安全的重要性。希望通过本篇文章,读者能够掌握其实现过程并激发对密码学的兴趣。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vigenere</title>
<link rel="stylesheet" href="Vigenere.css">
</head>
<body>
<div class="container">
<h1>维吉尼亚密码</h1>
<div class="input-group">
<textarea id="inputText" placeholder="输入要加密/解密的文本"></textarea>
<textarea id="outputText" placeholder="结果将显示在这里" readonly></textarea>
</div>
<div class="key-group">
<input type="text" id="key" placeholder="输入加密密钥">
<button onclick="encrypt()">加密</button>
<button onclick="decrypt()">解密</button>
</div>
</div>
<script src="Vigenere.js"></script>
</body>
</html>
body {
display: flex;
justify-content: center;
align-items: center;
height:100%;
background-color: #f5f5f5;
margin: 0;
}
.container {
margin-top: 150px;
max-width: 600px;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
.input-group {
margin-bottom: 20px;
}
textarea, input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
resize: vertical;
}
button {
width: 100%;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
margin-bottom: 10px;
}
button:hover {
background-color: #0056b3;
}
function encrypt() {
let inputText = document.getElementById('inputText').value;
let key = document.getElementById('key').value;
let outputText = '';
let j = 0;
for (let i = 0; i < inputText.length; i++) {
let Code = inputText.charCodeAt(i);
// 处理大写字母
if (Code >= 65 && Code <= 90) {
let keyCharCode = key.charCodeAt(j % key.length);
let encryptedCharCode = ((Code - 65) + (keyCharCode - 65)) % 26 + 65;
outputText += String.fromCharCode(encryptedCharCode);
j++;
}
// 处理小写字母
else if (Code >= 97 && Code <= 122) {
let keyCharCode = key.charCodeAt(j % key.length);
let encryptedCharCode = ((Code - 97) + (keyCharCode - 97)) % 26 + 97;
outputText += String.fromCharCode(encryptedCharCode);
j++;
}
// 处理非字母字符
else {
outputText += inputText.charAt(i);
}
}
document.getElementById('outputText').value = outputText;
}
function decrypt() {
let inputText = document.getElementById('inputText').value;
let key = document.getElementById('key').value;
let outputText = '';
let j = 0;
for (let i = 0; i < inputText.length; i++) {
let Code = inputText.charCodeAt(i);
// 处理大写字母
if (Code >= 65 && Code <= 90) {
let keyCharCode = key.charCodeAt(j % key.length);
let decryptedCharCode = ((Code - 65) - (keyCharCode - 65) + 26) % 26 + 65;
outputText += String.fromCharCode(decryptedCharCode);
j++;
}
// 处理小写字母
else if (Code >= 97 && Code <= 122) {
let keyCharCode = key.charCodeAt(j % key.length);
let decryptedCharCode = ((Code - 97) - (keyCharCode - 97) + 26) % 26 + 97;
outputText += String.fromCharCode(decryptedCharCode);
j++;
}
// 处理非字母字符
else {
outputText += inputText.charAt(i);
}
}
document.getElementById('outputText').value = outputText;
}