⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sushu.java

📁 用于一般的文件加密和解密
💻 JAVA
字号:
import java.math.*; 
import java.io.*; 
import java.util.Random;
import java.lang.reflect.*;
import java.lang.*;
class sushu{
		
	public BigInteger p,q,n,fn,d,i,l,e;	
		
	public void sushuInit()//这个函数经过测试正确。
	{ 
    
		p=new BigInteger("0");
		q=new BigInteger("0");
		n=new BigInteger("0");
		fn=new BigInteger("0");
		d=new BigInteger("0");
		i=new BigInteger("0");
		l=new BigInteger("1");
		e=new BigInteger("0");
		long gcde;
		Random j=new Random();
		
		p=p.probablePrime(1024,j);
		q=q.probablePrime(1024,j);
		e=e.probablePrime(512,j);
		n=p;
		n=n.multiply(q);
		i=q;
		i=i.subtract(l);
		fn=p;
		fn=fn.subtract(l);
		fn=fn.multiply(i);
		
		i=fn.gcd(e);
		gcde=i.longValue();
		while(gcde!=1)
		{
			e=e.probablePrime(512,j);
			i=fn.gcd(e);
			gcde=i.longValue();
		}
		
	}
	
	public sushu Init2() throws IOException
	{	
		
		String aOut	;
		sushu aSushu=new sushu();
		aSushu.sushuInit();
		set aSet =new set();
		aSet=aSushu.Extended_Euclid(aSushu.e,aSushu.fn);
		aSushu.d=aSet.x.mod(aSushu.fn);
			
		OutputStream fos=new FileOutputStream("解密密码.txt");
		
		try
		{
			DataOutputStream dos=new DataOutputStream(fos);
			aOut=aSushu.p.toString(10);	
			dos.writeBytes("\np:   "+aOut);
			aOut=aSushu.q.toString(10);	
			dos.writeBytes("q:   "+aOut);
			aOut=aSushu.n.toString(10);	
			dos.writeBytes("n:   "+aOut);
			aOut=aSushu.e.toString(10);	
			dos.writeBytes("e:   "+aOut);
			aOut=aSushu.d.toString(10);	
			dos.writeBytes("d:   "+aOut);
			dos.close();
		

		}
		
		catch(Exception e)
		{	
			System.out.println("Exception:"+e);
		}


		try
		{	
			
			fos.close();
		}
		catch(Exception e)
		{	
			System.out.println("Exception:"+e);
		}

		
		return aSushu;


	}
	
