由於工作需要,想把github作為公司的代碼管理倉庫,但是又不能公開代碼,所以很簡單,就是加密后再git上傳。加密算法自然要選擇效率高的,同時又是安全的。但是歷史上好像這兩項都是違背的,我說我要自己設計加密算法你們會不會噴我?但是我就是自己設計了。
思想很簡單,就是用與明文相同位數的隨機序列與明文異或!偽隨機數發生器也是我自己diy的!如果你會因為這兩點就懷疑我的加密算法的安全性,那先稍等一會兒。這個偽隨機數發生器是用混沌方程當中的最簡單的單峰映射(logstic map): xn+1=a*xn*(1-xn) , 這個可以看wikipedia 的英文版介紹:https://en.wikipedia.org/wiki/Logistic_map 其中說到:The relative simplicity of the logistic map makes it a widely used point of entry into a consideration of the concept of chaos. A rough description of chaos is that chaotic systems exhibit a great sensitivity to initial conditions—a property of the logistic map for most values of r between about 3.57 and 4 (as noted above).
我曾用NIST的隨機監測工具檢測過經logistic map選取合適的 a 值后產生序列的隨機性(要截取每個產生的數的小數點第五位以后的部分作為隨機數),發現甚至比目前大多數偽隨機算法都要好,甚至比安全學領域中使用的一些偽隨機算法還好。而且最大的優點是,人家速度快。
於是,這是一個可以高度自己定制的偽隨機數發生器,a值只要選在3.57~4之間就行。於是選取的a不同,用於加密的算法也就變了(算法沒變,但相當於變了)。這個加密方法類似於一次一密,所以不可能從對密文的分析中找出破解辦法,因為密文是隨機的。因此破解辦法是暴力的方法,但是每個人使用的加密算法都可以不同,畢竟改一個a值太簡單。另外就是xn的初始值,這也就是加密時的密鑰。初始值需要時在(0,1)之間的任何值,但不包括0和1. 遞推后產生的值也一直處於該區間內。
采用暴力破解法,復雜度取決於xn的有效數字。double類型的數有16~17位的有效數字,因此密鑰的復雜度大概是1016,約相當於253,也就是說該加密算法密鑰長度是53位。額,首先,我認為這已經足夠了,因為沒人知道我的加密算法,即使我把算法公之於眾,因為我還可以改a的值。其次,在加密中,是借錢xn的第5~8位數與Unicode異或,我完全可以改成與第12~16位啊。即便如此,我們還可以任意的加長遞推運算的有效數字位數,雖然這會帶了效率上的缺失。你完全可以使用50位甚至更高精度的浮點數,50位復雜度就可以達到1050,相當於2165~2170位密鑰長度。總之,這是一個沒有限制的的算法。要是量子計算機來了,暴力破解100位的密鑰長度的該算法需要1秒,那么就可以采用10000位啊,搜索域增加了1010000-10100,反正時間還是指數增長……
算法本來加密輸出是亂碼的,后來改為輸出Unicode數字碼,這樣網絡兼容性更好(不會因為亂碼中空格漏掉一個而全都解密不了)。不說了,貼python代碼:
import os
# -*- coding:utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import tkinter.ttk as ttk
import os
class Window:
def __init__(self,root):
self.label=ttk.Label(root, text='Input PassWord: ')
self.entry=ttk.Entry(root,show='*')
self.passw=''
if os.path.exists('important_file'):
with open('important_file') as f:
self.passw=f.readline()
else:
self.label.pack(side='left',anchor='nw',pady=10)
self.entry.pack(side='top', after=self.label,anchor='nw',pady=10)
pass
self.button=ttk.Button(root, text='解密', command=self.Decipher)
self.button.pack(side='top',anchor='nw')
self.button=ttk.Button(root,text='加密',command=self.Encrypt)
self.button.pack(side='top',anchor='nw')
self.texti=tk.Text()
#self.texti.insert(1.0,'Input something here.')
self.texto=tk.Text()
self.texti.pack(side='top',anchor='nw',pady=20)
self.texto.pack(side='top',anchor='nw',pady=20)
self.inputext=''
def Decipher(self):
if not self.passw:
self.passw=self.entry.get()
if not self.passw:
tk.messagebox.showinfo('Error','Why not enter the password?'); return
self.inputext=self.texti.get(1.0,tk.END)
if self.inputext=='': tk.messagebox.showinfo('Error','What is your inputext?'); return
if not self.inputext[0].isdigit(): tk.messagebox.showinfo('Error','Be sure that your input starts with digits!'); return
self.__crack()
def Encrypt(self):
if not self.passw:
self.passw=self.entry.get()
if not self.passw:
tk.messagebox.showinfo('Error','Why not enter the password?'); return
self.inputext=self.texti.get(1.0,tk.END)
if self.inputext=='': tk.messagebox.showinfo('Error','What is your inputext?'); return
if self.inputext[0].isdigit():
if not tk.messagebox.askyesno('Note','Are you sure you are encrypting digits?'): return
self.__encrypt()
def __crack(self):
'''indicates that the input text should be number only '''
self.texto.delete(1.0,tk.END)
listp=list(self.passw)
p=[ord(x) for x in listp]
pw=1
for x in p:
pw=x*pw
pws='0.'+str(pw)
pw=float(pws)
listin=self.inputext.split(' ')
for x in listin:
pw=pw*(1-pw)*3.93699989893668722729139042
pws=str(pw)
pivot= int(pws[-6:-3])
if x=='':
self.texto.insert(tk.INSERT,' ')
elif x[0]=='\n':
self.texto.insert(tk.INSERT,'\n')
else:
self.texto.insert(tk.INSERT,chr(int(x)^pivot))
if not os.path.exists('important_file'): self.__writepw()
pass
def __encrypt(self):
'''turn string into number string every word takes one lines'''
self.texto.delete(1.0,tk.END)
listin=list(self.inputext)
listp=list(self.passw)
p=[ord(x) for x in listp]
pw=1
for x in p:
pw=x*pw
pws='0.'+str(pw)
pw=float(pws)
for x in listin:
pw=pw*(1-pw)*3.93699989893668722729139042
pws=str(pw)
pivot= int(pws[-6:-3])
self.texto.insert(tk.INSERT,str(ord(x)^pivot)+' ')
pass
def __writepw(self):
with open('important_file','w') as f:
f.write(self.passw)
os.system('attrib +s +r +h +a important_file')
root=tk.Tk()
root.title('OUR CIPHER TALK')
window=Window(root)
root.mainloop()
代碼是python3的,需要tkinter,windows上能運行,其它系統未知。這個只是加密解密結合的程序,博主根據這個算法寫的將github改造為私密倉庫的代碼在我的github地址上:https://github.com/AdaJass/privatesolution