使用java实现对称加密解密(AES),非对称加密解密(RSA)

对称加密:双方采用同样的秘钥进行加密和解密。特点是速度快,但是安全性没有非对称加密高

非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有秘钥,使用它加密后发送给发送方。

(对称加密和非对称加密都是可逆加密,像SHA,MD4,MD5都是不可逆加密,通常用来检验数据在传输的过程中是否被修改)

AES,RSA加密及解密代码如下:

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;


public class DigestTest{
    public static void main(String[] args){
        try{
            AESEncodeTest();
            RSAEncodeTest();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * AES对称加密 
     * @throws Exception
     */
    private static void AESEncodeTest() throws Exception{
        String content = "content";
        String key = "key"; //可以将该key存入本地文件,相当于一个秘钥。这个秘钥是不会通过网络传播的。
        //加密
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128, new SecureRandom(key.getBytes()));
        SecretKey secretKey = kg.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
        Cipher enCipher = Cipher.getInstance("AES");
        enCipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] result = enCipher.doFinal(content.getBytes());
        System.out.println("AES加密后:" + new HexBinaryAdapter().marshal(result));
        //解密
        //keyGenerator可以重复使用,但是每次使用前都需要调用init方法
        kg.init(128, new SecureRandom(key.getBytes()));
        SecretKeySpec dekeySpec = new SecretKeySpec(kg.generateKey().getEncoded(),"AES");
        Cipher deCipher = Cipher.getInstance("AES");
        deCipher.init(Cipher.DECRYPT_MODE, dekeySpec);
        System.out.println("AES解密后:" + new String(deCipher.doFinal(result)));
    }
    
    /**
     * RSA非对称加密</br>
     * 接收方生成的publicKey公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密</br>
     * 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有key,使用它加密后发送给发送方
     */
    private static void RSAEncodeTest() throws Exception{
        String content = "content";
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024, new SecureRandom());
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        
        //这儿模拟接收方生成了公、私有key。这些key可以存到文件里,要使用的时候读取出来即可
        Key privateKey = keyPair.getPrivate();
        Key publicKey = keyPair.getPublic();
        
        //这儿模拟发送方已经获取了接收方给的publicKey,并且使用它来加密要发送的数据
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(content.getBytes());
        System.out.println("RSA加密后:" + new HexBinaryAdapter().marshal(result));
        
        //这儿模拟接收方使用自己生成的私钥进行解密
        Cipher deCipher = Cipher.getInstance("RSA");
        deCipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] deResult = deCipher.doFinal(result);
        System.out.println("RSA解密后:" + new String(deResult));
    }
}

 

posted @ 2015-03-22 18:44  huliangbin  阅读(1272)  评论(0编辑  收藏  举报