		public static set Extended_Euclid (BigInteger a,BigInteger b)
 		{
        set t=new set();
        set t1=new set();
        sushu bSushu=new sushu();
        int bbb;
        BigInteger aMb=new BigInteger("0");
        BigInteger aChb=new BigInteger("0");
        
        /*System.out.println(a+"aaaa");
        System.out.println(b+"bbbb");*/
        
        bbb=b.intValue();
        if(bbb==0) 
        {
                t.d=a;
                t.x=new BigInteger("1");
                t.y=new BigInteger("0");
                return t;
        }
        
        aMb=a.mod(b);
        //System.out.println(aMb+"aammmbb");
        
        t1=bSushu.Extended_Euclid(b,aMb);
        t.d=t1.d;
        t.x=t1.y;
        
        aChb=a.divide(b);
        aChb=aChb.multiply(t1.y);
        //System.out.println(aChb+"aachchbb");
        t.y=t1.x.subtract(aChb);
        return t;
	   }
 

		
	public static void jiaMi(File inputfile,File outputfile) throws IOException{ 
	
		sushu aSushu=new sushu();
		aSushu=aSushu.Init2();
		System.out.println(aSushu.p);
		System.out.println(aSushu.q);
		
		System.out.println(outputfile.getAbsolutePath());
		
		byte[] inByte=new byte[100];
		byte[] outByte=new byte[500];
		byte[] mesByte=new byte[4];
		BigInteger c;
		
			
		FileInputStream input=null;
		FileOutputStream output=null;
		int i,j,k,len,s1,s2,s3;
		Integer s100,s10,s0;
		
		try{
			input=new FileInputStream(inputfile);
			output=new FileOutputStream(outputfile);
			
			for(k=0;k<100;k++)
			{
				inByte[k]=0;
			}
			
			while((i=input.read(inByte))!=-1)
			{
					
				if(i!=100)
				{
					byte[] myByte=new byte[i];
					for(k=0;k<i;k++)
					 myByte [k]=inByte[k];
					 c=new BigInteger(myByte);

				}
				else  
				c=new BigInteger(inByte);
				
				if((j=c.signum())<0)  
				{
					c=c.negate();
					c=c.modPow(aSushu.e,aSushu.n);//加密
					outByte=c.toByteArray();
				}
				
				if(j==0)
					outByte=inByte;	
				
				if(j>0)  
				{
					c=c.modPow(aSushu.e,aSushu.n);//加密
					outByte=c.toByteArray();
				}
				
				
				System.out.println("len::   "+ outByte.length);
			
				
				len=outByte.length;
				s1=len/100;
				s2=(len-s1*100)/10;
				s3=len%10;
				
				s100=new Integer(s1);
				s10=new Integer(s2);
				s0=new Integer(s3);
			
				
				mesByte[0]=s100.byteValue();
				mesByte[1]=s10.byteValue();
				mesByte[2]=s0.byteValue();
				if(j<0)
					mesByte[3]=1;
					else if(j==0) mesByte[3]=0;
						else mesByte[3]=2;
				
				System.out.println("j:       "+j);
					
				
				output.write(mesByte);
				output.write(outByte);
				

				for(k=0;k<100;k++)
				{
				inByte[k]=0;
				}
		
			}
				
		}
		catch(IOException e)
		{
			;
		}
		finally{
			 if(input!=null) try {input.close();}  catch(IOException e)  {;}
		     if(output!=null) try {output.close();}  catch(IOException e)  {;}
		}		
		
	}
		
	
	
	
	public static void jieMi(File inputfile,File outputfile,String s1,String s2) throws IOException{ 
	
		BigInteger dd=new BigInteger(s1);
		BigInteger nn=new BigInteger(s2);
		System.out.println("dd:    "+dd);
		
		System.out.println(inputfile.getAbsolutePath());
		
		byte[] mesByte=new byte[4];	
		byte[] inByte=new byte[500];
		byte[] outByte=new byte[100];
		
		FileInputStream input=null;
		FileOutputStream output=null;
		int i,len,j,s100,s10,s0,k;
		
		try{
			input=new FileInputStream(inputfile);
			output=new FileOutputStream(outputfile);
			
			while((i=input.read(mesByte))!=-1)
			{	
				
				j=mesByte[3];
				s100=mesByte[0];
				s10=mesByte[1];
				s0=mesByte[2];
				len=0;
				len=s100*100+s10*10+s0;
				
				byte[] myByte=new byte[len];
				
				System.out.println("j:::    "+j);

				
				i=input.read(inByte,0,len);
				
				for(k=0;k<len;k++)
					myByte[k]=inByte[k];
				
				System.out.println("lennn::::"+myByte.length);
				
				if(j!=0)
				{
					BigInteger c=new BigInteger(myByte);
					c=c.modPow(dd,nn);//解密;			
					if(j==1) c=c.negate();
					outByte=c.toByteArray();
				}
					else outByte=myByte;
				
				output.write(outByte);;
			}
			System.out.println("end jie mi");	
		}
		catch(IOException e)
		{
			;
		}
		finally{
			 if(input!=null) try {input.close();}  catch(IOException e)  {;}
		     if(output!=null) try {output.close();}  catch(IOException e)  {;}
		}		

	}
	
}

class set
	{
        BigInteger  x,y,d;
        public void set()
        {
        x=new BigInteger("0");
		y=new BigInteger("0");
		d=new BigInteger("0");
		}
		
	}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -