如何为XMPP android的聊天节实现OMEMO加密
Posted
技术标签:
【中文标题】如何为XMPP android的聊天节实现OMEMO加密【英文标题】:How to implement OMEMO encryption for chat stanza of XMPP android 【发布时间】:2020-01-23 12:22:14 【问题描述】:美好的一天!!!
我尝试使用 OMEMO 进行加密以使用 XMPP 进行聊天,但在进行实际加密时遇到了设备列表问题。
按照以下步骤,
-
OMEMO 服务注册
生成的设备 ID
生成的指纹
信任 OMEMO 身份
使用 MEMO 管理器加密
使用 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)
请让我知道此代码中的问题,以便我继续前进。让我知道您是否有关于相同的适当文档,以便我可以深入了解此过程中到底发生了什么。
提前谢谢你。
【问题讨论】:
您能否获取应用发送和接收的 XMPP 流量日志?没有它就很难知道发生了什么。 【参考方案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);
另外你需要检查this
【讨论】:
以上是关于如何为XMPP android的聊天节实现OMEMO加密的主要内容,如果未能解决你的问题,请参考以下文章