用Bouncy Castle实现AES_128_CBC在j2se和j2me上的加密解密

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加密解密(j2se)

 

 

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;

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");
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Java 使用 Bouncy Castle实现 GPG 加密的示例代码: ```java import java.io.*; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.util.*; import java.util.zip.*; import org.bouncycastle.bcpg.*; import org.bouncycastle.bcpg.attr.*; import org.bouncycastle.openpgp.*; import org.bouncycastle.openpgp.operator.*; import org.bouncycastle.openpgp.operator.bc.*; public class GpgEncrypt { public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); String publicKeyPath = "/path/to/public/key.asc"; String outputFilePath = "/path/to/output/file.gpg"; String inputFilePath = "/path/to/input/file.txt"; FileInputStream publicKeyInputStream = new FileInputStream(publicKeyPath); PGPPublicKey publicKey = readPublicKey(publicKeyInputStream); FileOutputStream outputStream = new FileOutputStream(outputFilePath); encryptFile(outputStream, inputFilePath, publicKey); outputStream.flush(); outputStream.close(); publicKeyInputStream.close(); } private static PGPPublicKey readPublicKey(InputStream inputStream) throws IOException, PGPException { PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(inputStream)); PGPPublicKeyRing publicKeyRing = (PGPPublicKeyRing) pgpObjectFactory.nextObject(); PGPPublicKey publicKey = publicKeyRing.getPublicKey(); if (publicKey == null) { throw new IllegalArgumentException("Public key not found in the key ring."); } return publicKey; } private static void encryptFile(OutputStream outputStream, String inputFilePath, PGPPublicKey publicKey) throws IOException, NoSuchProviderException, PGPException { PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP); PGPUtil.writeFileToLiteralData(compressedDataGenerator.open(outputStream), PGPLiteralData.BINARY, new File(inputFilePath)); JcePGPDataEncryptorBuilder dataEncryptorBuilder = new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256).setSecureRandom(new SecureRandom()); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptorBuilder); encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey)); OutputStream encryptedOutputStream = encryptedDataGenerator.open(compressedDataGenerator.open(outputStream), new byte[PGPUtil.BUFFER_SIZE]); compressedDataGenerator.close(); OutputStream armoredOutputStream = new ArmoredOutputStream(encryptedOutputStream); armoredOutputStream.flush(); armoredOutputStream.close(); encryptedOutputStream.close(); } } ``` 该代码使用 Bouncy Castle 库中的类实现了 GPG 的加密功能,具体实现方式如下: 1. 读取公钥:从指定的文件中读取 GPG 公钥,将其转换为 `PGPPublicKey` 实例。 2. 加密文件:使用 `PGPCompressedDataGenerator` 将要加密的文件进行 Zip 压缩,然后使用 `PGPEncryptedDataGenerator` 和 `BcPublicKeyKeyEncryptionMethodGenerator` 将压缩后的数据加密并写入输出流中。 3. 输出加密数据:使用 `ArmoredOutputStream` 将加密数据转换为 ASCII 码格式,并将其写入输出流中。 通过以上步骤,即可实现 GPG 加密并输出加密数据。同时,解密时也需要使用 Bouncy Castle 库中的类进行解密操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值