使用Base64编码的CRT组件解密使用RSA编码的Base64字符串

时间:2014-10-29 09:17:55

标签: java encryption cryptography rsa

所以我发现自己必须通过API解密给我的字符串。我知道它已被Base64编码,然后用RSA密钥加密(虽然我不知道它的顺序是什么)。我已经获得了模数,公共和私人指数以及素数。下面的代码是我的(截断的)尝试,它给我一个" javax.crypto.BadPaddingException:解密错误"在最后一行。不知道从哪里开始,任何帮助将不胜感激:)

import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import javax.crypto.Cipher;


public static final String RSAKeyFactory = "RSA";
public static final String RSAKeyAlgorithm = "RSA/ECB/PKCS1Padding";
public static final String UTF_8 = "UTF-8";

byte[] mod = Base64.getDecoder().decode("3dlF3Frvwmuet+gM/LX8EQBI...");
BigInteger modulus = new BigInteger(1,mod);

byte[] prive64 = Base64.getDecoder().decode("YwTJhmqOS58PJzhhvuREI...");
BigInteger privExp = new BigInteger(1,prive64);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(new RSAPrivateKeySpec(modulus, privExp));

byte[] cipherBytes = Base64.getDecoder().decode("VdznCyJeYukJahmHsbge...");
Cipher cipher = Cipher.getInstance(RSAKeyAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainData = cipher.doFinal(cipherBytes);

干杯

0 个答案:

没有答案