加密算法使用(三):用用BASE64

采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到

package testEncrypt;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.KeyGenerator;

import org.apache.commons.codec.binary.Base64;
//import sun.misc.BASE64Encoder;
public class TestEncrypt {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecureRandom random = new SecureRandom();
        keygen.init(random);
        Key key = keygen.generateKey();
        
        //1,使用commons-codec.jar
        String key64Str = Base64.encodeBase64String(key.getEncoded());
        System.out.println(key64Str);
        
        //2、使用不被推荐的sun.misc.BASE64Encoder
        //key64Str = (new BASE64Encoder()).encodeBuffer(key.getEncoded()); 
        //System.out.println(key64Str);
        //Base64.decodeBase64(key64Str);

    }

}

 

posted @ 2016-04-26 15:22  剑握在手  阅读(311)  评论(0编辑  收藏  举报
返回顶部↑