Android AES加密算法,现在实际上
昨天,老板让我来看看android加密算法。于是在网上找了找,发现AES加密算法。(当然,MD5,BASE64什么http://snowolf.iteye.com/blog/379860这篇文章列举了非常多。可是基本都是j2se平台的。android平台不一定支持,可是AES算法Android是自带了包的,从官方的http://developer.android.com/reference/javax/crypto/Cipher.html能够看到。
)
AES加密算法是什么?大家能够自己去google,专家级程序猿写好包,project人员会用即可了。
这个样例事实上是来自http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml。
src文件夹主文件:
package com.qq;
import java.security.SecureRandom;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;public class SimpleCrypto extends Activity {public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] result = encrypt(rawKey, cleartext.getBytes()); return toHex(result); } public static String decrypt(String seed, String encrypted) throws Exception { byte[] rawKey = getRawKey(seed.getBytes()); byte[] enc = toByte(encrypted); byte[] result = decrypt(rawKey, enc); return new String(result); } private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } public static String toHex(String txt) { return toHex(txt.getBytes()); } public static String fromHex(String hex) { return new String(toByte(hex)); } public static byte[] toByte(String hexString) { int len = hexString.length()/2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue(); return result; } public static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2*buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final static String HEX = "0123456789ABCDEF"; private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f)); } /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);String masterPassword = "a";String originalText = "0123456789";byte[] text = new byte[]{'0','1','2','3','4','5','6','7','8','9'};byte[] password = new byte[]{'a'};try {String encryptingCode = SimpleCrypto.encrypt(masterPassword,originalText);
// System.out.println("加密结果为 " + encryptingCode);Log.i("加密结果为 ",encryptingCode);String decryptingCode = SimpleCrypto.decrypt(masterPassword, encryptingCode);System.out.println("解密结果为 " + decryptingCode);Log.i("解密结果",decryptingCode);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
布局文件main.xml和配置文件AndroidManifest.xml默认就好了。
最后结果在Log里面看,利用adb logcat > d:\1.txt定位到D盘的1.txt文件。然后用记事本打开就,查找”加密结果为“就能够看到了:
执行结果(从log日志中看到的):
I/加密结果 ( 190): BFB77D8F1E1EE9D5E252926A12659DE8
I/解密结果( 190): 0123456789
转载于:https://www.cnblogs.com/mengfanrong/p/4562652.html
Android AES加密算法,现在实际上相关推荐
- Android AES加密算法及事实上现
昨天老大叫我看看android加密算法.于是网上找了找,找到了AES加密算法.(当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了 ...
- Android AES加密算法及其实现
找到了AES加密算法.(当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了很多,但是基本都是j2se平台的,android平台不一 ...
- AES加密算法原理及java android实现
AES当今最流行的对称加密算法之一,是DES的替代者. 密钥是AES算法实现加密和解密的根本.对称加密算法之所以对称,是因为这类算法对明文的加密和解密需要使用同一个密钥. AES支持三种长度的密钥: ...
- Android AES 文件加密解密
几番折磨终有结果,现将Demo整理出来... [java] view plain copy package com.king.zjc; import java.io.File; import ja ...
- Android AES加密解密
AES加密算法模式有四种:ECB.CBC.CFB.OFB 要想AES加密,至少需要一个16位的密钥,如果是非ECB模式的加密,至少还得需要密钥偏移量. 下面是AES在线加密解密链接: AES在线加密解 ...
- AES加密算法及逆向
AES加密算法逆向以及特征识别 1.AES加密算法 关于AES加密算法,网上资料特别特别多,个人认为可以从这几点去学习: AES加密算法设计者的相关论文以及其他相关论文(如安全性.性能.硬件/指令实现 ...
- AES加密算法原理及python实现
AES加密算法原理及python实现 AES对称加密算法 1.Rijndael的设计思想 2.AES的基本结构 3.加密解密的详细结构 4.四种轮操作 1.字节代换(SubBytes) 2.行移位操作 ...
- Android AES加密解密工具类
一个用于Android AES加密解密的工具类,记录一下... import android.os.Build import android.security.keystore.KeyGenParam ...
- 什么是AES加密?详解AES加密算法原理流程
在密码学中,加密算法分为双向加密和单向加密.单向加密包括MD5.SHA等摘要算法,它们是不可逆的.双向加密包括对称加密和非对称加密,对称加密包括AES加密.DES加密等.双向加密是可逆的,存在密文的密 ...
最新文章
- 源码里throw new RuntimeException(“Stub!“)什么意思
- 网络编程学习笔记(recvmsg和sendmsg函数)
- 【机器学习】周志华 读书笔记 第一章 绪论
- Linux下启动和关闭Oracle数据库的方法
- 【SPFA】桐人的约会
- UI设计灵感|购物界面更有吸引力
- pathinfo函数获取非UTF-8字符集文件名的问题
- 基于Protostuff的通用序列化、反序列化功能实现
- 当浏览器版本过低时提示升级浏览器
- Linux替换压缩包下的文件,Linux下rar及zip压缩包中批量替换某文件脚本
- 周华健机器人演唱会_周华健伦敦花旦演唱会:歌声悠悠流过梦一样人生
- 金融工程 | 远期与期货概述
- 《道德经》全文 (马王堆出土帛书版)
- 安卓ps2模拟器_RetroArch 1.9.0 for windows/mac/ios/Android【全平台主机游戏模拟器】
- UE4 DataAsset 的使用
- 英语语法之句子成分(造句)
- 启发式与元启发式算法
- 在数据库中能不能像用excel那样直接填表保存进数据库,而不是用SQL语句
- 工具类五合一小程序源码星座配对+星座运势+恶搞放屁音效+引流工具+流量主
- SOEM 源码解析 ecx_map_sii
热门文章
- CList添加出错AddTail();rror C2664: 'struct __POSITION *__thiscall CList错误
- centos6.8 yum mysql7_Centos6.8通过yum安装mysql5.7
- mysql command type_mysql command line client 使用命令
- mysql系统云结构图_腾讯梁定安解密织云系统!(附架构图)
- dalsa线扫相机调试文档_线阵相机调试文档
- phpthink 隐藏index.php,nginx 配置--支持phpthink框架 path_info
- log4j 打印线程号配置_log4j配置参数
- 查询ecshop网站代码排查方法_功能测试——查询测试
- IDEA 系列安装资料及教程
- java数组split_js数组接受split(java split)