赞
踩
近日在做一个关于短信及文件加解密的小项目,查看了一些Android加解密方面的知识。关于加解密这部分以前完全没有接触过,所以网上乱翻了一天对于什么DES,AES,RSA,BASE64,MD5之类的还是懵懵懂懂,这里也就不再来说它们的原理了,实在是我自己也没弄懂。写这篇博客时也就大致了解了一下Cipher类,并实现了一个简单的AES编解码工具类,当然是用的都是些最简单的默认模式,更详细的知识还有待进一步的学习。 此博文主要是复习记录一下一天学习所得,加深印象。
Android的Cipher类其实是来自于JAVA的加密环境JCE,查看官方的文档就可以看到: javax.crypto.Cipher。当然据说这个类也不在官方的正式JDK里面,而且有些解密方法在Android中不可用(这部分不敢下结论,加密的东西我也刚刚开始了解)。下面就来具体看一下实现AES加密的过程吧。
Step1:实现一个简单XML文件做测试界面,实现一个EditText,两个Button,一个TextView显示结果:
- <EditText
- android:id="@+id/et_input"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn_encrypt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="加密"/>
- <Button
- android:id="@+id/btn_decrypt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="解密"/>
- <TextView
- android:id="@+id/tv_output"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>

Step2:Activity代码就不贴出来了,无非就是获得上面4个控件,然后监听加密、解密两个按钮执行加解密工具类的加解密方法。
Step3:实现加解密工具类,这也是此篇博文的关键。下面来一步一步的分析:
先来看看官方给出的加密大致流程:Use a secure random number generator, SecureRandom
, to initialize any cryptographic keys,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。