密码学实验二:

  

/**

                                                                       :;LaEaHKEEGpPXU7;,
.:75pKH11252U252XapZgRQgD6XJscLr;,.
:LXpRgGaX521JLw1JswJJsJs22XHPPEZEGDOMMRDOa7.
.r2EDDZEpZPZP6KpHX5SXH5XXa5KwaXaSX5UJ1c77sLs2GMQQ6r .
,LpgOGpEZGZEZEpZKpHHU5wP5HEDgpXpHa2SSa5aSXULr7rrirrJXRBp; ;B
,J6MRZH6EgEEZE6E6EZZPZXXwSSGQXr::aPpP5USUHaHaKa5Lvrr7ri;rLHBB2: Kc
rpQDOpPPOGGZOGOZG6GEOEOEDPPGBa. .PaSSUXSUUUaUSaKXKS177r7rrrirSBBR7 .O,
:UBQOKPK6ZOOOEDEO6GZE6EpEpDgDBR: UBpXHa5aSaUS5SUS5XapPHJc7rrv7rr7sgBBs .g.
;gBMPXpO6GEOEOEOEGEOEE6EZEEDRGBB EB5pKSXpHKaHSX552S5aUHHEX17c7vr7777s5RBS: .R;
.sQBPXpDZOODOgODGOGgEDEOEOGgGgOOMB: LBKKSXSHa52aaKXHXKaa5aSaaHXSJLcL7vcc777JDBBg2;. Qi
;2ggp2EDDOggGEDGgDDOgGGZDOOZOGg6gEBX vBZaHUKaaUXXXSXXKXpXHXH5wwaa52U1wssLsLJccv1UDQBQ67. O7
:ZZUU5PROOEOZOZGGOODZOGgODZOOgOggRgRB; ;:..R6XaKKpP6PGppKPHpHpPX5aU21UUa5Sw52UwUJJv77L77sSpQMDU; ;B5
,SRJ7sSHGggEOZOEG6OODpOZgggOQQBBBBBQBBQ.,;;. LBOgOgDRDDZODMgQRgRDaa552a252UUa25w5UaU2sLvccs7r7sJZBBMr ,XQJ:
LQHr77J6RGOZDZOEGEDGgDRORQBBBQRDPU1Jscwa7.,::.:J7r;::::. ..,:;i7UOgRRRgDPH5SUSU52U2HHa1JJJLJLccLr71RBB, 7R2,
:RZv;77JSgGOZEODEDGOEggQBBBMS7;:,:,,.,.,.:7L;:,;.: ,: . ..... .:;rJU6GgGRggEZHPaKXX2S221Js1Lc7r:7QB. .XX:
7g1;;7rcXG6gpGDZGgZOOQBBQpr. ::::;:;:::Jr::sr;::;:.:vs,:::::::::::,, ,:7L5HGOggRgZUUU5wSUaJLc7r7BOiDr ...
.XX;;;irLHGKpZZZEKgDRBBB6i ,;;:;;;;r;;:s177:,;L7:;7:.rHi,:::::::::::::::::,. .,;7ZRQgO6KUUJsLwsJ7KBM. .....
JZ:;rrrc5EPHp6XgpRBBBE; :i:;;;;;:;;;:c7::r7;,::::::::rv:,:::::::::::,:,:,:,,,, .;sORQRGX21wsXU: .... .
.Br;iir72EHPHZ6EgBBR7. ..;ii:;;;;;;;;;:71r::.:7, .::7;:;H;.::,:j::::::,:::::,,,::,,, ,7wEDRZBMr .......
1D;:r;rwOPXPKH6BBX, .:;:;;;:::;:;;;;;::Ls,;ss..r. ,c77;sLU,:,:::,:::,:,:,:,,,:,,,:::. .:.rP:.......
D2:i;rJpKHXHXgg7 .,,::::;:::::;:;:;;:;SL7sS2, :. ::::,:U7.:::,:::,:,,,,,:,,.,,,.,:, ;L:. .. ...
:Qc;i7LGZPa6gBM, ...,,::::::::::::;;::;.JJ;ic: ;:::,v1,.:::;7,,,:,:,:,,,,.:,,,,.. :2wr. .......
sRr:rrwZGgBQR7. .,,::::::::::::::;::;:::Hr:7i ,;;:::U: .,,,:r.,,:,:,:,,,:,,,..... 7K2:. ..........
OX:irsXgQZ:. .,,:::,,:::,::::::::;;;::r5;r7: :;;;:7L ..,.,;:,:,:,,,:,:,,,,...... .rU6w;..............
.BJ71EK5;. .,,::,:,:,,,:,:,::::::::;:;.Ls;r7 :;:::s, ..,,,r:.:.,,,.:, .,..... . .;s5XJr,..,.............
1Mv::. .:,:,,,,,,,:::::::,;:::;:;::..J7;c: ,. ,rri:27 , .,:;. ,:,:,,:..,.... .rpPL;:.. ... .............
..7Ls: .,,,:,,,:,:,:::,:,:::::,:::::: .Srrr, .,:;;;::::..:7r;r1 .: :E:..,,:,...,,.., ..XBQ7, ................... ..
,;7v7r7:, ,,,,:.,,,,,,:,,::,:,,.::.,:.,:, :Jrr; ,r7,:.. ,:::L: . .7RJ .,.:.,......,,:MBs ..............,........
.:;vJs7i, ,,,., ..,,:,,,:,,,:,:.,rs,,.:,, ;J;c, .,::;; Lr.E: .,...,....:,:1Z: .........,.,...........:,.
.;,,:r7J1wv;,. ....,.:. .,.,,:,,,:,,::::,sS;.:::, cLrr. . .,. .,..: ;; r5 ........,,:;s7. ....,.,.,...........,.,.,,.
,BBs::: . ........,.,,,,,.:,,,:,:,,.;s7r,,,,, cc7; ,.,,. .:7rrJGMPOEL1, ..... ,,:rSr, : ,.,.,.,.,.........,,,,,. ..
rZL. ....... ..,,,.,,,..,,,.,;,...7J:s: .. .wr7, ... .rJpQBBBBBBBQgKP77s .. .,7S2,,....,....,.,.,......,,.,. .:cX2
;SH7, . . . .. .,.,.,.,,,...,..r, ::Ur;7L . .57;. .rPBBQBBBPws:;r::::.,:P. .;S5;,..,...,.,.,.,.... ..,,, ..,7HSJvr
r7r;: . ... ..........,...,.. 7L;,rS;;ivr . 1r: . .rZBBK7;.JL,::Jrs;.:;,:;J, .,;LDv..:.,...,.,.,.,...,.,,:,, ,7sKGwc77;
.:7L7:, . ,r,..,.,,,...,..:iLL 7s;r;cv: U7. :vi;:. .. :Er:::.Ls. . ,7::::vHEi ,:,,.,,,.,.,,,,,,::::, .:1QBKJJUsssc
.:rvw1JsL7;, ,;..........,.:ir :r .ELr77v:,. Pr .. . Ls ,w. rr.r:JPr. ::,..,,,.,,,,:,:::,:,..7XgRE52US25w1c
.:;7JRQpX: ; .........,i: .,J ;gri;7r : 1; .X: . ,v :aK1, ,,:.,,,.,,,,:::::,;::::cOBB6K55UXUX5XUw
;J,XQB7,: . ...,: .:7Z.7Zi7;r: , v, :w:;,.,:::7LUsc:..:,:.:,,,,,:,:::::::,:;c5BBPS5wSUSUaUaUa2
J7 rEBBg..,. ..... . ,L1PLKr:,:;r;;::, :, :;SXsJU1XLLr....:,,,,,,::::,::::::,::LL,,rZXUJU25552aUUUU
JL .:sXBB:... ., . ,OH777;,rZRBBBQL ..r, ,:r7::r:,;;....:,,,,,,,,.::::;::;:::.7gL UK2UUS1aw2wU25J
U7 .r71BB; . :.. .. :QJ;.;1pBBBBBGRBRi i: . .w7. ..,,:,,,,,:,:,:::::::,.,2BR: .DSaUSUUS525w5U1
2r :7rJBQ7 .;. . KL::JGBBBgE6Hp6XMQ; . ... ,2s ,,,,:::::,:::::::,,.:sQQB2 ;DHa5U52S2U25USJ
a: :7;1BB2 .v, sL;LPQgDBPH6KPQBpGBG . ... .. aP:. .. ,::,:::::,,,,.;rSgBQDa; :gSSwSUUU525US2U
5; ;;r2QBB, .s: :a7:PBZ2,:BEaZPKgBZOgB, . ...:LEHri::,:.,,.,,,::rsXRBgGKEJi, 2GH252SUS5S11wH5
U: ;:rwDBBr .c, rB6r 167.,,RQPpEP6OpKEBc ., . : ::sS7;cKB6HHa1XOQRRQBRgEZDBH; .. ;RZUPSSU525USSpK2
s; ;::aBg, .; .Gv,H::,,::,;BQDOGHPXpKBs .,. . .,. .r:;:6gOEBQRRRDggQgOHgMGL, .;PEHHXaaKaPXPKPaXS
s7.:,sBa :..S, vE::::; 7BBMgZH6pQBr . ,:, ,.,r2RHSSpMZPKRRZpgggav, ;UpEZaaaKHHUHPPaUJHGO
a;.775: .::rU. gR:,:. :;:BBBBQar . ,, ,.,7rggJwU6DDGMgOGgXc:, ,rPGOpKSXSXSKUKHU1UHgOEP
1i :SZJrLXpBBMRB: .v Ba,. :sv: . irsBUSpEEGPPpg65r:..,;sKZDZHSX5XaHU55K2wUGMRZ6Z6
:K :7asvwc2MgEQB, :gg.:iB7 .,,,:. ...,. . .. .:iM6GEpSSXZOEs:...;spZpKKXXSX5Xa5Ja5wwKGQDEpp6OZ
w; ,LXLr77sLwL5aR. gRgQgaQBi ,vJwvi,. .;;:;7: . . ,... rRDRJUSXpgas:,.:;U66SaHa2X5aUa552X12SgQgpZpEpGOD
H: ;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sr:,. JgGgBQDOSJss77r
:r;::: , PH r, ,;5: ,::::;7Ls7Lc7;: ,:7JP17rJUs
.:J: :; .,:K6BQS7:.,.,.
:r7Ji;r7;::;;.
. **/ #include<bits/stdc++.h>
using namespace std; #define FFI(a, b) for(int i = a; i < b; i++)
#define FFJ(a, b) for(int j = a; j < b; j++)
#define FFK(a, b) for(int k = a; k < b; k++)
#define RR(a, b) for(int i = a; i > b; i++)
#define ME(a, b) memset(a, b, sizeof(a))
#define SC(x) scanf("%d", &x)
#define SCC(a, b) scanf("%d%d", &a, &b)
#define SCCC(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define PR(x) printf("%d\n", x)
#define PRR(a, b) printf("%d%d\n", a, b);
#define INF 0x3f3f3f3f
#define MAX 50
#define MOD 1000000007
#define E 2.71828182845
#define M 8
#define N 6
typedef long long LL;
const double PI = acos(-1.0);
/*******************************************************************************************************************************************************/
int k1 = 0;int k2 = 0;
static int P10MAX = 10;
static int P8MAX = 10;
static int P4MAX = 4;
static int IPMAX = 8;
static int IPIMAX = 8;
static int EPMAX = 4;
static int P10[] = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6, '\0'};
static int P8[] = {6, 3, 7, 4, 8, 5, 10, 9, '\0'};
static int P4[] = {2, 4, 3, 1, '\0'};
static int IP[] = {2, 6, 3, 1, 4, 8, 5, 7, '\0'};
static int IPI[] = {4, 1, 3, 5, 7, 2, 8, 6, '\0'};
static int EP[] = {4, 1, 2, 3, 2, 3, 4, 1, '\0'};
static int S0[4][4] = {
{1, 0, 3, 2},
{3, 2, 1, 0},
{0, 2, 1, 3},
{3, 1, 3, 2},
};
static int S1[4][4] = {
{0, 1, 2, 3},
{2, 0, 1, 3},
{3, 0, 1, 0},
{2, 1, 0, 3},
};
//将二进制变成数字
int BinaryToDecimal(string binary){
int result = 0;
FFI(0, binary.length())result = 2 * result + (binary[i] - '0');
return result;
}
//数组的长度
int Getlength(int p[]){
int i = 0;
for(; p[i] != '\0'; i++);
return i;
}
//置换函数
int Permute(int inum, int p[], int pmax){
int result = 0, length = Getlength(p);
FFI(0, length){
result <<= 1;
result |= (inum >> (pmax - p[i])) & 1;
}
return result;
}
void SDES(int k){
int t1 ,t2;
t1 = t2 = 0;
k = Permute(k, P10, P10MAX);
//保留最低5位
t1 = (k >> 5) & 0x1f;
t2 = k & 0x1f;
t1 = ((t1 & 0xf) << 1) | ((t1 & 0x10) >> 4);
t2 = ((t2 & 0xf) << 1) | ((t2 & 0x10) >> 4);
k1 = Permute((t1 << 5) | t2, P8, P8MAX); t1 = ((t1 & 0x07) << 2) | ((t1 & 0x18) >> 3);
t2 = ((t2 & 0x07) << 2) | ((t2 & 0x18) >> 3);
k2 = Permute((t1 << 5) | t2, P8, P8MAX);
cout<<"K1: "<<endl;
cout<<"K2: "<<endl;
}
//fk里面的F函数
int F(int R, int K){
int t = Permute(R, EP, EPMAX) ^ K;
int t0 = (t >> 4) & 0xf;
int t1 = t & 0xf;
t0 = S0[((t0 & 0x8) >> 2) | (t0 & 1)][(t0 >> 1 & 0x3)];
t1 = S1[((t1 & 0x8) >> 2) | (t1 & 1)][(t1 >> 1 & 0x3)];
t = Permute((t0 << 2) | t1, P4, P4MAX);
return t;
} //最低4位
int fk(int m, int k){
int l = (m >> 4) & 0xf;
int r = m & 0xf;
return ((l ^ F(r, k)) << 4) | r;
}
int SW(int x){
return ((x & 0xf) << 4) | ((x >> 4) & 0xf);
}
//加密
int encrypt(int m){
m = Permute(m, IP, IPMAX);
m = fk(m, k1);
m = SW(m);
m = fk(m, k2);
m = Permute(m, IPI, IPIMAX);
return m;
}
//解密
int decrypt(int m){
m = Permute(m, IP, IPMAX);
m = fk(m, k2);
m = SW(m);
m = fk(m, k1);
m = Permute(m, IPI, IPIMAX);
return m;
} void printBin(int x, int n){
int mask = 1 << (n - 1);
while(mask > 0){
((x & mask) == 0) ? printf("0") : printf("1");
mask >>= 1;
}
cout<<endl;
}
int main(void){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int index;string plaintext, ciphertext, key;
printf("**************************************************************************\n");
printf("*****************!!!欢迎使用S-DES加载助手!!!************************\n");
printf("*****************作者:Empirefree *********************\n");
printf("*****************工具: DEV-C++ *********************\n");
printf("*****************时间: 2018/10/29 9:08 *********************\n");
printf("**************************************************************************\n");
cout<<"*************** 输入均为二进制 ***************"<<endl;
loop: cout<<"***************请选择:加密为1 解密为0***************"<<endl;
cin>>index;
if(!index){
cout<<"***************解密:***************"<<endl;
cout<<"***************请输入密文:***************"<<endl;
cin>>plaintext;
cout<<"***************请输入密钥:***************"<<endl;
cin>>key;
SDES(BinaryToDecimal(key));
cout<<"***************得到明文:***************"<<endl;
printBin(encrypt(BinaryToDecimal(plaintext)), 8);
cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
cin>>index;
switch(index){
case 0:{
cout<<"***************欢迎使用s-des程序***************"<<endl;
cout<<"***************hyq ***************";
return 0;
}
case 1: goto loop;
}
}
else{
cout<<"***************加密:***************"<<endl;
cout<<"***************请输入明文:***************"<<endl;
cin>>ciphertext;
cout<<"***************请输入密钥:***************"<<endl;
cin>>key;
SDES(BinaryToDecimal(key));
cout<<"***************得到密文:***************"<<endl;
printBin(decrypt(BinaryToDecimal(ciphertext)),8);
cout<<"***************是否跳出 跳出为0 继续为1***************"<<endl;
cin>>index;
switch(index){
case 0:{
cout<<"***************欢迎使用s-des程序***************"<<endl;
cout<<"***************hyq ***************";
return 0;
}
case 1: goto loop;
}
}
return EXIT_SUCCESS;
}

