java序列化SealedObject是否出错?
所以对于一个Android项目,我需要在NFC标签上读写。当我尝试写和读纯文本记录时,我的NFCReader代码工作得很好。但是,为了使系统安全,我尝试用Cipher
密封SealedObject
中的TagProfile
对象,然后序列化该SealedObject
以将其写入NFC标记。为了读取标记,我尝试反序列化SealedObject
,然后从中获取TagProfile
对象
编写代码似乎工作得很好。但是,当我尝试读取标记时,我得到以下错误:java.io.StreamCorruptedException: invalid stream header: 02656EAC
我想知道我是写错了还是读错了数据。我认为它和序列化的关系比和加密的关系更大。我已经查找了这个错误,但是我找不到一个与抛出的头相同的示例。我找到了this,但我对输入和输出都使用ByteArrayStream和ObjectStreams。在任何情况下,下面是我的èncryptProfile()
和decryptProfile()
方法,它们也执行序列化。前者在创建要写入的NdefRecord
时调用,后者在从NdefRecord
有效负载构建TagProfile
时调用
private byte[] encryptProfile(TagProfile tagProfile) throws InvalidKeyException, IOException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, key);
SealedObject sealedProfile = new SealedObject(tagProfile, cipher);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(sealedProfile);
oos.flush();
return bos.toByteArray();
}
...
private TagProfile decryptProfile(byte[] bytes) throws IOException, ClassNotFoundException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
SealedObject sealedObject = (SealedObject) ois.readObject();
cipher.init(Cipher.DECRYPT_MODE, key);
return (TagProfile) sealedObject.getObject(cipher);
}
当我在decryptProfile()
执行实例并初始化ObjectInputStream ois
时,会引发错误。Cipher
和Key
对象是全局实例化的
编辑:我正在添加用于将NdefMessage
写入标记并从中构建TagProfile
的代码。使用纯文本可以很好地工作(尽管实现略有不同)
// ---------- WRITE
public void writeToTag(TagProfile profile, Tag tag) throws IOException, FormatException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException {
/* Plain text implementation:
NdefRecord[] records = {
createRecord(profile.id),
createRecord(profile.name),
...
};
NdefMessage message = new NdefMessage(records);
*/
NdefRecord[] encryptedRecord = {
createRecord(profile)
};
NdefMessage encryptedMessage = new NdefMessage(encryptedRecord);
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(encryptedMessage);
ndef.close();
}
private NdefRecord createRecord(TagProfile tagProfile) throws IOException, NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
byte[] profileBytes = encryptProfile(tagProfile);
String lang = "en";
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int bytesLength = profileBytes.length;
byte[] payload = new byte[1 + langLength + bytesLength];
payload[0] = (byte) langLength;
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(profileBytes, 0, payload, 1 + langLength, bytesLength);
NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload);
return recordNFC;
}
// ---------- READ
private TagProfile buildProfile(NdefMessage[] msgs) throws EmptyTagException, ImproperTagException{
try {
if (msgs == null || msgs.length == 0) throw new EmptyTagException();
String[] records = new String[msgs[0].getRecords().length];
if(records.length == tagLength) { // tagLength is the number of records I expect.
return decryptProfile(msgs[0].getRecords()[0].getPayload());
} else throw new ImproperTagException();
} catch (Exception e) {
e.printStackTrace();
return new TagProfile();
}
}
共 (0) 个答案