简单的加密与解密的实现---仿射密码
2016-03-06 15:47
627 查看
#include <iostream> #include <string.h> using namespace std; int main() { void encrytion_decrypt(char input[],int len); char input[10]; cout<<"Please input password:"; cin>>input; int len; len=strlen(input); cout<<"\n"<<"password is:"<<input<<endl; encrytion_decrypt(input,len); return 0; } void encrytion_decrypt(char input[],int len) //加密_解密函数 { // 仿射加密公式 : f(x)=3x+2; int i; char string[10]; for(i=0;i<len;i++) { int a; a=(input[i]-97)*3+2; //加密 if(a>25) //对照为0-25 mod为26 { do //保证不超出域 { a=a-26; }while(a>25); a=a+97; //加密后得到的ASC码; string[i]=(char)a; } else { a=a+97; string[i]=(char)a; } } cout<<"encrytion password is:"; for(i=0;i<len;i++) { printf("%c",string[i]); } cout<<endl; char res[10]; for(i=0;i<len;i++) //解密过程 { int b; b=(string[i]-97)-2; /* if(b==0) { res[i]=(char)(b+97); continue; } */ while(b%3!=0) { b=b+26; } b=b/3; res[i]=(char)(b+97); } cout<<"keyword is:"; for(i=0;i<len;i++) printf("%c",res[i]); cout<<endl; }
本文出自 “启思・朝圣者” 博客,谢绝转载!
相关文章推荐
- HDOJ1016(搜索DFS)
- 《软件工程》第一周学习进度
- js获取字符串字节数方法小结
- 几种经典的Hash算法的实现(源代码)
- location 学习与总结
- Multi-digits Recognition Using ConVNet on Mobile categories:
- Android常用系统广播
- Hadoop作业提交与停止命令
- java高级---线程、网络、文件、流、序列化等代码示例
- 分类器性能评估
- 软件测试第一次作业——描述曾经遇到的bug
- 第一周学习进度条
- php基础复习(3)文件上传于下载
- 随机生成四则运算(代码)
- Java架构
- OpenCV典型程序结构
- 剑指offer——快速排序
- 剑指offer——快速排序
- 由浅入深学习MySQL
- 2015蓝桥杯 曼哈顿距离