如何使用 Virgil Security SDK 在 android firebase 应用程序中添加端到端加密

How to add end-to-end encryption in android firebase app with Virgil Security SDK

请帮帮我!在查看 Virgil Security E3Kit 文档后,我一直在尝试在我的 Android 聊天应用程序中包含一些代码,但无济于事。实际上,我不知道把那些示例代码片段放在哪里。

  1. 我的应用有一个 setup/login activity,用户在其中进行身份验证 在他们可以聊天之前。 Firebase 身份验证是这样的:

    public void registerUser(final String email, final String password){
        if(email.equals("") || password.equals("")){
            Toast.makeText(getBaseContext(), "Please enter email and/or password",
                    Toast.LENGTH_LONG).show();
        }else{
            //auth is already initialized somewhere
            auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
    
                        FirebaseUser user = auth.getCurrentUser();
                        Intent intent = new Intent(SetupActivity.this,
                                MoreActivity.class);
                        intent.putExtra("email", email);
                        intent.putExtra("userID", user.getUid());
                        startActivity(intent);
                    }else{
                        Toast.makeText(SetupActivity.this, "Error logging in, try again", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }
    

我的问题是我不知道 where/how 将 Virgil 文档中处理用户注册的部分添加到上述方法中。

  1. 用户认证后,他们可以轻松地与他们的联系人聊天。同样,这里有一个问题,因为我不知道聊天的初始化和加密应该放在哪里 - 在 onCreate, onStart etc 方法或其他地方。

这是来自 ChatActivity 的 onCreate 方法,它将用户输入的聊天记录插入 Firebase 数据库:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        Toolbar toolbar = (Toolbar) findViewById(R.id.chatToolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //What to do on back clicked
                onSupportNavigateUp();
            }
        });

        //Get receiver name from Intent extras
        Intent intent = getIntent();
        receiverName = intent.getStringExtra("receiverName");
        toolbar.setTitle(receiverName);

        database = FirebaseDatabase.getInstance();
        reference = database.getReference("chats");

        toolbar.setSubtitle(receiverPhone);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);

        //Firebase instance and user details
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        phone = user.getPhoneNumber();

        userName = user.getDisplayName();
        userId = user.getUid();
        userPhotoUrl = user.getPhotoUrl();

        //Get widgets
        newChat = (EditText) findViewById(R.id.chatMessage);
        receiverMsg = (TextView) findViewById(R.id.receiverMessage);
        myMsg = (TextView) findViewById(R.id.myMessage);

        dateAdded = (TextView) findViewById(R.id.dateAdded);
        receivedDate = (TextView) findViewById(R.id.receivedDate);

        myPicText = (ImageView) findViewById(R.id.myPicture);
        receiverPicText = (ImageView) findViewById(R.id.receiverPicture);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = newChat.getText().toString();
                Chat chat = new Chat(userId, message, receiverID, "");
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mma");
                String date = dateFormat.format(Calendar.getInstance(
                        TimeZone.getDefault()).getTime());

                chat.setTime(date);
                DatabaseReference ref = reference.push();

                ref.setValue(chat);
                newChat.getText().clear();
            }
        });
    }

我的问题是加密部分,因为 Firebase 身份验证和聊天实现已经完美运行。

纳义.

e3kit 初始化和用户注册应该在使用 firebase 进行身份验证后立即进行。用户注册只发生一次,最好是在注册时进行,并在每次登录时初始化。在您的代码中,它会在 startActivity(intent);.

之前发生

e3kit 加密应该在您构建 Chat 对象之前进行,因此在 Chat chat = new Chat(userId, message, receiverID, ""); 之前确保您进行了 message = eThree.encrypt(message, publicKeys);。当然,解密会在您收到加密消息后发生。

如果您需要更多详细信息,可以查看此示例:https://github.com/VirgilSecurity/virgil-e3kit-kotlin/blob/master/samples/android-java-firebase-function/app/src/main/java/com/android/virgilsecurity/ethreesamplejavafirebase/EThreeActivity.java

此外,另一个获得支持的好地方是 Virgil Security 的 slack 社区:https://virgilsecurity.com/join-community