简单命令行的RC4算法的单个文件加密小程序,rc4文件加密小程序,简单文件加密import


简单文件加密

import sysimport os#import datetimeimport time#This program is used to encrypt a file to another file and it take a RC4 algorithm to finish this job.#This is only a simple version, and will be improved later.#start_time = time.clock()if len(sys.argv) < 4:  print 'usage:"python encrpyptiobn.py inputFilename outputFilename password"'  exit(0)file_input = sys.argv[1]file_object = open(file_input)try:  byte_data = file_object.read()  byte_data = [ord(i) for i in byte_data]finally:  file_object.close()file_output = sys.argv[2]file_object = open(file_output, 'wb')byte_write = byte_data[:]key = sys.argv[3]keyLength = len(key)key = [ord(i) for i in key]#s = [0 for i in range(256)]i = 0j = 0#k = [0 for i in range(256)]k = []tmp = 0#for i in range(256):#  s[i] = i#  k[i] = key[i % keyLength]s = [ i for i in range(256)]k = [ key[i % keyLength] for i in range(256)]for i in range(256):  j = (j + s[i] + k[i]) % 256  tmp = s[i]  s[i] = s[j]  s[j] = tmpx = 0y = 0t = 0i = 0tmp = 0dataLength = len(byte_data)#for i in range(dataLength):while i < dataLength:  x = (x + 1) % 256  y = (y + s[x]) % 256  tmp = s[x]  s[x] = s[y]  s[y] = tmp  t = (s[x] + s[y])%256  byte_data[i] = byte_data[i] ^ s[t]  i += 1out_bytes = ''i = 0#for i in range(dataLength):#while i < dataLength:#  out_bytes += chr(byte_data[0])#  del byte_data[0]#  i += 1while i < dataLength:  out_bytes+=chr(byte_data[i])  i += 1try:  file_object.write(out_bytes)finally:  file_object.close()#end_time = time.clock()#delavalue = (end_time-start_time)#delavalue = delavalue.seconds*1000+delavalue.microseconds#print "executing the program takes %s seconds" % delavalue

评论关闭