java安全,RSA算法。

本文介绍了一个Java加密解密的示例程序,演示了如何使用RSA和AES两种加密算法来实现数据的安全传输。该程序首先生成公钥和私钥,接着使用公钥加密AES密钥,并将加密后的密钥及加密的数据一同发送给接收方,接收方再使用私钥解密数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

接下来这个例子就是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对文件进行解密。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值