RSA -Entschlüsselungsfehler: Eingabe für RSA -Chiffre mit Bouncycastle zu großJava

Java-Forum
Anonymous
 RSA -Entschlüsselungsfehler: Eingabe für RSA -Chiffre mit Bouncycastle zu groß

Post by Anonymous »

Derzeit haben wir Migration von Java 1.8 nach Java 11 durchgeführt. Während der Migration werden wir unter dem Problem der PGP -Nachricht in einer der Anwendungen entschlüsselt. mit Bouncycastle
In dieser Codezeile

Code: Select all

clear = pbe.getDataStream (b);
< /code>
Ich habe einen Polstermechanismus ausprobiert, wie in mehreren Websiteen erwähnt. Es hat nicht trainiert. < /P>
Die vollständige Klasse ist unten angegeben. Entschuldigung bei Codierung/Tippfehler. Nicht in der Lage, den tatsächlichen Code hier aufgrund der Zugriffsbeschränkung einzufügen. Ich habe hier eingestellt, um den Code hier zu schreiben und zu kleben. Korrigierte größte Fehler. Auch dieser Code funktioniert einwandfrei für die Java 1.8 -Version, aber als wir zu Java 11 migriert sind, stehen wir vor diesem Problem.  < /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:

Code: Select all

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post