四种古典密码的C++实现(2)-----Virginia密码

//Virginia密码
/*理解算法最重要,最好自己动手实现试试看,可以使用MFC写一个简单的交互界面*/
#include<iostream> 
#include<cstring> 

using namespace std; 
#define MINCHAR 32 
#define CHARSUM 94 
char table[CHARSUM][CHARSUM]; 
bool Init(); 
bool Encode(char* key, char* source, char* dest); 
bool Dncode(char* key, char* source, char* dest); 
int main() 
{ 
if(!Init()) 
{ 
cout << "初始化错误!" << endl; 
return 1; 
} 
char key[256]; 
char str1[256]; 
char str2[256]; 
int operation; 
while(1) 
{ 
do 
{ 
 cout << "请选择一个操作:1. 加密; 2. 解密; -1. 退出\n"; 
 cin >> operation; 
}while(operation != -1 && operation != 1 && operation != 2); 
if(operation == -1) 
return 0; 
else if(operation == 1)//加密 
{ 
 cout << "请输入密钥:"; 
 cin >> key; 
 cout << "请输入待加密字符串:"; 
 cin >> str1; 
 Encode(key, str1, str2); 
 cout << "加密后的字符串:" << str2 << endl; 
} 
else if(operation == 2)//解密 
{ 
 cout << "请输入密钥:"; 
 cin >> key; 
 cout << "请输入待解密字符串:"; 
 cin >> str1; 
 Dncode(key, str1, str2); 
 cout << "解密后的字符串:" << str2 << endl; 
} 
cout << endl; 
} 
return 0; 
} 
// 初始化维吉尼亚方阵 
bool Init() 
{ 
 int i, j; 
 for(i = 0; i < CHARSUM; i++) 
 { 
  for(j = 0; j < CHARSUM; j++) 
  { 
   table[i][j] = MINCHAR + (i + j) % CHARSUM; 
  } 
 } 
 return true; 
} 
// 加密 
// key:密钥 
// source:待加密的字符串 
// dest:经过加密后的字符串 
bool Encode(char* key, char* source, char* dest) 
{ 
 char* tempSource = source; 
 char* tempKey = key; 
 char* tempDest = dest; 
 do 
 { 
  *tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR]; 
  tempDest++; 
  if(!(*(++tempKey))) 
   tempKey = key; 
 }while(*tempSource++); 
 dest[strlen(source)] = 0; 
 return true; 
} 
// 解密 
// key:密钥 
// source:待解密的字符串 
// dest:经过解密后的字符串 
bool Dncode(char* key, char* source, char* dest) 
{ 
 char* tempSource = source; 
 char* tempKey = key; 
 char* tempDest = dest; 
 char offset; 
 do 
 { 
  offset = (*tempSource) - (*tempKey); 
  offset = offset >= 0 ? offset : offset + CHARSUM; 
  *tempDest = MINCHAR + offset; 
  tempDest++; 
  if(!(*(++tempKey))) 
   tempKey = key; 
 }while(*++tempSource); 
 dest[strlen(source)] = 0; 
 return true; 
}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是美国各地区 ZIP Code 的范围: 1. 东北地区(Connecticut, Maine, Massachusetts, New Hampshire, New Jersey, New York, Pennsylvania, Rhode Island, Vermont) - Connecticut: 06001-06928 - Maine: 03901-04992 - Massachusetts: 01001-05544 - New Hampshire: 03001-03897 - New Jersey: 07001-08989 - New York: 00501-14975 - Pennsylvania: 15001-19640 - Rhode Island: 02801-02940 - Vermont: 05001-05907 2. 中大西洋地区(Delaware, Maryland, District of Columbia, Virginia, West Virginia) - Delaware: 19701-19980 - Maryland: 20601-21930 - District of Columbia: 20001-20599 - Virginia: 20101-24658 - West Virginia: 24701-26886 3. 南部地区(Alabama, Arkansas, Florida, Georgia, Kentucky, Louisiana, Mississippi, North Carolina, South Carolina, Tennessee) - Alabama: 35004-36925 - Arkansas: 71601-72959 - Florida: 32004-34997 - Georgia: 30001-31999 - Kentucky: 40003-42788 - Louisiana: 70001-71497 - Mississippi: 38601-39776 - North Carolina: 27006-28909 - South Carolina: 29001-29948 - Tennessee: 37010-38589 4. 中西部地区(Illinois, Indiana, Iowa, Kansas, Michigan, Minnesota, Missouri, Nebraska, North Dakota, Ohio, South Dakota, Wisconsin) - Illinois: 60001-62999 - Indiana: 46001-47997 - Iowa: 50001-52809 - Kansas: 66002-67954 - Michigan: 48001-49971 - Minnesota: 55001-56763 - Missouri: 63001-65899 - Nebraska: 68001-68118 - North Dakota: 58001-58856 - Ohio: 43001-45999 - South Dakota: 57001-57799 - Wisconsin: 53001-54990 5. 西部地区(Alaska, Arizona, California, Colorado, Hawaii, Idaho, Montana, Nevada, New Mexico, Oklahoma, Oregon, Texas, Utah, Washington, Wyoming) - Alaska: 99501-99950 - Arizona: 85001-86556 - California: 90001-96162 - Colorado: 80001-81658 - Hawaii: 96701-96898 - Idaho: 83201-83876 - Montana: 59001-59937 - Nevada: 88901-89883 - New Mexico: 87001-88441 - Oklahoma: 73001-74966 - Oregon: 97001-97920 - Texas: 73301-79999 - Utah: 84001-84784 - Washington: 98001-99403 - Wyoming: 82001-83128 请注意,这些范围可能会随着时间的推移而有所变化,因此仅供参考。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值