#!/usr/bin/python
#encoding: UTF-8
#################################################################
# script: encode.py
# description: 實現對檔案相關加密解密
# version: 0.1 實現對普通檔案的加密解密
# usage: python encode.py [encode | decode] filename [key]
#################################################################
import sys, os, struct
# 加密字串
def encodeString(str):
key_index = 0
# 定義加密字串
str_encode = ''
# 字串轉換成位元組數組
bytes = bytearray(str)
# 按位元組加密
for b in bytes:
b_encode = (b + key_bytes[key_index]) % 256
str_encode += struct.pack('B', b_encode)
key_index = (key_index + 1) % len(key_bytes)
# 返回加密字串
return str_encode
# 解密字串
def decodeString(str):
key_index = 0
# 定義解密字串
str_decode = ''
# 字串轉換成位元組數組
bytes = bytearray(str)
for b in bytes:
b_decode = (b + 256 - key_bytes[key_index]) % 256
str_decode += struct.pack('B', b_decode)
key_index = (key_index + 1) % len(key_bytes)
# 返回解密字串
return str_decode
# 檢查命令列參數
if len(sys.argv) < 3 or len(sys.argv) > 4:
print 'Usage: python', sys.argv[0], '[encode | decode] filename [key]'
exit(1)
if sys.argv[1] not in ('encode', 'decode'):
print 'Usage: python', sys.argv[0], '[encode | decode] filename [key]'
exit(1)
# 檔案名稱
filename = sys.argv[2]
if not os.path.isfile(filename):
print 'File "' + filename + '" is not exsisted'
print 'Usage: python', sys.argv[0], '[encode | decode] filename [key]'
exit(1)
# 定義密鑰。加密方式:字串每個位元組與金鑰組應位元組相加後求餘
key = 'TESTKEY'
if len(sys.argv) == 4:
key = sys.argv[3]
key_bytes = bytearray(key)
# 加密或解密
mode = sys.argv[1]
# 加密或解密檔案
file_mode_name = filename + '.' + mode
# 執行加密或解密
if mode == 'encode':
file = open(filename, 'r')
file_encode = open(file_mode_name, 'w')
str = file.read()
str_encode = encodeString(str)
file_encode.write(str_encode)
file.close()
file_encode.close()
else:
file = open(filename, 'r')
file_decode = open(file_mode_name, 'w')
str = file.read()
str_decode = decodeString(str)
file_decode.write(str_decode)
file.close()
file_decode.close()
print 'Succeed to', mode, 'file "' + filename + '", result file is "' + file_mode_name + '"'