S-DES算法实现(C++版本)的更多相关文章

  1. PHP版DES算法加密数据(3DES)另附openssl_encrypt版本

    PHP版DES算法加密数据(3DES) 可与java的DES(DESede/CBC/PKCS5Padding)加密方式兼容 <?php /** * Created by PhpStorm. * ...

  2. DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 1.DES算法简介 DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准. DES是一个分组 ...

  3. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  4. DES 算法的 C++ 与 JAVA 互相加解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  5. MFC 简单实现 DES 算法

    前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...

  6. 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密

    在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ...

  7. Asp.Net 常用工具类之加密——对称加密DES算法(2)

    又到周末,下午博客园看了两篇文章,关于老跳和老赵的程序员生涯,不禁感叹漫漫程序路,何去何从兮! 转眼毕业的第三个年头,去过苏州,跑过上海,从一开始的凌云壮志,去年背起行囊默默回到了长沙准备买房,也想有 ...

  8. AES算法,DES算法,RSA算法JAVA实现

    1     AES算法 1.1    算法描述 1.1.1      设计思想 Rijndael密码的设计力求满足以下3条标准: ① 抵抗所有已知的攻击. ② 在多个平台上速度快,编码紧凑. ③ 设计 ...

  9. 安全体系(一)—— DES算法详解

    本文主要介绍了DES算法的步骤,包括IP置换.密钥置换.E扩展置换.S盒代替.P盒置换和末置换. 安全体系(零)—— 加解密算法.消息摘要.消息认证技术.数字签名与公钥证书 安全体系(二)——RSA算 ...

  10. DES算法原理完整版

    1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...

随机推荐

  1. linux软链接的创建、删除和更新

    大家都知道,有的时候,我们为了省下空间,都会使用链接的方式来进行引用操作.同样的,在系统级别也有.在Windows系列中,我们称其为快捷方式,在Linux中我们称其为链接(基本上都差不多了,其中可能有 ...

  2. python将字符串类型改成日期类型

    将字符串类型的'2019-03-14'改成date类型,如下: import datetime b = datetime.date(*map(int,'2019-03-14'.split('-'))) ...

  3. cf 938E

    哇自闭了. 一样个毛啊. 和之前见过的几道感觉很类似啊. 首先一个数如果有贡献那么在他后面一定有一个大于它的数,并且前面的全比他小,然后我就跑偏了... 于是我们先排个序,显然无影响,我们可以考虑从 ...

  4. roadhog 介绍

    官方网站:https://www.npmjs.com/package/roadhog; 项目搭建demo: https://github.com/ght5935/antd-dva-less-webpa ...

  5. Java 中的泛型

    泛型的一般意义: 泛型,又叫 参数多态或者类型参数多态.在强类型的编程语言中普遍作用是:加强编译时的类型安全(类型检查),以及减少类型转换的次数. Java 中的 泛型: 编译时进行 类型擦除 生成与 ...

  6. 24.redux

    Flux:Flux 是一种架构思想 https://facebook.github.io/flux/ 官网 资料: http://www.ruanyifeng.com/blog/2016/01/flu ...

  7. 无网络 使用pip安装mxnet

    # 在有网络的同系统机器上运行以下命令:pip download mxnet# 目前mxnet版本为1.3.0,执行后当前目录得到以下文件: # . # ├── certifi--py2.py3-no ...

  8. ELK之使用heartbeat监控WEB站点

    简介 无论您要测试同一台主机上的服务,还是要测试开放网络上的服务,Heartbeat 都能轻松生成运行时间数据和响应时间数据 Heartbeat 能够通过 ICMP.TCP 和 HTTP 进行 pin ...

  9. C#4.0 HTTP协议无法使用TLS1.2的问题

    在发送HTTP请求前加入下行代码 ServicePointManager.SecurityProtocol = (SecurityProtocolType) | (SecurityProtocolTy ...

  10. 模拟器中安装APK报Error:INSTALL_FAILED_NO_MATCHING_ABIS

    1.启动AVD Manager.exe 2.将APP的安装包.apk直接拖到模拟器中,报错 3.原来是代码里由于大小限制只开放了armeabi-v7a这个ABI,但创建的模拟机支持的CPU是其他类型的 ...