这是一个从文件中解密某些文本的程序。一个按钮允许用户从打开的对话框中选择一个文件。选择后,内容将显示在输入文本区域中。
请忽略下面的模块“readByLine”和“readKeyword”,因为存在一些问题。
不确定 HTML5 是否支持 Chrome 20.0.1132.47 m 和 IE7。
附上代码如下:
<html>
<head>
<script type="text/javascript">
//Select a Cipher file from Open Dialog Box
function readCipherFile()
{
if (!window.FileReader)
document.getElementById("cipher").innerHTML = "<p>This browser doesn\'t support File API</p>";
else
{
// Input handler for cipher text
document.getElementById("cipherfile").onchange = function() {
readFileAsText(this.files[0]);
};
}
}
//Read content as text and display in Input textarea
function readFileAsText(file)
{
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event) {
document.getElementById("cipher").innerHTML = event.target.result;
};
reader.onerror = function() {
document.getElementById("cipher").innerHTML = 'Unable to read ' + file.fileName;
};
}
//Select a Keyword file from Open Dialog Box
function readKeyword()
{
if (!window.FileReader)
document.getElementById("keyword").innerHTML = "<p>This browser doesn\'t support File API</p>";
else
{
// Input handler for keyword
document.getElementById("keyfile").onchange = function() {
readByLine(this.files[0]);
};
}
}
//Read and display one keyword at a time
function readByLine(file)
{
var reader = new FileReader();
var arr = "";
console.log("test" + arr);
console.log(file);
reader.onload = function(event) {
arr = event.target.result;
};
reader.readAsText(file);
for(i=0; i<arr.length; i++)
{
arr = arr.split("\n");
document.getElementById("keyword").value = arr;
}
//display the next keyword when button is clicked
var keyButton = document.getElementById("keyfile");
keyButton.addEventListener("click", readByLine, false);
}
</script>
</head>
<body>
<center>
<h2>Vigenere Cipher</h2>
<br>
<form name="form1">
<table>
<tr>
<td>
Input:
</td>
<td>
<textarea name=input id="cipher" rows=10 cols=60 wrap=virtual></textarea>
</td>
</tr>
<tr>
<td></td>
<td align="center">
<input type=file id="cipherfile" onClick="readCipherFile(this)" />
</td>
</tr>
<tr height="20px"></tr>
<tr>
<td>
Key:
</td>
<td>
<textarea name=key id="keyword" rows=2 cols=60 wrap=virtual></textarea>
</td>
<tr>
<td></td>
<td align="center">
<input type=file id="keyfile" onClick="readKeyword()" />
</td>
</tr>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<input type=button value="Decrypt" onClick="decode()" style="margin-left: 135px">
</td>
</tr>
<td>
Output:
</td>
<td>
<textarea id="output" rows=10 cols=60 wrap=virtual></textarea>
</td>
</table>
</form>
</center>
</body>
<html>