热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

我有两种方法来从Android内部存储中的文件进行加密保存和解密加载对象。加密和保存过程已完成,没有任何问题,但是当我要加载对象StreamCorrup

我有两种方法来从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);

}



推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • [论文笔记] Crowdsourcing Translation: Professional Quality from Non-Professionals (ACL, 2011)
    Time:4hoursTimespan:Apr15–May3,2012OmarZaidan,ChrisCallison-Burch:CrowdsourcingTra ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • Android LED 数字字体的应用与实现
    本文介绍了一种适用于 Android 应用的 LED 数字字体(digital font),并详细描述了其在 UI 设计中的应用场景及其实现方法。这种字体常用于视频、广告倒计时等场景,能够增强视觉效果。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 本文详细探讨了在Android 8.0设备上使用ChinaCock的TCCBarcodeScanner进行扫码时出现的应用闪退问题,并提供了解决方案。通过调整配置文件,可以有效避免这一问题。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
author-avatar
扯淡的青春0707
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有