0

再会!!!

我尝试使用 OMEMO 进行加密以使用 XMPP 进行聊天,但在进行实际加密时遇到了设备列表问题。

按照以下步骤,

  1. OEMO服务注册
  2. 生成的设备 ID
  3. 生成的指纹
  4. 信任 OEMO 身份
  5. 使用 OMEMO 管理器加密
  6. 使用 XMPP 连接对象发送消息。

    请检查下面的代码,请注意,我对存储文件一无所知,所以我创建了一个本地文件并在此处给出。我没有找到任何关于相同的文档。如果有人知道,也请让我知道。

       try {
    
            var messageToSend = tMessagetext!!.text.toString();
    
    
            var mFriendJID = "abc@localhost/yyy";
    
    
            val jidFriend = JidCreate.from(mFriendJID);
    
            val jidBare = JidCreate.bareFrom(mFriendJID);
    
            val msg = Message(jidFriend, Message.Type.chat);
    
            msg.setBody(messageToSend);
    
            SignalOmemoService.acknowledgeLicense();
            if (!SignalOmemoService.isServiceRegistered())
                SignalOmemoService.setup();
    
            val service = SignalOmemoService.getInstance()
    
            val file = File(Environment.getExternalStorageDirectory().absolutePath,"myAppFile.txt");
    
            OmemoConfiguration.setFileBasedOmemoStoreDefaultPath(file);
    
    
            val memomanager = OmemoManager.getInstanceFor(ChatService.xmppConnection)
    
            val  device = memomanager.ownDevice
    
    
            Log.e(TAG, device.deviceId.toString()+", "+device.jid);
            val fingerPrint = memomanager.getFingerprint(device)
    
    
            Log.e(TAG, fingerPrint.toString());
            memomanager.trustOmemoIdentity(device, fingerPrint);
    
            val encryptedMessage = memomanager!!.encrypt(jidBare, messageToSend)
    
            ChatService.xmppConnection!!.sendStanza(encryptedMessage)
            tMessagetext!!.text.clear();
    
        }catch (e :java.lang.Exception){
            e.printStackTrace()
        }
    

我得到的错误是,

OmemoService: Could not fetch device list of abc@localhost: org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPP error reply received from abc@localhost: XMPPError: service-unavailable - cancel
    org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPP error reply received from abc@localhost: XMPPError: service-unavailable - cancel
        at org.jivesoftware.smack.StanzaCollector.nextResultOrThrow(StanzaCollector.java:268)
        at org.jivesoftware.smack.StanzaCollector.nextResultOrThrow(StanzaCollector.java:219)
        at org.jivesoftware.smackx.pubsub.LeafNode.getItems(LeafNode.java:196)
        at org.jivesoftware.smackx.pubsub.LeafNode.getItems(LeafNode.java:189)
        at org.jivesoftware.smackx.pubsub.LeafNode.getItems(LeafNode.java:136)
        at org.jivesoftware.smackx.pubsub.PubSubManager.getLeafNodeProsodyWorkaround(PubSubManager.java:350)
        at org.jivesoftware.smackx.pubsub.PubSubManager.getLeafNode(PubSubManager.java:333)
        at org.jivesoftware.smackx.omemo.OmemoService.fetchDeviceListNode(OmemoService.java:435)
        at org.jivesoftware.smackx.omemo.OmemoService.fetchDeviceList(OmemoService.java:452)
        at org.jivesoftware.smackx.omemo.OmemoService.refreshDeviceList(OmemoService.java:504)
        at org.jivesoftware.smackx.omemo.OmemoService.buildOrCreateOmemoSessionsFromBundles(OmemoService.java:605)
        at org.jivesoftware.smackx.omemo.OmemoService.processSendingMessage(OmemoService.java:810)
        at org.jivesoftware.smackx.omemo.OmemoService.processSendingMessage(OmemoService.java:787)
        at org.jivesoftware.smackx.omemo.OmemoManager.encrypt(OmemoManager.java:209)
        at com.abcd.androidChatapp.MainActivity.onSendMessage(MainActivity.kt:347)
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6312)
        at android.view.View$PerformClick.run(View.java:24943)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:192)
        at android.app.ActivityThread.main(ActivityThread.java:6754)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

请让我知道此代码中的问题,以便我继续前进。让我知道您是否有关于相同的适当文档以便我可以深入了解此过程中到底发生了什么。

提前谢谢你。

4

1 回答 1

1

这是我使用的代码。

//Setup stream
SignalOmemoService.acknowledgeLicense();
SignalOmemoService.setup();

SignalOmemoService service = (SignalOmemoService) SignalOmemoService.getInstance();
service.setOmemoStoreBackend(new SignalCachingOmemoStore(new SignalFileBasedOmemoStore(mContext.getFilesDir())));

omemoManager = OmemoManager.getInstanceFor(getSafeConnection(), deviceId);
omemoManager.setTrustCallback(this);
omemoManager.addOmemoMessageListener(this);
omemoManager.addOmemoMucMessageListener(this);

//After login
omemoManager.purgeDeviceList();
omemoManager.initialize();

//send message
BareJid bareJid = JidCreate.bareFrom(jid);
OmemoMessage.Sent encMesage = omemoManager.encrypt(bareJid, msgText);
Message newMessage = encMesage.asMessage(bareJid);
xmppStream.sendMessage(newMessage);

你也需要检查这个

于 2020-03-19T14:48:01.807 回答