<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
.special1
{
vertical-align:bottom;
}
</style>
</head>
<body>
<h3>凯撒加密</h3>
<table border-collapse:collapse>
<tr>
<td class="special1">请输入要加密的字符: </td>
<td><textarea name="text1" id="plainText" rows="6" cols="30" wrap="" ></textarea></td>
<td class="special1">加密后的字符为:</td>
<td><textarea name="text2" id="cipherText" rows="6" cols="30" wrap="" ></textarea></td>
</tr>
</table>
<table border-collapse:collapse>
<tr>
<td>usekey:
<select id="key">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
</select></td>
<td>
<p>
<input type="button" value="加密" οnclick="encrypt()">
<input type="button" value="置空" οnclick="clearFields()">
</p>
</td>
</tr>
</table>
<script>
function encrypt()
{
var plainText = document.getElementById("plainText").value;
var key = parseInt(document.getElementById("key").value);
var cipherText = "";
for (var i = 0; i < plainText.length; i++)
{
var char = plainText.charAt(i);
if (char.match(/[a-z]/i))
{
var code = plainText.charCodeAt(i);
if (code >= 65 && code <= 90)
{
char = String.fromCharCode(((code - 65 + key) % 26) + 65);
}
else if (code >= 97 && code <= 122)
{
char = String.fromCharCode(((code - 97 + key) % 26) + 97);
}
}
cipherText += char;
}
document.getElementById("cipherText").value = cipherText;
}
function clearFields()
{
document.getElementById("plainText").value = "";
document.getElementById("key").value = "";
document.getElementById("cipherText").value = "";
}
</script>
</body>
</html>