java cipheroutputstream 解密到对象_Java:使用DES解密对象时发生StreamCorruptedException...

我有两种方法来从Android内部存储中的文件进行加密保存和解密加载对象。

加密和保存过程已完成,没有任何问题,但是当我要加载对象StreamCorruptedException时,inputStream = new

ObjectInputStream(cipherInputStream);

我搜索的次数越来越多,但没有找到解决问题的方法。所有其他解决方案都是为了延长插座寿命或类似的。

我的代码如下:

private static byte[] iv = { (byte) 0xB1, (byte) 0x15, (byte) 0xB5,

(byte) 0xB7, (byte) 0x66, (byte) 0x43, (byte) 0x2F, (byte) 0xA4,

(byte) 0xB1, (byte) 0x15, (byte) 0x35, (byte) 0xC7, (byte) 0x66,

(byte) 0x58, (byte) 0x2F, (byte) 0x5F };

保存方法:(工作正常)

private static String saveToFile(Serializable object, String fileName,

Context ctx) {

try {

Cipher cipher = null;

cipher = Cipher.getInstance("DES");

SecretKey key = KeyGenerator.getInstance("DES").generateKey();

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

SealedObject sealedObject = null;

sealedObject = new SealedObject(object, cipher);

CipherOutputStream cipherOutputStream = null;

FileOutputStream fos = ctx.openFileOutput(fileName,

Context.MODE_PRIVATE);

cipherOutputStream = new CipherOutputStream(

new BufferedOutputStream(fos), cipher);

ObjectOutputStream outputStream = null;

outputStream = new ObjectOutputStream(cipherOutputStream);

outputStream.writeObject(sealedObject);

outputStream.close();

return "Save Complete!";

} catch (IOException e) {

e.printStackTrace();

return e.getMessage();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

return e.getMessage();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

return e.getMessage();

} catch (InvalidKeyException e) {

e.printStackTrace();

return e.getMessage();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

return e.getMessage();

} catch (InvalidAlgorithmParameterException e) {

e.printStackTrace();

return e.getMessage();

}

}

加载方法:(无法从中加载对象cipherInputStream)

private static Serializable loadFromFile(String fileName, Context ctx) {

Cipher cipher = null;

Serializable userList = null;

try {

cipher = Cipher.getInstance("DES");

// Code to write your object to file

SecretKey key = KeyGenerator.getInstance("DES").generateKey();

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

CipherInputStream cipherInputStream = null;

FileInputStream fos = ctx.openFileInput(fileName);

cipherInputStream = new CipherInputStream(new BufferedInputStream(

fos), cipher);

ObjectInputStream inputStream = null;

inputStream = new ObjectInputStream(cipherInputStream);

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SealedObject sealedObject = null;

sealedObject = (SealedObject) inputStream.readObject();

userList = (Serializable) sealedObject.getObject(cipher);

inputStream.close();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

return e.getMessage();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

return e.getMessage();

} catch (InvalidKeyException e) {

e.printStackTrace();

return e.getMessage();

} catch (InvalidAlgorithmParameterException e) {

e.printStackTrace();

return e.getMessage();

} catch (FileNotFoundException e) {

e.printStackTrace();

return e.getMessage();

} catch (StreamCorruptedException e) {

e.printStackTrace();

return e.getMessage();

} catch (IOException e) {

e.printStackTrace();

return e.getMessage();

} catch (ClassNotFoundException e) {

e.printStackTrace();

return e.getMessage();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

return e.getMessage();

} catch (BadPaddingException e) {

e.printStackTrace();

return e.getMessage();

}

return userList;

}

公开的保存和加载方法:

public Serializable loadPlayer(Context ctx) {

return loadFromFile("player.dat", ctx);

}

public String savePlayer(Player player, Context ctx) {

return saveToFile(player, "player.dat", ctx);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值