使用Bouncy Castle实现RSA加密

使用RSA/ECB/PKCS1Padding

package iie.ac.cn.asymmetric_encryption.RSA_ECB_PKCS1Padding;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;

import java.math.BigInteger;
import java.security.SecureRandom;

/**
 * RSAEngine实例使用默认的填充模式:即RSA/ECB/PKCS1Padding
 */
public class RSAECBPKCS1PaddingBouncyCastleExample {

    public static void example() throws Exception {

        RSAKeyPairGenerator rsaKeyPairGenerator = new RSAKeyPairGenerator();
        SecureRandom random = new SecureRandom();
        RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(
                BigInteger.valueOf(65537), // public exponent
                random,                     // secure random generator
                2048,                       // key length
                80                          // certainty
        );
        rsaKeyPairGenerator.init(params);

        AsymmetricCipherKeyPair asymmetricCipherKeyPair = rsaKeyPairGenerator.generateKeyPair();
        AsymmetricKeyParameter publicKey = asymmetricCipherKeyPair.getPublic();
        AsymmetricKeyParameter privateKey = asymmetricCipherKeyPair.getPrivate();
        // Get the public key

        // Generate some plaintext data to encrypt
        byte[] plaintext = "Hello, world!".getBytes("UTF-8");

        // Create an instance of the RSA engine
        RSAEngine rsa = new RSAEngine();

        // Encrypt the plaintext data using RSA/ECB/PKCS1Padding
        rsa.init(true, publicKey);
        byte[] ciphertext = rsa.processBlock(plaintext, 0, plaintext.length);

        // Print out the encrypted ciphertext in hex format
        System.out.println(javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
    }
}

OAEP填充

package iie.ac.cn.asymmetric_encryption.RSA_ECB_OAEPPadding;

import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.encodings.OAEPEncoding;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;

import java.math.BigInteger;
import java.security.*;


public class RSAECBOAEPPaddingBouncyCastleExample {
    public static void example() throws Exception {

        SecureRandom random = new SecureRandom();
        RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(
                BigInteger.valueOf(65537), // public exponent
                random,                     // secure random generator
                2048,                       // key length
                80                          // certainty
        );
        RSAKeyPairGenerator rsaKeyPairGenerator = new RSAKeyPairGenerator();
        rsaKeyPairGenerator.init(params);
        AsymmetricCipherKeyPair asymmetricCipherKeyPair = rsaKeyPairGenerator.generateKeyPair();
        AsymmetricKeyParameter publicKey = asymmetricCipherKeyPair.getPublic();
        AsymmetricKeyParameter privateKey = asymmetricCipherKeyPair.getPrivate();

        AsymmetricBlockCipher rsaEngine = new RSAEngine();
        OAEPEncoding oaepEncoding = new OAEPEncoding(rsaEngine);

        rsaEngine.init(true, publicKey);


        // Encrypt plaintext data using OAEP encoding
        String plaintext = "Hello, world!";
        byte[] input = plaintext.getBytes("UTF-8");
        oaepEncoding.init(true, publicKey);
        byte[] encryptedData = oaepEncoding.processBlock(input, 0, input.length);

        // Decrypt ciphertext data using OAEP encoding
        oaepEncoding.init(false, privateKey);
        byte[] decryptedData = oaepEncoding.processBlock(encryptedData, 0, encryptedData.length);

        // Print results
        System.out.println("Original text: " + plaintext);
        System.out.println("Encrypted text: " + new String(encryptedData, "UTF-8"));
        System.out.println("Decrypted text: " + new String(decryptedData, "UTF-8"));

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值