要求是实现DES加密,解密,我是用python实现的,还是有挺多坑的,改bug就改了挺久,加密实现后,解密过程就比较轻松。

另外,附加base64编码源码

要求:输入秘钥为64位二进制数(有效位为56位,其中每八位最后一位为奇偶校验位),明文为64位二进制数,输出为64位二进制数,解密过程大致为逆向过程

base64代码如下:

base='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

def base64(string):
out=''
c=len(string)%3
a=''.join(bin(ord(i)).replace('0b','').zfill(8) for i in string)
if(c==1):
a+='0000'
if(c==2):
a+='00'
m=int(len(a)/6)
for j in range(m):
out+=base[int(a[j*6:j*6+6],2)]
if(c==1):
out+='=='
if(c==2):
out+='='
return out string=raw_input('please input the text:')
print ('the base64text is:'),base64(string)

  DES代码如下:

# -*- coding: utf-8 -*-
import time
import base64
import os m=0
C0=''
D0=''
L0=''
R0=''
cipher=''
outtext='' substitute1=[57,49,41,33,25,17, 9,
1 ,58,50,42,34,26,18,
10, 2,59,51,43,35,27,
19,11, 3,60,52,44,36,
63,55,47,39,31,23,15,
7 ,62,54,46,38,30,22,
14, 6,61,53,45,37,29,
21,13, 5,28,20,12, 4] substitute2=[14,17,11,24, 1, 5, 3,28,
15, 6,21,10,23,19,12, 4,
26, 8,16, 7,27,20,13, 2,
41,52,31,37,47,55,30,40,
51,45,33,48,44,49,39,56,
34,53,46,42,50,36,29,32] shift=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1] KEY=[ dict() for i in range(16)] sbox= [
[
0xe,0x4,0xd,0x1,0x2,0xf,0xb,0x8,0x3,0xa,0x6,0xc,0x5,0x9,0x0,0x7,
0x0,0xf,0x7,0x4,0xe,0x2,0xd,0x1,0xa,0x6,0xc,0xb,0x9,0x5,0x3,0x8,
0x4,0x1,0xe,0x8,0xd,0x6,0x2,0xb,0xf,0xc,0x9,0x7,0x3,0xa,0x5,0x0,
0xf,0xc,0x8,0x2,0x4,0x9,0x1,0x7,0x5,0xb,0x3,0xe,0xa,0x0,0x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10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32, 1] substituteP=[
16, 7,20,21,29,12,28,17,
1 ,15,23,26, 5,18,31,10,
2 ,8 ,24,14,32,27, 3, 9,
19,13,30, 6,22,11, 4,25] resubstituteip=[
40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,33,1,41, 9,49,17,57,25] def handleraw(string):
a=''
for i in string:
a+=bin(ord(i)).replace('0b','').zfill(8)
return a def shiftleft(string,x):
return string[-(len(string)-x):]+string[:x] def encodepassword1(x):
global C0
global D0
for i in range(0,28):
C0+=x[substitute1[i]-1]
for j in range(28,56):
D0+=x[substitute1[j]-1] k=0
def encodepassword2():
global k,C0,D0
t=''
if(k!=16):
C0=shiftleft(C0,shift[k])
D0=shiftleft(D0,shift[k])
s=C0+D0
for i in substitute2:
t+=s[i-1]
KEY[k]=t
k+=1
encodepassword2() def substituteIP(string):
global L0,R0
a=''
for i in substituteip:
a+=string[i-1]
L0=a[:32]
R0=a[-32:] def sboxoperate():
mid=''
for i in selectoperate:
mid+=R0[i-1]
mid=str(bin(int(mid,2)^int(KEY[m-1],2)).replace('0b','').zfill(48))
outraw=''
for n in range(8):
a=int(mid[6*n]+mid[6*n+5],2)
b=int(mid[(6*n+1):(6*n+5)],2)
outraw+=str(bin(int(str(sbox[n][a*16+b]),10)).replace('0b','').zfill(4))
'''print 'S:',m,outraw'''
out=''
for i in substituteP:
out+=outraw[i-1]
return out def encodemain():
global m,L0,R0,cipher
'''print 'L0,R0',L0,R0'''
m+=1
a=L0
L0=R0
R0=str(bin(int(sboxoperate(),2)^int(a,2)).replace('0b','').zfill(32))
if(m!=16):
encodemain()
else:
b=R0+L0
'''print 'L0,R0',L0,R0'''
for i in resubstituteip:
cipher+=b[i-1] def decodecircle():
global L0,R0,outtext,m
a=R0
R0=L0
L0=str(bin(int(sboxoperate(),2)^int(a,2)).replace('0b','').zfill(32))
m-=1
if(m!=0):
decodecircle()
else:
d=L0+R0
decodetext=[0 for i in range(64)]
for i in range(64):
decodetext[substituteip[i]-1]=d[i]
outtext=''.join(decodetext) def decoderesubstituteip(string):
global R0,L0,m
m=16
a=[0 for i in range(64)]
for i in range(64):
a[resubstituteip[i]-1]=string[i]
a=''.join(a)
R0=a[:32]
L0=a[-32:] if __name__=='__main__':
e=raw_input('This is a DES encrypt and decrypt function\n press 1 to encrypt, 2 to decrypt:')
password=raw_input('please input the key:\n')
encodepassword1(password)
encodepassword2()
'''print 'the KEY is:\n',KEY'''
if(e=='1'):
text=raw_input('please input the text:\n')
substituteIP(text)
encodemain()
print 'the cipher is:',cipher
if(e=='2'):
ciphertext=raw_input('please input the cipher:\n')
decoderesubstituteip(ciphertext)
decodecircle()
print 'the decodetext is:',outtext
os.system("pause")

  

  

PYTHON实现DES加密及base64源码的更多相关文章

  1. Java DES 加密和解密源码(转)

    原文地址:http://www.oschina.net/code/snippet_727646_18383 Java密码学结构设计遵循两个原则: 1) 算法的独立性和可靠性. 2) 实现的独立性和相互 ...

  2. 为SRS流媒体服务器添加HLS加密功能(附源码)

    为SRS流媒体服务器添加HLS加密功能(附源码) 之前测试使用过nginx的HLS加密功能,会使用到一个叫做nginx-rtmp-module的插件,但此插件很久不更新了,网上搜索到一个中国制造的叫做 ...

  3. 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解

    原文地址:http://docode.top/Article/Detail/10003 目录: 1..Net(C#)平台下Des加密解密源代码 2..Net(C#)平台下Aes加密解密源代码 3..N ...

  4. python+requests接口自动化完整项目设计源码

    前言 有很多小伙伴吵着要完整的项目源码,完整的项目属于公司内部的代码,这个是没法分享的,违反职业道德了,就算别人分享了,也只适用于本公司内部的业务. 所以用例的代码还是得自己去一个个写,我只能分享项目 ...

  5. python+requests接口自动化完整项目设计源码(一)

    原文地址https://www.cnblogs.com/yoyoketang/tag/python接口自动化/ 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址 ...

  6. pyDes库 实现python的des加密

    下载及简介地址:https://twhiteman.netfirms.com/des.html 如需要在python中使用des加密,可以直接使用pyDes库加密,该库提供了CBC和ECB两种加密方式 ...

  7. python+requests接口自动化完整项目设计源码【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

  8. iOS DES 加密转base64

      //用法 加密转base 64 NSString *str = [self base64StringFromText:@"qingjoin" withKey:@"ke ...

  9. python成长之路10——socketserver源码分析

    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 参数一:地址簇 socket.AF_INET ipv4(默认) socket.AF_INE ...

随机推荐

  1. javascript进阶系列专题:作用域与作用域链

    字面意思,作用域是指变量和函数的作用范围,换言之,作用域决定了变量和函数的可见性和有效时间.javascript作用域是用函数来区分,与其他语言的大括号不同. for (var i=0; i<5 ...

  2. archlinux 安装手记

    Wiki常用软件 https://wiki.archlinux.org/index.php/Common_Applications -> 移动硬盘等的自动挂载 pacman -S gvfs-af ...

  3. 实际项目中的一个angularjs 应用

    实际需求:通过下拉框,选择自己需要的类型,创建元素(要求必须是输入点击保存了才能出现对应的类型块) html代码: <div class="list-panel-data"& ...

  4. MongoDB 驱动以及分布式集群读取优先级设置

    本文主要介绍使用MongoDB C驱动读取分布式MongoDB集群时遇到的坑,主要在读取优先级和匹配tag上:同时简单介绍Python驱动.Node.js驱动.Mongoose驱动如何使用读取优先级和 ...

  5. C#读写config配置文件

    应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 对于一个config ...

  6. 常用的web功能测试方法

    功能测试就是对产品各功能进行验证,根据功能测试用例,逐项测试,检查产品是否达到用户要求功能,即是否满足需求.常用的测试方法如下: 1.页面连接检查:每一个连接是否都有对应的页面,并且页面之间切换正确. ...

  7. spark 1.5.2配置记录

    1)slaves # A Spark Worker will be started on each of the machines listed below. dataNode 2)spark-env ...

  8. Android基础:startActivityForResult 和 onActivityResult 问题

    项目中用到弹出Acitivity来获得用户输入 所以用到 onActivityResult()方法接受用户输入 奇怪问题 startActivityForResult() 后直接调用 onActivi ...

  9. HDU 4937 Lucky Number(2014 Multi-University Training Contest 7)

    思路:先枚举  a*bas +b = n  求出 bas 在sqrt(n)到n的  (bas>a&&bas>b) 再枚举  a*bas*bas+b*bas+c =n  求出 ...

  10. Redux教程1:环境搭建,初写Redux

    如果将React比喻成士兵的话,你的程序还需要一位将军,去管理士兵(的状态),而Redux恰好是一位好将军,简单高效: 相比起React的学习曲线,Redux的稍微平坦一些:本系列教程,将以" ...