混沌加密算法設計 java(單例模式設計)

什麼是混沌系統?

一種非線性系統,什麼是非線性?java

比較典型的混沌系統?
Logistic 混沌映射
Logistic 混沌映射是一個多項式映射,它做爲一個由很是簡單的非線性動力學方程產生很是複雜而混沌的結果的經典例子,而常常被引用。該混沌映射最初是由生物學家 RobertMay 1976 年的一份創新性文件中,以一個和皮埃爾 · 弗朗索瓦 · 弗赫爾斯特所創的 Logistic 方程相似的離散人口模型的形式來推廣的。
 
計算公式?
Logistic 映射法生成混沌數據利用以下公式實現
 
 
 

 

代碼設計以下:ide

 

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();
				}
			}
		}
	}
	
	
}


 

封裝的也許不是很好,但願對你們學習有幫助性能

相關文章
相關標籤/搜索