基于混沌序列的图像加密解密
计 算 机 工 程 2004年11月
摘 要:提出了基于混沌序列的图像加密解密算法,该算法由一种新的混沌系统生成整数值混沌序列,然后利用该整数值混沌序列把图像的每
一像素变换到另一位置,得到加密图像。 该算法具有计算量少、安全性高、无失真的特点,实验结果令人满意。
终于试着实现了,不过我做的效果不太好,而且解密不成功,原论文中也有错误。
用python实现的,程序写的很乱!
先放着
GenList.py就是上边那个迭代公式
===================GenList.py==========================
def GenList ( m, a, x ):
if x
x = 1
if m % a == 0:
x = m * x / a
else:
x = m * x / a + 1
elif x <= a:
if m % a == 0:
x = m * x / a
else:
x = m * x / a + 1
elif x > a and x
x = m * (m-x) / (m-a)
else :
x = 1
return x
if __name__ == "__main__":
m = 371
n = 205
x = 371
list = []
for i in range(1,15):
x = Fun (m, n, x)
list.append(x)
print x
print list[1]
print list
en.py加密图象
===================en.py==========================
import Image
import GenList
im = Image.open("hi.jpg")
imn = Image.new("RGB",im.size)
m = 1997
n = 1335
x = 33
list = []
#print m
#print n
M = im.size[0]
N = im.size[1]
for i in range(1,M+N+1):
x = GenList.GenList (m, n, x)
list.append(x)
#print x
#print list
for i in range(0,M):
XI = list[i] % N
for j in range(0,N):
if j + XI >= N:
imn.putpixel((i,j + XI - N),im.getpixel((i,j)))
else:
imn.putpixel((i,j + XI),im.getpixel((i,j)))
for j in range(0,N):
YI = list[j + M] % M
for i in range(0,M):
if i + YI >= M:
imn.putpixel((i + YI - M ,j),im.getpixel((i,j)))
else:
imn.putpixel((i + YI ,j),im.getpixel((i,j)))
imn.save("ok.jpg")
de.py解密图象(我解密出来的图象比我加密后的图象效果还好,不知道问题出在哪里)
===================de.py==========================
import Image
import GenList
im = Image.open("ok.jpg")
imn = Image.new("RGB",im.size)
m = 1997
n = 1335
x = 33
list = []
#print m
#print n
M = im.size[0]
N = im.size[1]
for i in range(1,M+N+1):
x = GenList.GenList (m, n, x)
list.append(x)
#print "hi"
#print list
for j in range(0,N):
YI = list[j + M] % M
for i in range(0,M):
if i >= YI:
imn.putpixel((i - YI,j),im.getpixel((i,j)))
else:
imn.putpixel((i - YI + M ,j),im.getpixel((i,j)))
for i in range(0,M):
XI = list[i] % N
for j in range(0,N):
if j >= XI:
imn.putpixel((i,j - XI ),im.getpixel((i,j)))
else:
imn.putpixel((i,j - XI + N),im.getpixel((i,j)))
imn.save("ori.jpg")
==========================================================
我写出来的python程序效率很低,用python加密一幅稍微大一点的图象要等近一分钟!
而且,加密后文件大小有变化,关键问题是不能解密,不知道问题出在哪里!
改天再研究吧!
2007-02-08
原因找到了!因为图片加密是分别对每行及每列象素移动,我写的python脚本,在移动完行以后进行列移动的时候依然使用原图片的象素颜色,而正确的使用经过行移动后的新图象的象素颜色!
脚本改后,加密效果果然好了,不过,解密后的图片出先了颜色丢失!可能是解密的过程有问题!
新修改的en.py
import Image
import GenList
im = Image.open("hi.jpg")
imn = Image.new("RGB",im.size)
imnn = Image.new("RGB",im.size)
m = 1997
n = 1335
x = 33
list = []
#print m
#print n
M = im.size[0]
N = im.size[1]
for i in range(1,M+N+1):
x = GenList.GenList (m, n, x)
list.append(x)
#print x
#print list
for i in range(0,M):
XI = list[i] % N
for j in range(0,N):
if j + XI >= N:
imn.putpixel((i,j + XI - N),im.getpixel((i,j)))
else:
imn.putpixel((i,j + XI),im.getpixel((i,j)))
for j in range(0,N):
YI = list[j + M] % M
for i in range(0,M):
if i + YI >= M:
imnn.putpixel((i + YI - M ,j),imn.getpixel((i,j)))
else:
imnn.putpixel((i + YI ,j),imn.getpixel((i,j)))
imnn.save("ok.jpg")
红色为修改部分,添加了临时图象文件!
修改后的de.py
import Image
import GenList
im = Image.open("ok.jpg")
imn = Image.new("RGB",im.size)
imnn = Image.new("RGB",im.size)
m = 1997
n = 1335
x = 33
list = []
#print m
#print n
M = im.size[0]
N = im.size[1]
for i in range(1,M+N+1):
x = GenList.GenList (m, n, x)
list.append(x)
#print "hi"
#print list
for j in range(0,N):
YI = list[j + M] % M
for i in range(0,M):
if i >= YI:
imnn.putpixel((i - YI,j),im.getpixel((i,j))) else:
imnn.putpixel((i - YI + M ,j),im.getpixel((i,j)))
for i in range(0,M):
XI = list[i] % N
for j in range(0,N):
if j >= XI:
imn.putpixel((i,j - XI ),imnn.getpixel((i,j)))
else:
imn.putpixel((i,j - XI + N),imnn.getpixel((i,j)))
imn.save("ori.jpg")
原图象
加密后
解密后
解密不成功,郁闷ing