Fix decrypting messages

This commit is contained in:
Paul Schaub 2018-05-24 16:01:33 +02:00
parent 653f318d37
commit 365a4d20d0
3 changed files with 30 additions and 9 deletions

View File

@ -77,11 +77,6 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
public BouncyCastleOpenPgpProvider(BareJid ourJid) throws IOException, PGPException, NoSuchAlgorithmException {
this.ourJid = ourJid;
PGPSecretKeyRing ourKey = generateKey(ourJid).generateSecretKeyRing();
ourKeyId = ourKey.getPublicKey().getKeyID();
ourKeys = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
ourKeys.addSecretKey(ourKey.getSecretKey().getEncoded());
ourKeys.addPublicKey(ourKey.getPublicKey().getEncoded());
}
@Override
@ -177,14 +172,20 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
InMemoryKeyring newKeyring = KeyringConfigs.forGpgExportedKeys(
KeyringConfigCallbacks.withUnprotectedKeys());
newKeyring.addSecretKey(secretKey.getEncoded());
newKeyring.addPublicKey(secretKey.getPublicKey().getEncoded());
newKeyring.addSecretKey(secretKey.getEncoded());
ourKeys = newKeyring;
ourKeyId = secretKey.getKeyID();
InMemoryKeyring theirKeyRing = KeyringConfigs.forGpgExportedKeys(
KeyringConfigCallbacks.withUnprotectedKeys());
theirKeyRing.addPublicKey(secretKey.getPublicKey().getEncoded());
theirKeys.put(ourJid, theirKeyRing);
}
} catch (PGPException | IOException e) {
e.printStackTrace();
throw new CorruptedOpenPgpKeyException(e);
}
}
@ -358,8 +359,9 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
decryptionConfig.addPublicKey(p.getPublicKey().getEncoded());
}
ByteArrayInputStream encryptedIn = new ByteArrayInputStream(
element.getEncryptedBase64MessageContent().getBytes(Charset.forName("UTF-8")));
byte[] b64decoded = Base64.decode(element.getEncryptedBase64MessageContent());
ByteArrayInputStream encryptedIn = new ByteArrayInputStream(b64decoded);
InputStream decrypted = BouncyGPG.decryptAndVerifyStream()
.withConfig(decryptionConfig)
@ -385,6 +387,19 @@ public class BouncyCastleOpenPgpProvider implements OpenPgpProvider {
}
}
@Override
public void createAndUseKey() throws CorruptedOpenPgpKeyException, NoSuchAlgorithmException {
try {
PGPSecretKeyRing ourKey = generateKey(ourJid).generateSecretKeyRing();
ourKeyId = ourKey.getPublicKey().getKeyID();
ourKeys = KeyringConfigs.forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
ourKeys.addSecretKey(ourKey.getSecretKey().getEncoded());
ourKeys.addPublicKey(ourKey.getPublicKey().getEncoded());
} catch (PGPException | IOException e) {
throw new CorruptedOpenPgpKeyException(e);
}
}
public static PGPKeyRingGenerator generateKey(BareJid owner) throws NoSuchAlgorithmException, PGPException {
PGPKeyRingGenerator generator = BouncyGPG.createKeyPair()
.withRSAKeys()

View File

@ -49,6 +49,9 @@ public class BouncyCastleOpenPgpProviderTest extends SmackTestSuite {
BouncyCastleOpenPgpProvider aliceProvider = new BouncyCastleOpenPgpProvider(alice);
BouncyCastleOpenPgpProvider cheshireProvider = new BouncyCastleOpenPgpProvider(cheshire);
aliceProvider.createAndUseKey();
cheshireProvider.createAndUseKey();
// dry exchange keys
PubkeyElement aliceKeys = aliceProvider.createPubkeyElement();
PubkeyElement cheshireKeys = cheshireProvider.createPubkeyElement();

View File

@ -16,6 +16,7 @@
*/
package org.jivesoftware.smackx.ox;
import java.security.NoSuchAlgorithmException;
import java.util.Set;
import org.jivesoftware.smackx.ox.element.CryptElement;
@ -156,4 +157,6 @@ public interface OpenPgpProvider {
SecretkeyElement createSecretkeyElement(String password) throws CorruptedOpenPgpKeyException;
void restoreSecretKeyElement(SecretkeyElement secretkeyElement, String password) throws CorruptedOpenPgpKeyException;
void createAndUseKey() throws CorruptedOpenPgpKeyException, NoSuchAlgorithmException;
}