Thank you guys for the wonderful work. we now use Olm to encrypt&decrypt our API in java, the encrypt session is working fine, but we have an error that happens on the decrypt method call:
/**
* Basic test:
* - alice creates an account
* - bob creates an account
* - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey)
* - alice encrypts a message with its session
* - bob creates an inbound session based on alice's encrypted message
* - bob decrypts the encrypted message with its session
*/
@Test
public void test01AliceToBob() {
String plainMessage = "Hello Bob, this is message from Alice.";
LOGGER.info("plain message: {}", plainMessage);
String decryptedMessage = "";
String bobIdentityKey = null;
String bobOneTimeKey = null;
Account aliceAccount = null;
Account bobAccount = null;
Session aliceSession = null;
Session bobSession = null;
try {
// ALICE & BOB ACCOUNTS CREATION
aliceAccount = new Account();
bobAccount = new Account();
bobIdentityKey = bobAccount.identityKeys().getCurve25519(); // generate identityKey
/**
* generate oneTimeKey
*/
bobAccount.generateOneTimeKeys(5);
Map<String, String> bobOneTimeKeys = bobAccount.oneTimeKeys().getCurve25519();
for (String key : bobOneTimeKeys.keySet()) {
bobOneTimeKey = bobOneTimeKeys.get(key);
LOGGER.info("OneTimeKeys: {} = {}", key, bobOneTimeKey);
}
LOGGER.info("identityKey = {}, oneTimeKey = {}", bobIdentityKey, bobOneTimeKey);
// create OutboundSession to Bob
aliceSession = Session.createOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey);
Message encryptedMessage = aliceSession.encrypt(plainMessage);
LOGGER.info("encrypted message: {}", JSONUtils.toJSONString(encryptedMessage));
LOGGER.info("encrypted message type: {}", encryptedMessage.type());
bobSession = Session.createInboundSession(bobAccount, encryptedMessage.getCipherText());
decryptedMessage = bobSession.decrypt(encryptedMessage);
LOGGER.info("decrypted message: {}", decryptedMessage);
aliceAccount.clear();
bobAccount.clear();
aliceSession.clear();
bobSession.clear();
} catch (Exception e) {
LOGGER.error("Olm session test error: ", e);
}
Assert.assertEquals(plainMessage, decryptedMessage);
}
2021-11-11 16:34:38.772 [main] INFO net.metachain.server.utils.TestOlmSession - plain message: Hello Bob, this is message from Alice.
2021-11-11 16:34:39.517 [main] INFO net.metachain.server.utils.TestOlmSession - OneTimeKeys: AAAABQ = +MkagVTW7bGjgM1Os9WC7r8VaQHNiPS643Tt1VOPM0A
2021-11-11 16:34:39.518 [main] INFO net.metachain.server.utils.TestOlmSession - OneTimeKeys: AAAABA = yOhxoS8gxy9M4SeoB77GfUyR17RIEE8/zHd6Ysfv4yA
2021-11-11 16:34:39.518 [main] INFO net.metachain.server.utils.TestOlmSession - OneTimeKeys: AAAAAw = 4sXwydQ8rNknUubMavJj4zIOhxEuLHetd1DtcZRn3no
2021-11-11 16:34:39.518 [main] INFO net.metachain.server.utils.TestOlmSession - OneTimeKeys: AAAAAg = UdlPlxp07nNdvd39bD202NTy899Ehn6RE/yGY27/0X8
2021-11-11 16:34:39.518 [main] INFO net.metachain.server.utils.TestOlmSession - OneTimeKeys: AAAAAQ = A7Gjemi/vAdTyWq8HARzRyQsSkOQYag9dMpmraUS5SE
2021-11-11 16:34:39.519 [main] INFO net.metachain.server.utils.TestOlmSession - identityKey = NOC83AGZLQI8K1YySs0iZgTS1Y4bXee4ISuD23DmJis, oneTimeKey = A7Gjemi/vAdTyWq8HARzRyQsSkOQYag9dMpmraUS5SE
2021-11-11 16:34:39.616 [main] INFO net.metachain.server.utils.TestOlmSession - encrypted message: {"cipherText":"AwogA7Gjemi/vAdTyWq8HARzRyQsSkOQYag9dMpmraUS5SESIGJEyhTLzbn/dktZgrQiFexgE5JoYhkrugBEkeqHwnI4GiD/XW0taj3ihcNuvnqrKzPIgVMO6MLBSGyUpdgCPPs8ESJfAwogPgA6/b48L+Z0kqS94BD5GuZM5vhen/b75Z9/J7MRo1IQACIw1uuyj9I+gRI6SUvLuojfC4JIxjFiD1936FdFfsvQy1DGE9wn1+Yl75OaKy5ZX4EGlY3zJ+8dKWw"}
2021-11-11 16:34:39.617 [main] INFO net.metachain.server.utils.TestOlmSession - encrypted message type: 0
2021-11-11 16:34:39.621 [main] ERROR net.metachain.server.utils.TestOlmSession - Olm session test error:
io.github.brevilo.jolm.Utils$OlmException: BAD_MESSAGE_FORMAT
at io.github.brevilo.jolm.Session.checkOlmResult(Session.java:268)
at io.github.brevilo.jolm.Session.checkOlmResult(Session.java:254)
at io.github.brevilo.jolm.Session.decrypt(Session.java:219)
at net.metachain.server.utils.TestOlmSession.test01AliceToBob(TestOlmSession.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
org.junit.ComparisonFailure:
Expected :Hello Bob, this is message from Alice.