程序是一个 Base64 和 MD5 加密解密的程序。很简单,主要用到了hashlib和base64这两个库。而且,有这两个库,可以随时拓展更多种加密方式,像是 sha 啊什么的。要注意的就是下面几点:
1)input()和raw_input()的区别。
input()要求的输入的字符串要加双引号,数字的话,直接输入就可以了。
raw_input()函数将输入的所有数据都当做字符串来看待,即使输入的数字,也会变成是字符串的形式。而且,我们经常要在程序运行到某处的时候中断一下。那么,就可以用raw_input()这个函数来中断。输入回车就继续运行了。若是用input()中断的话,直接回车会爆错。。
2)Python 里面貌似没有主函数的说法
没有main(),没有入口点。那么怎么办呢?可以用下面的代码:
if __name__=="__main__":
Index(); //你的第一个函数
这样就可以让程序从第一个函数开始运行了。
3)千万注意 TAB 和空格。
也就是代码的格式。Python 是一个很注重格式的语言。因为没有大括号之类的,所以像是循环语句,判断语句的,一定要注意语句前面的 TAB。而且,千万不要 TAB 和空格混用!会报错.
import hashlib
import base64
def Index():
print("---------Joy_nick's Encrypt Tool---------");
print("Please choose one:");
print("1) MD5");
print("2) Base64");
while(1):
choose=input();
if(choose==1):
Page_MD5();
elif(choose==2):
Page_Base64();
def Page_MD5():
print("----------------MD5----------------");
print("Please choose one:");
print("1) MD5 Encrypt");
print("2) MD5 Check");
while(1):
choose=input();
if(choose==1):
Page_MD5_Encrypt();
elif(choose==2):
Page_MD5_Check();
def Page_Base64():
print("--------------Base64---------------");
print("Please choose one:");
print("1) Base64 Encrypt");
print("2) Base64 Decrypt");
while(1):
choose=input();
if(choose==1):
Page_Base64_Encrypt();
elif(choose==2):
Page_Base64_Decrypt();
def Input_From_File():
path=raw_input("Please input the file path : ");
file_handle=open(path,'r');
file_contant=file_handle.read();
file_handle.close();
return file_contant;
def Input_From_Stdin():
contant=raw_input("Please input : ");
return contant;
def Page_MD5_Encrypt():
print("-----------MD5 Encrypt------------");
print("Please choose one:");
print("1) Input from file");
print("2) Input");
while(1):
choose=input();
if(choose==1):
contant=Input_From_File();
break;
elif(choose==2):
contant=Input_From_Stdin();
break;
m=hashlib.md5()
m.update(contant);
print("Do you want to output to a file?(y or n)");
while(1):
choose=raw_input();
if(choose=='y'):
file_handle=open(m.hexdigest(),'w');
file_handle.write(m.hexdigest());
file_handle.close();
exit();
elif(choose=='n'):
print(m.hexdigest());
raw_input();
exit();
def Page_MD5_Check():
print("-----------MD5 Check------------");
print("Please choose one:");
print("1) Input from file");
print("2) Input");
while(1):
choose=input();
if(choose==1):
contant=Input_From_File();
break;
elif(choose==2):
contant=Input_From_Stdin();
break;
m=hashlib.md5()
m.update(contant);
MD5=raw_input("Please input the original MD5:");
if(MD5==m.hexdigest()):
print("It's a correct file.");
else:
print("The file is wrong.");
raw_input();
exit();
def Page_Base64_Encrypt():
print("-------------Base64 Encrypt----------------");
print("Please choose one:");
print("1) Input from a file");
print("2) Input");
while(1):
choose=input();
if(choose==1):
contant=Input_From_File();
break;
elif(choose==2):
contant=Input_From_Stdin();
break;
m=base64.b64encode(contant);
choose=raw_input("Do you want to save it to a file?(y or n)");
while(1):
if(choose=='y'):
file_handle=open("Base64_%c"%m[1],'w');
file_handle.write(m);
file_handle.close();
exit();
elif(choose=='n'):
print(m);
raw_input();
exit();
def Page_Base64_Decrypt():
print("--------------Base64 Decrypt------------------");
print("Please choose one:");
print("1) Input from a file");
print("2) Input");
while(1):
choose=input();
if(choose==1):
contant=Input_From_File();
break;
elif(choose==2):
contant=Input_From_Stdin();
break;
m=base64.b64decode(contant);
choose=raw_input("Do you want to save it to a file?(y or n)");
while(1):
if(choose=='y'):
file_handle=open("Base64_%c"%m[2],'w');
file_handle.write(m);
file_handle.close();
exit();
elif(choose=='n'):
print(m);
raw_input();
exit();
if __name__=="__main__":
print("main");
Index();