好吧,我有作业。已经很晚了,我的大脑功能不好。 我有以下函数使我的最终结果混乱(但我必须使用它们):
def letterToIndex(char):
az = "abcdefghijklmnopqrstuvwxyz "
idx = az.find(char)
if idx < 0:
print ("err: not in here ", char)
return idx
def indexToLetter(idx):
az = "abcdefghijklmnopqrstuvwxyz "
if idx > 25:
print ("err: ", idx, " too large")
letter = ''
elif idx < 0:
print "err: ", idx , "should not be this"
else:
return letter
另外,这个,显然是造成问题的那个(我还不清楚如何扭转那个模式26):
def vigneIndex(keylet, plainlet):
keyIdx = letterToIndex(keylet)
plainIdx = letterToIndex(plainlet)
newIdx = (plainIdx + keyIdx) % 26
return indexToLetter(newIdx)
最后这些是我的加密器/解密器:
def encryptVigne(key,string):
cipher = ''
keyLen = len(key)
for i in range(len(string)):
char = string[i]
if char == ' ':
cipher = cipher + char
else:
cipher = cipher + vigneIndex(key[i%keyLen], char)
return cipher
加密器可以工作。
nkey = 'abc'
print nkey
print encryptVigne(nkey, 'testing')
cip = encryptVigne(nkey, 'testing')
def undoVigne(key,cipher):
string = ''
keyLen = len(key)
for i in range(len(cipher)):
char = cipher[i]
if char == ' ':
string = string + char
else:
string = string + vigneIndex(key[i%keyLen], char)
return string
但是解密并没有解密它。我被告知它就像转换过程一样简单,但很明显我错过了图片中的内容。
答案 0 :(得分:0)
我没有对它进行测试,但从飞行视图来看,您的函数indexToLetter
似乎无法正常工作。尝试通过这个改变它:
def indexToLetter(idx):
az = "abcdefghijklmnopqrstuvwxyz"
if idx > 25:
print("err: ", idx, " too large")
return ''
elif idx < 0:
print("err: ", idx , "should not be this")
return ''
else:
return az[idx]
但如果idx
错误,您应该引发异常,而不是将消息打印到输出流。
答案 1 :(得分:0)
实际上有解密的代码吗?
例如,当反转过程(解密)时,在函数newIdx = (plainIdx - keyIdx) % 26
中,你不应该服用:
newIdx = (plainIdx + keyIdx) % 26
转换加密时的操作:
<script type="text/javascript">
$(document).ready(function(){
$("#login").click(function(){
var user = $('#userID').val();
var pass = $('#password').val();
if (user === '' || pass === ''){
alert("Please Fill Required Fields");
}
else {
alert (user+" " +pass);
var x="<?php confirm();?>";
alert (x);
window.location.href = 'http://localhost/Annapoorna/Welcome_Page.php';
}
});
});
</script>