`
jkfzero
  • 浏览: 102685 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java安全,RSA算法。

阅读更多

一般加密原理是,由用户共有的公共密钥和传输数据的用户直接有的私有密钥组成。每次加密的时候,都是由一定算法随机生成一对密钥,用密钥加密所需加密的文件,用公钥加密密钥,然后将这些一起传输给目标用户,目标用户将数据处理后,又按照此密钥加密,然后传输回去。

这样的优点别人很难破解,网络上的数据无法定位,而且产生的密钥是随机的。

接下来这个例子就是java核心技术卷2高级特性安全性一章的最后一个例子。

import  java.io. * ;
import  java.security. * ;
import  javax.crypto. * ;
import  javax.crypto.interfaces. * ;
import  javax.crypto.spec. * ;
public   class  RSATest  ... {

    
private   static   final   int  KEYSIZE  =   512 ;

    
/** */ /**
     * 
@param  args
     
*/

    
public   static   void  main(String[] args)  ... {
        
//  TODO Auto-generated method stub
         try ... {
            
if (args[ 0 ].equals( " -genkey " )) ... {
                KeyPairGenerator keyPair 
=  KeyPairGenerator.getInstance( " RSA " );
                SecureRandom random 
=   new  SecureRandom();
                keyPair.initialize(KEYSIZE, random);
                KeyPair keyP 
=  keyPair.generateKeyPair();
                ObjectOutputStream oos 
=   new  ObjectOutputStream( new  FileOutputStream(args[ 1 ]));
                oos.writeObject(keyP.getPublic());
                oos.close();
                oos 
=   new  ObjectOutputStream( new  FileOutputStream(args[ 2 ]));
                oos.writeObject(keyP.getPrivate());
                oos.close();
            }
else   if (args[ 0 ].equals( " -encrypt " )) ... {
                KeyGenerator keygen 
=  KeyGenerator.getInstance( " AES " );
                SecureRandom random 
=   new  SecureRandom();
                keygen.init(random);
                SecretKey key 
=  keygen.generateKey();
                
                ObjectInputStream kin 
=   new  ObjectInputStream( new  FileInputStream(args[ 3 ]));
                Key publicKey 
=  (Key)kin.readObject();
                kin.close();
                
                Cipher cipher 
=  Cipher.getInstance( " RSA " );
                System.out.println(cipher.getAlgorithm());
                cipher.init(Cipher.WRAP_MODE,publicKey);
                
byte [] wrappedKey  =  cipher.wrap(key);
                DataOutputStream out 
=   new  DataOutputStream( new  FileOutputStream(args[ 2 ]));
                out.writeInt(wrappedKey.length);
                out.write(wrappedKey);
                
                InputStream in 
=   new  FileInputStream(args[ 1 ]);
                cipher 
=  Cipher.getInstance( " AES " );
                cipher.init(Cipher.ENCRYPT_MODE, key);
                crypt(in,out,cipher);
                in.close();
                out.close();
                
            }
else ... {
                DataInputStream in 
=   new  DataInputStream( new  FileInputStream(args[ 1 ]));
                
int  length  =  in.readInt();
                
byte  []wrappedKey  =   new   byte [length];
                in.read(wrappedKey,
0 ,length);
                
                ObjectInputStream keyIn 
=   new  ObjectInputStream( new  FileInputStream(args[ 3 ]));
                Key privateKey 
=  (Key) keyIn.readObject();
                keyIn.close();
                
                Cipher cipher 
=  Cipher.getInstance( " RSA " );
                cipher.init(Cipher.UNWRAP_MODE, privateKey);
                Key key 
=  cipher.unwrap(wrappedKey,  " AES " , Cipher.SECRET_KEY);
                
                OutputStream out 
=   new  FileOutputStream(args[ 2 ]);
                cipher 
=  Cipher.getInstance( " AES " );
                cipher.init(Cipher.ENCRYPT_MODE, key);
                crypt(in,out,cipher);
                in.close();
                out.close();
            }

        }
catch (Exception e) ... {
            e.printStackTrace();
        }

    }


    
private   static   void  crypt(InputStream in, OutputStream out, Cipher cipher) throws  IOException,GeneralSecurityException  ... {
        
//  TODO Auto-generated method stub
         int  blockSize  =  cipher.getBlockSize();
        
int  outputSize  =  cipher.getOutputSize(blockSize);
        
byte [] inBytes  =   new   byte [blockSize];
        
byte [] outputBytes  =   new   byte [outputSize];
        
int  inLength  =   0 ;
        
boolean  more  =   true ;
        
while (more) ... {
            inLength 
=  in.read(inBytes);
            
if (inLength  ==  blockSize) ... {
                
int  outLength  =  cipher.update(inBytes,  0 ,blockSize,outputBytes);
                out.write(outputBytes, 
0 , outLength);
            }
else  
                more 
=   false ;
        }

        
if (inLength > 0 )
            outputBytes 
=  cipher.doFinal(inBytes,  0 , inLength);
        
else
            outputBytes 
=  cipher.doFinal();
        out.write(outputBytes);
    }


}

 

运行的时候,先在eclipse里面运行的参数里加上-genkey public.key private.key

生成两个文件,就是用于加密的公钥和私钥。

然后运行的参数里面加上-encrypt textFile encryptedFile public.key对第二个参数对应的文件加密,第三个参数对应的就是生成的加密过的文件。

最后运行-decrypt encryptedFile decryptedFile private.key对文件进行解密。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics