欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中實現(xiàn)的RC4算法

 更新時間:2015年02月14日 11:15:42   作者:IT筆記  
這篇文章主要介紹了Python中實現(xiàn)的RC4算法,本文給出了類和函數(shù)兩種實現(xiàn)方式,需要的朋友可以參考下

閑暇之時,用Python實現(xiàn)了一下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中的集合介紹

    Python中的集合介紹

    今天小編就為大家分享一篇關于Python中的集合介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Python中Dataframe數(shù)據(jù)排序方法(含實例講解)

    Python中Dataframe數(shù)據(jù)排序方法(含實例講解)

    在進行數(shù)據(jù)分析操作時,經(jīng)常需要對數(shù)據(jù)按照某行某列排序,或者按照多行多列排序,以及按照索引值排序等等,下面這篇文章主要給大家介紹了關于Python中Dataframe數(shù)據(jù)排序方法的相關資料,需要的朋友可以參考下
    2023-02-02
  • 利用python爬取古詩文網(wǎng)中各類古詩的方法

    利用python爬取古詩文網(wǎng)中各類古詩的方法

    這篇文章主要介紹了利用python爬取古詩文網(wǎng)中各類古詩的方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • 使用?OpenCV?開發(fā)虛擬鍵盤的方法

    使用?OpenCV?開發(fā)虛擬鍵盤的方法

    OpenCV是一個強大的圖像處理工具,用于機器學習、圖像處理等的跨平臺開源庫,用于開發(fā)實時計算機視覺應用程序,本文重點給大家介紹使用?OpenCV?開發(fā)虛擬鍵盤的方法,感興趣的朋友一起看看吧
    2021-11-11
  • Python開發(fā)的HTTP庫requests詳解

    Python開發(fā)的HTTP庫requests詳解

    Requests是用Python語言編寫,基于urllib,采用Apache2 Licensed開源協(xié)議的HTTP庫。它比urllib更加方便,可以節(jié)約我們大量的工作,完全滿足HTTP測試需求。Requests的哲學是以PEP 20 的習語為中心開發(fā)的,所以它比urllib更加Pythoner。更重要的一點是它支持Python3哦!
    2017-08-08
  • Python3的urllib.parse常用函數(shù)小結(jié)(urlencode,quote,quote_plus,unquote,unquote_plus等)

    Python3的urllib.parse常用函數(shù)小結(jié)(urlencode,quote,quote_plus,unquot

    這篇文章主要介紹了Python3的urllib.parse常用函數(shù),結(jié)合實例形式分析了urlencode,quote,quote_plus,unquote,unquote_plus等函數(shù)的相關使用技巧,需要的朋友可以參考下
    2016-09-09
  • Python基礎之數(shù)據(jù)類型知識匯總

    Python基礎之數(shù)據(jù)類型知識匯總

    今天帶大家復習一下Python基礎知識,文中對數(shù)據(jù)類型相關知識做了詳細的匯總,對剛?cè)腴Tpython的小伙伴很有幫助喲,需要的朋友可以參考下
    2021-05-05
  • pytest中fixture函數(shù)使用

    pytest中fixture函數(shù)使用

    本文主要介紹了pytest中fixture函數(shù)使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python列表切片用法示例

    Python列表切片用法示例

    這篇文章主要介紹了Python列表切片用法,結(jié)合實例形式分析了Python列表切片的常見操作方法及相關注意事項,需要的朋友可以參考下
    2017-04-04
  • python字符串拼接和列表拼接方式

    python字符串拼接和列表拼接方式

    這篇文章主要介紹了python字符串拼接和列表拼接方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02

最新評論