The RC4 algorithm implemented in Python _python

Source: Internet
Author: User
Tags base64 chr md5 ord in python

In my spare time, I implemented the RC4 algorithm with Python.

Coding UTF-8

Class method

#/usr/bin/python #coding =utf-8 Import sys,os,hashlib,time,base64 class Rc4:def __init__ (Self,public_key = None,ckey_ Lenth =: 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]). He Xdigest () Self.keyc = ' def encode (self,string): SELF.KEYC = Hashlib.md5 (str (time.time ()). Hexdigest () [32-se  LF.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.key c = string[0:self.ckey_lenth] string = Base64.b64decode (string[self.ckey_lenth:]) Self.result = ' Self.docryp T (string) result = Self.result if (result[0:10] = = ' 0000000000 ' or int (result[0:10])-Int (Time.time ()) > 0) D result[10:26] = = HASHLIB.MD5 (result[26:] + self.keyb). Hexdigest () [0:16]: return result[26:] Else:return None def docrypt (sel f,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): Rand
      Key.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])

Test:

rc = RC4 (' Nishidahuaidan ')
string = ' I'm here, where are you? '
print (string)
str = Rc.encode (string)
print (str)
str = rc.decode (str)
print (str)

function mode

#/usr/bin/python #coding =utf-8 Import sys,os,hashlib,time,base64 def RC4 (string, op = ' encode ', Public_key = ' ddd ', exp
  Irytime = 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 = d Ecode ' 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-xrange (255): Randkey.append (Ord (cryptkey[i% key_lenth])) for I I

  n 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] = TM P Result + = Chr (ord (String[i]) ^ (box[(Box[a] + box[j])% 256]) if op = = ' decode ': if (result[0:10) = = ' 000000 0000 ' or int (result[0:10])-Int (Time.time ()) > 0) and result[10:26] = = HASHLIB.MD5 (result[26:] + keyb). Hexdigest () [0:1

 6]: Return result[26:] Else:return None else:return KEYC + base64.b64encode (Result)

Test:

String = ' I'm here, where are you? '
print (string)
str = RC4 (string, ' encode ')
print (str)
rc = RC4 (str, ' decode ')
Print (RC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.