发布于 2015-02-24 14:54:34 | 466 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python3 官方中文指南,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


闲暇之时,用Python实现了一下RC4算法

编码 UTF-8

class 方式

#/usr/bin/python
#coding=utf-8

import sys,os,hashlib,time,base64
class rc4:

  def __init__(self,public_key = None,ckey_lenth = 16):
    self.ckey_lenth = ckey_lenth
    self.public_key = public_key or 'none_public_key'
    key = hashlib.md5(self.public_key).hexdigest()
    self.keya = hashlib.md5(key[0:16]).hexdigest()
    self.keyb = hashlib.md5(key[16:32]).hexdigest()
    self.keyc = ''

  def encode(self,string):
    self.keyc = hashlib.md5(str(time.time())).hexdigest()[32 - self.ckey_lenth:32]
    string = '0000000000' + hashlib.md5(string + self.keyb).hexdigest()[0:16] + string
    self.result = ''
    self.docrypt(string)
    return self.keyc + base64.b64encode(self.result)

  def decode(self,string):
    self.keyc = string[0:self.ckey_lenth]
    string = base64.b64decode(string[self.ckey_lenth:])
    self.result = ''
    self.docrypt(string)
    result = self.result
    if (result[0:10] == '0000000000' or int(result[0:10]) - int(time.time()) > 0) and result[10:26] == hashlib.md5(result[26:] + self.keyb).hexdigest()[0:16]:
      return result[26:]
    else:
      return None

  def docrypt(self,string):
    string_lenth = len(string)
    result = ''
    box = list(range(256))
    randkey = []

    cryptkey = self.keya + hashlib.md5(self.keya + self.keyc).hexdigest()
    key_lenth = len(cryptkey)

    for i in xrange(255):
      randkey.append(ord(cryptkey[i % key_lenth]))

    for i in xrange(255):
      j = 0
      j = (j + box[i] + randkey[i]) % 256
      tmp = box[i]
      box[i] = box[j]
      box[j] = tmp

    for i in xrange(string_lenth):
      a = j = 0
      a = (a + 1) % 256
      j = (j + box[a]) % 256
      tmp = box[a]
      box[a] = box[j]
      box[j] = tmp
      self.result += chr(ord(string[i]) ^ (box[(box[a] + box[j]) % 256]))

测试:

rc = rc4('nishidahuaidan')
string = '我在这里呢,你在那里呢'
print(string)
str = rc.encode(string)
print(str)
str = rc.decode(str)
print(str)

function方式

#/usr/bin/python
#coding=utf-8

import sys,os,hashlib,time,base64

def rc4(string, op = 'encode', public_key = 'ddd', expirytime = 0):
  ckey_lenth = 4
  public_key = public_key and public_key or ''
  key = hashlib.md5(public_key).hexdigest()
  keya = hashlib.md5(key[0:16]).hexdigest()
  keyb = hashlib.md5(key[16:32]).hexdigest()
  keyc = ckey_lenth and (op == 'decode' and string[0:ckey_lenth] or hashlib.md5(str(time.time())).hexdigest()[32 - ckey_lenth:32]) or ''
  cryptkey = keya + hashlib.md5(keya + keyc).hexdigest()
  key_lenth = len(cryptkey)
  string = op == 'decode' and base64.b64decode(string[4:]) or '0000000000' + hashlib.md5(string + keyb).hexdigest()[0:16] + string
  string_lenth = len(string)

  result = ''
  box = list(range(256))
  randkey = []

  for i in xrange(255):
    randkey.append(ord(cryptkey[i % key_lenth]))

  for i in xrange(255):
    j = 0
    j = (j + box[i] + randkey[i]) % 256
    tmp = box[i]
    box[i] = box[j]
    box[j] = tmp

  for i in xrange(string_lenth):
    a = j = 0
    a = (a + 1) % 256
    j = (j + box[a]) % 256
    tmp = box[a]
    box[a] = box[j]
    box[j] = tmp
    result += chr(ord(string[i]) ^ (box[(box[a] + box[j]) % 256]))

  if op == 'decode':
    if (result[0:10] == '0000000000' or int(result[0:10]) - int(time.time()) > 0) and result[10:26] == hashlib.md5(result[26:] + keyb).hexdigest()[0:16]:
      return result[26:]
    else:
      return None
  else:
    return keyc + base64.b64encode(result)

测试:

string = '我在这里呢,你在那里呢'
print(string)
str = rc4(string,'encode')
print(str)
rc = rc4(str,'decode')
print(rc)


相关阅读 :
Python中实现的RC4算法实例代码
Python实现的Kmeans++算法实例
在Python中实现贪婪排名算法的教程
Python中使用SAX解析xml实例代码
Python中对象的引用与复制代码示例
python+matplotlib实现动态绘制图片实例代码(交互式绘图)
Python中使用插入排序算法的简单分析与代码示例
python opencv实现任意角度的透视变换实例代码
python实现的排序算法示例
python实现的拒绝服务攻击代码
python桶排序(分块排序)算法实例代码
python实现ipsec开权限实例代码
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务