我正在尝试接收一个字符串(消息)并对其进行加密,然后将其发送到firebase数据库。然后当应用程序加载数据时,应该向用户显示未加密的数据。所以加密的字符串位于服务器/数据库上。
我得到nul指针异常,因为它似乎在你使用快照时调用默认的消息构造函数,它不包含我调用的文本。实际上datasnapshot调用默认构造函数,它不允许我调用对象的属性实际的构造函数。反正有吗?
一旦应用程序请求将从数据库请求消息的方法。应用程序崩溃。我尝试在显示消息之前从字节串中解密消息...然而消息将不显示且应用程序崩溃
这是指向屏幕截图的链接
这是在登录成功时调用的方法,并从数据库请求消息并显示它们(这个工作直到我搞砸了)我想从DB读取并解密然后显示消息
//READ DECRYPT AND DISPLAY THE MESSAGES
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//called when new message is inserted into the list also
//triggered for every child message
EncryptoMessage encryptoMessage =
dataSnapshot.getValue(EncryptoMessage.class);
//get the text from the encryptomessage object store as string
String encryptedString = encryptoMessage.getText(); //value of
//encryptedString ends up empty
byte [] bytes = encryptedString.getBytes();
String str = null;
try {
str = new EncryptoMessage().decrypt(bytes);
} catch (Exception e) {
e.printStackTrace();
}
EncryptoMessage decryptedMessage = new EncryptoMessage();
decryptedMessage.setText(str);
mMessageAdapter.add(decryptedMessage);
}
这是来自用户输入的消息,加密并存储为DB中的字符串。我将btye []转换为字符串
//SEND CLICKED
// Send button sends a message and clears the EditText && Write to the
//Database
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: Send messages on click
//get the message the user enters
String messageText = mMessageEditText.getText().toString();
byte[] codedtext = new byte[0];
try {
//take that message and encrypt it
//codedtext now equal to byte array of messageText
codedtext = new EncryptoMessage().encrypt(messageText);
} catch (Exception e) {
e.printStackTrace();
}
//change the encrypted bytes into a string and send to the
//database
String str = new String(codedtext, StandardCharsets.UTF_8);
//Create an EncryptoMessage object for the message the user
//enters
EncryptoMessage encryptoMessage = new EncryptoMessage(str,
mUsername, null);
//add the new message object to the EncryptoChat database
mMsgDatabaseReference.push().setValue(encryptoMessage);
// Clear input box
mMessageEditText.setText("");
}
});
这是加密邮件对象类(加密)
package com.google.firebase.david.encryptochat; //firebase API
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
//**************************************
//THis class is our message object
//*****************************************
public class EncryptoMessage {
private String text;
private String name;
private String photoUrl;
public EncryptoMessage() {
// Default constructor required for calls to
//DataSnapshot.getValue(User.class)
}
// The encrypto message constructor to create the object
public EncryptoMessage(String text, String name, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
}
//Copy constuctor
/* public EncryptoMessage(EncryptoMessage EncryptoMessageCopy){
this(EncryptoMessageCopy.getText(), EncryptoMessageCopy.getName());
}*/
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
//Encryption of the messages
public byte[] encrypt(String message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
.getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
return cipherText;
}
//Decrypt
public String decrypt(byte[] message) throws Exception {
//get the bytes if the string passing in
//byte [] bytes = message.getBytes();
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
.getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher =
Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key, iv);
final byte[] plainText = decipher.doFinal(message);
return new String(plainText, "UTF-8");
}
}
这是我在运行时遇到的错误,当我登录并显示消息时它也会崩溃
FATAL EXCEPTION: main
Process: com.google.firebase.david.encryptochat, PID: 8733
java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference
at com.google.firebase.david.encryptochat.MainActivity$4.onChildAdded(MainActivity.java:238)