java 采用RSA加密算法 采用的填充方式为:RSA/ECB/OAEPPadding

public static String decrypt(String str, String privateKey) throws Exception{
    //64位解码加密后的字符串
    byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
    //base64编码的私钥
    byte[] decoded = Base64.decodeBase64(privateKey);
    RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
    //RSA解密
    Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
    cipher.init(Cipher.DECRYPT_MODE, priKey);
    String outStr = new String(cipher.doFinal(inputByte));
    return outStr;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个常见的RSA加密算法的实现。RSA是一种非对称加密算法,它使用公钥加密、私钥解密或者私钥加密、公钥解密来实现数据的保护。在这个算法中,ECB是块加密模式,OAEP填充方式,SHA-256是散列算法。MGF1是一种掩码生成功能,用于保护数据的完整性。 在Java中,我们可以使用JCE(Java Cryptography Extension)提供的RSA API来实现RSA加密解密。实现步骤大体如下: 1. 生成RSA密钥对:使用KeyPairGenerator,设置密钥长度和随机数生成器等参数,生成公钥和私钥。 2. 加密:使用Cipher,设置加密模式和填充方式,使用公钥对数据进行加密。 3. 解密:使用Cipher,设置解密模式和填充方式,使用私钥对加密数据进行解密。 下面是Java代码示例: ```java import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.util.*; import java.io.*; public class RSAExample { public static void main(String[] args) { try { // 生成RSA密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048, new SecureRandom()); KeyPair keyPair = keyGen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 加密 Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] plaintText = "This is a test message".getBytes("UTF-8"); byte[] cipherText = cipher.doFinal(plaintText); // 解密 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedText = cipher.doFinal(cipherText); // 输出结果 System.out.println("Original plain text: " + new String(plaintText)); System.out.println("Encrypted cipher text: " + new String(cipherText)); System.out.println("Decrypted plain text: " + new String(decryptedText)); } catch (Exception ex) { ex.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值