В настоящее время мы проходили миграцию с Java 1.8 в Java 11. Во время миграции мы сталкиваемся ниже дешифтируя проблему сообщения PGP в одном из приложений. с Bouncycastle
На этой строке кода
clear = pbe.getDataStream (b);
< /code>
Я попробовал некоторый механизм заполнения, как упомянуто на нескольких веб -сайте. Это не тренировалось. < /P>
Полный класс приведен ниже. Приносим извинения в случае проблемы кодирования/опечатки. Не в состоянии вставить здесь фактический код из -за ограничения доступа. Я сделал изображение, чтобы текстовать и вставить код здесь. Исправлены в самых ошибках. Также этот код отлично работает для версии Java 1.8, но когда мы мигрировали на Java 11, мы сталкиваемся с этой проблемой. < /P>
public class TestDecrypt {
private static final Logger LOGGER = LogManager.getLogger(TestDecrypt.class);
public TestDecrypt() {
}
public static String decryptContent(String input, String publicKey, String keystoreFile, String keystoreCred, String fileName) {
System.out.println("read :"+input );
LOGGER.info("[Starting Decryption for {} ] ", fileName);
String output = "";
try {
InputStream in = PGPUtil.getDecoderStream(new ByteArrayInputStream(input.getBytes()));
// System.out.println("read :"+in );
Throwable var7 = null;
try {
File publicKeyObject = new File(publicKey);
File privateKeyObject = new File(keystoreFile);
PGPBean pgpBean = new PGPBean(publicKeyObject, privateKeyObject, keystoreCred);
Security.insertProviderAt(new BouncyCastleProvider(),1);
Security.addProvider(new BouncyCastleProvider());
PGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
Object o = pgpF.nextObject();
PGPEncryptedDataList enc;
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList)o;
} else {
enc = (PGPEncryptedDataList)pgpF.nextObject();
}
Iterator
it = enc.getEncryptedDataObjects();
PGPPrivateKey sKey = null;
PGPPublicKeyEncryptedData pbe;
for(pbe = null; sKey == null && it.hasNext(); sKey = pgpBean.getPgpPrivateKey()) {
pbe = (PGPPublicKeyEncryptedData)it.next();
}
if (sKey == null) {
throw new IllegalArgumentException("Secret key for message not found.");
}
if (Security.getProvider("BC") == null) {
Security.insertProviderAt(new BouncyCastleProvider(), 0);
LOGGER.info("Security provider added successfully 1");
}
PublicKeyDataDecryptorFactory b = (new JcePublicKeyDataDecryptorFactoryBuilder()).setProvider("BC").setContentProvider("BC").build(sKey);
if (Security.getProvider("BC") == null) {
Security.insertProviderAt(new BouncyCastleProvider(), 0);
LOGGER.info("Security provider added successfully");
}
InputStream clear = pbe.getDataStream(b);
LOGGER.info("dd :"+clear.toString());
PGPObjectFactory plainFact = new JcaPGPObjectFactory(clear);
Object message = plainFact.nextObject();
if (message instanceof PGPCompressedData) {
PGPCompressedData cData = (PGPCompressedData)message;
PGPObjectFactory pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
message = pgpFact.nextObject();
}
if (!(message instanceof PGPLiteralData)) {
if (message instanceof PGPOnePassSignatureList) {
throw new PGPException("Encrypted message contains a signed message - not literal data.");
}
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
PGPLiteralData ld = (PGPLiteralData)message;
InputStream unc = ld.getInputStream();
output = (String)((Stream)(new BufferedReader(new InputStreamReader(unc))).lines().parallel()).collect(Collectors.joining("\n"));
if (pbe.isIntegrityProtected() && !pbe.verify()) {
throw new PGPException("Message failed integrity check");
}
LOGGER.info("[Completed Decryption for {} ] ", fileName);
} catch (Throwable var31) {
var7 = var31;
throw var31;
} finally {
if (in != null) {
if (var7 != null) {
try {
in.close();
} catch (Throwable var30) {
var7.addSuppressed(var30);
}
} else {
in.close();
}
}
}
} catch (Exception var33) {
LOGGER.error("[Exception occurred while decrypting {} ] ", fileName);
LOGGER.error("Exception has been caught : ", var33);
output = "FAILURE";
}
return output;
}
Stacktrace:
15:01:08,025[main] ERROR(TestDecrypt.java:141) - Exception has been caught :
org.bouncycastle.openpgp.PGPException: exception decrypting session data
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.decryptSessionData(Unknown Source) ~[bcpg-jdk18on-1.78.1.jar:?]
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder.access$100(Unknown Source) ~[bcpg-jdk18on-1.78.1.jar:?]
at org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder$2.recoverSessionData(Unknown Source) ~[bcpg-jdk18on-1.78.1.jar:?]
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getSessionKey(Unknown Source) ~[bcpg-jdk18on-1.78.1.jar:?]
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source) ~[bcpg-jdk18on-1.78.1.jar:?]
at com.ourcompany.test.util.service.TestDecrypt.decryptContent(TestDecrypt.java:98) ~[test-utility/:?]
at com.ourcompany.test.util.service.TestDecrypt.main(TestDecrypt.java:257) ~[test-utility/:?]
Caused by: org.bouncycastle.crypto.DataLengthException: input too large for RSA cipher.
at org.bouncycastle.crypto.engines.RSACoreEngine.convertInput(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at org.bouncycastle.crypto.engines.RSABlindedEngine.processBlock(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CustomPKCS1Encoding.decodeBlock(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CustomPKCS1Encoding.processBlock(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.getOutput(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineDoFinal(Unknown Source) ~[bcprov-jdk18on-1.78.1.jar:?]
at javax.crypto.Cipher.doFinal(Cipher.java:2083) ~[?:?]
... 7 more
Подробнее здесь: https://stackoverflow.com/questions/794 ... uncycastle
Ошибка дешифрования RSA: ввод слишком большой для шифра RSA с Bouncycycastle ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ошибка дешифрования RSA: ввод слишком большой для шифра RSA с Bouncycycastle
Anonymous » » в форуме JAVA - 0 Ответы
- 21 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Ошибка дешифрования RSA: ввод слишком большой для шифра RSA с Bouncycycastle
Anonymous » » в форуме JAVA - 0 Ответы
- 15 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Дешифруя с помощью шифра потока с использованием той же клавиши шифра потока
Anonymous » » в форуме Python - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Почему мой код дешифрования RSA вызывает исключение «ошибка переполнения»
Anonymous » » в форуме Python - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-