【转】RSA加密的填充模式

跟DES,AES一样, RSA也是一个块加密算法( block cipher algorithm),总是在一个固定长度的块上进行操作。

但跟AES等不同的是, block length是跟key length 以及所使用的填充模式 有关的。

1)RSA_PKCS1_PADDING 填充模式,最常用的模式

要求:
输入 必须 比 RSA 钥模长(modulus) 短至少11个字节, 也就是 RSA_size(rsa) – 11
如果输入的明文过长,必须切割, 然后填充

输出 和modulus一样长

根据这个要求,对于512bit的密钥, block length = 512/8 – 11 = 53 字节

2)  RSA_PKCS1_OAEP_PADDING
RSA_size(rsa) – 41

3)for RSA_NO_PADDING  不填充

文件: PKCS__1_v2.1_RSA_算法标准.pdf
大小: 838KB
下载: 下载


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用Java Cryptography Architecture(JCA)提供的API来生成RSA密钥对并进行加密解密。下面是使用JCA生成RSA密钥对并进行加密解密的示例代码: ```java import javax.crypto.Cipher; import java.security.*; public class RSAExample { private static final String ALGORITHM = "RSA"; private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding"; private static final int KEY_SIZE = 2048; public static void main(String[] args) throws Exception { // 生成密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM); keyPairGenerator.initialize(KEY_SIZE); KeyPair keyPair = keyPairGenerator.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 明文 String plainText = "Hello, world!"; // 加密 Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherText = cipher.doFinal(plainText.getBytes()); // 解密 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedText = cipher.doFinal(cipherText); // 输出结果 System.out.println("Plain text: " + plainText); System.out.println("Cipher text: " + new String(cipherText)); System.out.println("Decrypted text: " + new String(decryptedText)); } } ``` 在上面的示例代码中,使用`KeyPairGenerator`生成RSA密钥对,使用`Cipher`进行加密和解密。其中,`TRANSFORMATION`指定了加密算法和填充模式,`KEY_SIZE`指定了密钥长度。 需要注意的是,加密和解密时需要使用相同的填充模式和密钥对。此外,生成的密钥对需要妥善保管,私钥不应该泄露给他人。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值