`
王者之剑
  • 浏览: 194105 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论

用Bouncy Castle实现AES-128-CBC加密解密

    博客分类:
  • java
阅读更多

Bouncy Castle Crypto APIs 是一个开源的轻量级Java 加密解密包,实现了JCE/JCA的provider,支持AES等多种加密解密算法。
详情请见主页:http://www.bouncycastle.org/java.html
本文的示例代码使用了http://www.bouncycastle.org/download/bcprov-jdk16-139.jar
1)使用JCE的AES-128-CBC加密解密

package com.albertsong.aes;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

/**
 * @author Albert
 * @version 1.0
 * 
 */
public class AESWithJCE {

    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
        byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38,
                0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 };
        String content ="TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        System.out.println("Original content:");
        System.out.println(content);
        try {
            Security.addProvider(new BouncyCastleProvider());
            Key key = new SecretKeySpec(keybytes, "AES");
            Cipher in = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
            in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] enc = in.doFinal(content.getBytes());
            System.out.println("Encrypted Content:");
            System.out.println(new String(Hex.encode(enc)));
            
            Cipher out = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
            out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            byte[] dec = out.doFinal(enc);
            System.out.println("Decrypted Content:");
            System.out.println(new String(dec));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

 2)不使用JCE的AES-128-CBC加密解密,可以用于J2ME程序中。

package com.albertsong.aes;

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Hex;

/**
 * @author Albert
 * @version 1.0
 *
 */
public class AESWithoutJCE {

    /**
     * @param args
     */
    public static void main(String[] args) {
        byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
                0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
        byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38,
                0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 };
        String content ="TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        System.out.println("Original content:");
        System.out.println(content);
        try {
            BufferedBlockCipher engine = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
            engine.init(true, new ParametersWithIV(new KeyParameter(keybytes),iv));
            byte[] enc = new byte[engine.getOutputSize(content.getBytes().length)];
            int size1 = engine.processBytes(content.getBytes(), 0, content.getBytes().length, enc, 0);
            int size2 = engine.doFinal(enc, size1);
            System.out.println("size2 ="+size2);
            byte[] encryptedContent =new byte[size1+size2];
            System.arraycopy(enc, 0, encryptedContent, 0, encryptedContent.length);
            System.out.println("Encrypted Content:");
            System.out.println(new String(Hex.encode(encryptedContent)));
            
            
            engine.init(false, new ParametersWithIV(new KeyParameter(keybytes),iv));
            byte[] dec = new byte[engine.getOutputSize(encryptedContent.length)];
            size1 = engine.processBytes(encryptedContent, 0, encryptedContent.length, dec, 0);
            size2 = engine.doFinal(dec, size1);
            System.out.println("size2 ="+size2);
            byte[] decryptedContent =new byte[size1+size2];
            System.arraycopy(dec, 0, decryptedContent, 0, decryptedContent.length);
            System.out.println("Decrypted Content:");
            System.out.println(new String(decryptedContent));

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

    }

}
 
5
0
分享到:
评论
6 楼 王者之剑 2009-01-06  
fantaxy025025 写道

好!我用的是:bcprov-jdk15-133.jar难道不行吗?

你这个版本比我的旧,可能不支持AES/CBC/PKCS7Padding这个算法,也有可能在旧版中名字不一样,我没有用过旧版的不清楚。
5 楼 fantaxy025025 2009-01-06  
好!
我用的是:bcprov-jdk15-133.jar
难道不行吗?
4 楼 王者之剑 2009-01-04  
fantaxy025025 写道

java.security.NoSuchAlgorithmException: No such algorithm: AES/CBC/PKCS7Padding我对加密解密了解不多,执行的时候出现错误,请问这是为啥?

提示的意思是找不到对于的加密算法。
你下载的是
http://www.bouncycastle.org/download/bcprov-jdk16-139.jar
这个吗?
3 楼 fantaxy025025 2009-01-04  
java.security.NoSuchAlgorithmException: No such algorithm: AES/CBC/PKCS7Padding

我对加密解密了解不多,执行的时候出现错误,请问这是为啥?
2 楼 王者之剑 2008-08-05  
不太懂你的意思,什么样的加密算法每次加密以后都不一样的?
1 楼 nyzzr 2008-08-05  
王者之剑:你好,我想请问一下,既然是AES-128-CBC加密,那为什么每次加密的结果都一样呀!!!

相关推荐

Global site tag (gtag.js) - Google Analytics