混沌加密算法设计 java(单例模式设计)

什么是混沌系统?

一种非线性系统,什么是非线性?

比较典型的混沌系统?

Logistic 混沌映射

Logistic混沌映射是一个多项式映射,它作为一个由非常简单的非线性动力学方程产生非常复杂而混沌的结果的经典例子,而经常被引用。该混沌映射最初是由生物学家RobertMay1976年的一份创新性文件中,以一个和皮埃尔·弗朗索瓦·弗赫尔斯特所创的Logistic方程类似的离散人口模型的形式来推广的。

计算公式?

Logistic映射法生成混沌数据利用如下公式实现

代码设计如下:

package com.cuit.hundun;

import java.io.*;
import java.math.BigDecimal;
import java.util.Arrays;

public class Chaos
{

	//懒汉式(单例模式)
	private static Chaos chaos = null;
	
	private Chaos(){}
	
	public static Chaos getInstance()
	{
		if(chaos == null)
		{
			//防止线程同步问题,这种方式可以提高性能
			synchronized(Chaos.class)
			{
				if(chaos == null)
				{
					chaos = new Chaos();
				}
			}
		}
		return chaos;
	}
	
	//得到明文byte[]
	public byte[] getPlaintextOfBytes(String filePath)
	{
		byte[] plaintextOfBytes = null;
		byte[] b = new byte[1024];
		InputStream is = null;
		
		if(filePath == null)
		{
			return null;
		}
		try
		{
			is = new FileInputStream(filePath);
			int byteRead,count=0;
			while((byteRead = is.read()) != -1)
			{
				b[count++] = (byte) byteRead;
				if(count%1024 ==0)
				{
					Arrays.copyOf(b, 1024*(count%1024+1));
				}
			}
			plaintextOfBytes = new byte[count];
			for(int i=0; i<count; i++)
			{
				plaintextOfBytes[i] = b[i];
			}
			System.out.println("\nlenth:"+plaintextOfBytes.length);
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}finally
		{
			try
			{
				if(is != null)
					is.close();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return plaintextOfBytes;
	}
	
	// 得到密钥byte[]
	public byte[] getKey(int u, double x,int length)
	{
		byte[] key = new byte[length];
		BigDecimal[] temp = new BigDecimal[length*8+1];
		BigDecimal uu = new BigDecimal(""+u);
		temp[0] = new BigDecimal(""+x);
		
		for(int i=1; i<length*8+1; i++)
		{
			temp[i] = uu.multiply(temp[i-1]).multiply((new BigDecimal("1").subtract(temp[i-1]))).setScale(2, BigDecimal.ROUND_FLOOR);
		}
		for(int i=1; i<length*8+1; i++)
		{
			System.out.println(i+":"+temp[i].doubleValue());
		}
		for(int i=1; i<length+1; i++)
		{
			int tt = 0;
			int j=(i-1)*8+1;
			if(temp[j].doubleValue() > 0.5)
				tt += 128;
			if(temp[j+1].doubleValue() > 0.5)
				tt += 64;
			if(temp[j+2].doubleValue() > 0.5)
				tt += 32;
			if(temp[j+3].doubleValue() > 0.5)
				tt += 16;
			if(temp[j+4].doubleValue() > 0.5)
				tt += 8;
			if(temp[j+5].doubleValue() > 0.5)
				tt += 4;
			if(temp[j+6].doubleValue() > 0.5)
				tt += 2;
			if(temp[j+7].doubleValue() > 0.5)
				tt += 1;
			key[i-1] = (byte)(tt&0xff);
		}
		return key;
	}
	
	//加密
	public byte[] encrypt(byte[] plaintextOfBytes,byte[] key)
	{
		int length = plaintextOfBytes.length;
		byte[] ciphertestOfBytes = new byte[length];
		for(int i=0; i<length; i++)
			ciphertestOfBytes[i] = (byte) ((plaintextOfBytes[i]&0xff)^(key[i]&0xff)&0xff);
		return ciphertestOfBytes;
	}
	
	//保存密文byte[]
	public void saveCiphertext(String saveFilePathName,byte[] ciphertextOfBytes)
	{
		OutputStream os = null;
		if(saveFilePathName != null)
		{
			try
			{
				String str = new String(ciphertextOfBytes);
				System.out.println(str);
				os = new FileOutputStream(saveFilePathName);
				//os.write(ciphertextOfBytes);
				os.write(ciphertextOfBytes);
			} catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally
			{
				try
				{
					if(os != null)
						os.close();
					
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
}


 

封装的也许不是很好,希望对大家学习有帮助。


最新博客

 

发布了40 篇原创文章 · 获赞 54 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/wik_123/article/details/8244550