Android 加密解密
为什么80%的码农都做不了架构师?>>>
1.
public SecretKey secretKey;public byte[] encrypt;public KeyPair keyPair;public byte[] rsaEncrypt;/*** BASE 64** @param view*/public void base64encode(View view) {//文本编码base64encoder = BASE64.base64encoder(STR_BASE64);Log.e(TAG, base64encoder);}public void base64decode(View view) {if (base64encoder != null) {//文本解码String base64decode = BASE64.base64decode(base64encoder);Log.e(TAG, base64decode);}}/*** MD5s* @param view*/public void md5(View view) {byte[] md5 = MD5.getMD5(STR_MD5.getBytes());StringBuilder sb = new StringBuilder();for (byte b : md5) {sb.append(b);}Log.e(TAG, "md5:"+sb.toString());String md5Str = CommUtils.bytesConvertToHexString(md5);Log.e(TAG, "md5:"+md5Str);}/*** SHA* @param view*/public void sha(View view) {try {byte[] bytes = SHA.encodeSHA(STR_SHA.getBytes());byte[] bytes256 = SHA.encodeSHA256(STR_SHA.getBytes());byte[] bytes384 = SHA.encodeSHA384(STR_SHA.getBytes());byte[] bytes512 = SHA.encodeSHA512(STR_SHA.getBytes());String bytesStr = CommUtils.bytesConvertToHexString(bytes);String bytes256Str = CommUtils.bytesConvertToHexString(bytes256);String bytes384Str = CommUtils.bytesConvertToHexString(bytes384);String bytes512Str = CommUtils.bytesConvertToHexString(bytes512);Log.e(TAG,"SIZE:"+bytesStr.length()+" "+bytesStr+" length:"+bytes.length);Log.e(TAG,"SIZE:"+bytes256Str.length()+" "+bytes256Str+" length:"+bytes256.length);Log.e(TAG,"SIZE:"+bytes384Str.length()+" "+bytes384Str+" length:"+bytes384.length);Log.e(TAG,"SIZE:"+bytes512Str.length()+" "+bytes512Str+" length:"+bytes512.length);/* StringBuilder sb = new StringBuilder();for (byte b : bytes512) {sb.append(b);}Log.e(TAG,"SIZE:"+bytes512Str.length()+" "+bytes512Str+" length:"+sb.length());*/} catch (Exception e) {e.printStackTrace();}}/*** DES* @param view*/public void desInit(View view) {//初始化密钥try {secretKey = DES.initKey();} catch (Exception e) {e.printStackTrace();}}public void desEncrypt(View view) {//加密if (secretKey != null) {try {//第一个参数 字节数组,密钥encrypt = DES.encrypt(STR_DES.getBytes(), secretKey);String encryptStr = CommUtils.bytesConvertToHexString(encrypt);Log.e(TAG,"encryptStr:"+encryptStr);} catch (Exception e) {e.printStackTrace();}}else{Toast.makeText(this, "请初始化密钥", Toast.LENGTH_SHORT).show();}}public void desDecrypt(View view) {//解密if (secretKey != null) {try {byte[] decrypt = DES.decrypt(encrypt, secretKey);Log.e(TAG,"decryptStr:"+new String(decrypt));} catch (Exception e) {e.printStackTrace();}}else{Toast.makeText(this, "请初始化密钥", Toast.LENGTH_SHORT).show();}}/*** RSA* @param view*/public void rsaInit(View view) {//rsa 初始化try {keyPair = RSA.initKey();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}public void rsaEncrypt(View view) {if (keyPair != null) {//rsa 加密RSAPublicKey rsaPublicKey = RSA.getRSAPublicKey(keyPair); //获取公钥
// RSAPrivateKey rsaPrivateKey = RSA.getRSAPrivateKey(keyPair);try {rsaEncrypt = RSA.encrypt(STR_RSA.getBytes(), rsaPublicKey.getEncoded());
// rsaEncrypt = RSA.encrypt(STR_RSA.getBytes(), rsaPrivateKey.getEncoded());String encryptStr = CommUtils.bytesConvertToHexString(rsaEncrypt);Log.e(TAG,"encryptStr:"+encryptStr);} catch (Exception e) {e.printStackTrace();}}else{Toast.makeText(this, "请初始化密钥", Toast.LENGTH_SHORT).show();}}public void rsaDecrypt(View view) {//rsa 解密if (keyPair != null) {RSAPrivateKey rsaPrivateKey = RSA.getRSAPrivateKey(keyPair);
// RSAPublicKey rsaPublicKey = RSA.getRSAPublicKey(keyPair);try {byte[] decrypt = RSA.decrypt(rsaEncrypt, rsaPrivateKey.getEncoded());
// byte[] decrypt = RSA.decrypt(rsaEncrypt, rsaPublicKey.getEncoded());Log.e(TAG,"decrypt:"+new String(decrypt));} catch (Exception e) {e.printStackTrace();}}else{Toast.makeText(this, "请初始化密钥", Toast.LENGTH_SHORT).show();}}
2.BASE64
public class BASE64 {/**** BASE64编码** @param data 待编码的数据* @return 编码后的数据*/public static String base64encoder(String data) {byte[] b = Base64.encode(data.getBytes(), Base64.DEFAULT);return new String(b);}/*** Base64解码** @param data 待解码的数据* @return 解码后的数据*/public static String base64decode(String data) {byte[] b = Base64.decode(data.getBytes(), Base64.DEFAULT);return new String(b);}
}
3.DES
public class DES {//加密方式public final static String ALGORITHM = "DES";//密码,长度要是8的倍数 自定义private static String password = "12345678";private static SecureRandom random;
// private static SecretKey securekey;/*** 初始化密钥* @throws InvalidKeyException* @throws NoSuchAlgorithmException* @throws InvalidKeySpecException*/public static SecretKey initKey() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {random = new SecureRandom();DESKeySpec desKey = new DESKeySpec(password.getBytes());//创建一个密匙工厂,然后用它把DESKeySpec进行转换SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);SecretKey securekey = keyFactory.generateSecret(desKey);return securekey;}/*** DES加密** @param data //待加密的数据* @return //加密后的数据* @throws NoSuchAlgorithmException* @throws InvalidKeyException* @throws InvalidKeySpecException* @throws NoSuchPaddingException* @throws BadPaddingException* @throws IllegalBlockSizeException*/public static byte[] encrypt(byte[] data,SecretKey securekey) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {try{//Cipher对象实际完成加密操作Cipher cipher = Cipher.getInstance(ALGORITHM);//用密匙初始化Cipher对象 编码模式cipher.init(Cipher.ENCRYPT_MODE, securekey, random);//现在,获取数据并加密//正式执行加密操作return cipher.doFinal(data);}catch(Throwable e){e.printStackTrace();}return null;}/*** DES数据解密** @param data 待解密的数据* @throws NoSuchAlgorithmException* @throws InvalidKeyException* @throws InvalidKeySpecException* @throws NoSuchPaddingException* @throws BadPaddingException* @throws IllegalBlockSizeException*/public static byte[] decrypt(byte[] data,SecretKey securekey) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {// Cipher对象实际完成解密操作Cipher cipher = Cipher.getInstance(ALGORITHM);// 用密匙初始化Cipher对象 解码模式cipher.init(Cipher.DECRYPT_MODE, securekey, random);// 真正开始解密操作return cipher.doFinal(data);}
}
4.MD5
/*** 消息摘要* MD5算法是典型的消息摘要算法,其前身有MD2,MD3和,MD4算法。* 不论哪种MD算法,它们都需要获得以讹随 机长度的信息并产生以一个128位的信息摘要。* 如果将这个128位的信息摘要转换为十六进制,就可以得到一 个32位的字符串,* 因此我们见到的大部分MD5算法的指纹都是32位十六进制的字符串。* <p/>* 特点:消息摘要的主要特点是对同一段数据做多次摘要处理后,其摘要值完全一致。*/public class MD5 {/*** 获得数据的MD5编码** @param data 待处理的数据* @return 加密后的MD5值*/public static byte[] getMD5(byte[] data) {try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(data);return md.digest();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}}
5.RSA
public class RSA {/*** 算法名称*/public static final String KEY_ALGORITHM = "RSA"; //算法/*** RSA密钥长度* 默认为1024位* 密钥长度必须为64的倍数 * 范围在512~65536之间*/private static final int KEY_SIZE = 1024;/*** 生成密钥对** @return 密钥对对象* @throws NoSuchAlgorithmException*/public static KeyPair initKey() throws NoSuchAlgorithmException {//实例化密钥对生成器KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_ALGORITHM);//初始化密钥对生成器generator.initialize(KEY_SIZE);//生成密钥对return generator.generateKeyPair();}/*** 获得密钥对中公钥* @param pair 密钥对 ** @return*/public static RSAPublicKey getRSAPublicKey(KeyPair pair){RSAPublicKey key = (RSAPublicKey)pair.getPublic();//获得公钥编码byte[] encoded = key.getEncoded();return key;}/*** 获得密钥对中私钥* @param pair 密钥对 ** @return*/public static RSAPrivateKey getRSAPrivateKey(KeyPair pair){RSAPrivateKey key = (RSAPrivateKey)pair.getPrivate();//获得私钥编码byte[] encoded = key.getEncoded();return key;}/*** RSA加密* @param data 待解密的数据* @param key 公钥* @return 解密后的数据* @throws NoSuchAlgorithmException* @throws InvalidKeySpecException* @throws NoSuchPaddingException* @throws InvalidKeyException* @throws BadPaddingException* @throws IllegalBlockSizeException*/public static byte[] encrypt(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {//取得私钥X509EncodedKeySpec spec = new X509EncodedKeySpec(key);KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);//生成私钥PublicKey publicKey = factory.generatePublic(spec);//对数据加密Cipher cipher = Cipher.getInstance(factory.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);}/*** RSA解密** @param data 待解密的数据* @param key 私钥* @return 解密后的数据* @throws NoSuchAlgorithmException* @throws InvalidKeySpecException* @throws NoSuchPaddingException* @throws InvalidKeyException* @throws BadPaddingException* @throws IllegalBlockSizeException*/public static byte[] decrypt(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {//取得私钥PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(key);KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);//生成私钥PrivateKey privateKey = factory.generatePrivate(spec);//对数据解密Cipher cipher = Cipher.getInstance(factory.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);}}
6.SHA
/*** 消息摘要* SHA算法基于MD4算法基础之上,作为MD算法的继任者。* SHA与MD算法不同之处在于摘要长度,SHA算法的 摘要长度更长,安全性更高。*/public class SHA {/*** SHA-1消息摘要** @param data 待处理的数据* @return 消息摘要* @throws NoSuchAlgorithmException*/public static byte[] encodeSHA(byte[] data) throws NoSuchAlgorithmException {MessageDigest md = MessageDigest.getInstance("SHA");return md.digest(data);}/*** SHA-256消息摘要** @param data 待处理的数据* @return 消息摘要* @throws NoSuchAlgorithmException*/public static byte[] encodeSHA256(byte[] data) throws NoSuchAlgorithmException {MessageDigest md = MessageDigest.getInstance("SHA-256");return md.digest(data);}/*** SHA-384消息摘要** @param data 待处理的数据* @return 消息摘要* @throws NoSuchAlgorithmException*/public static byte[] encodeSHA384(byte[] data) throws NoSuchAlgorithmException {MessageDigest md = MessageDigest.getInstance("SHA-384");return md.digest(data);}/*** SHA-512消息摘要** @param data 待处理的数据* @return 消息摘要* @throws NoSuchAlgorithmException*/public static byte[] encodeSHA512(byte[] data) throws NoSuchAlgorithmException {MessageDigest md = MessageDigest.getInstance("SHA-512");return md.digest(data);}
}
7.工具类
public class CommUtils {/*** byte数组 转 16进制* @param bytes* @return*/public static String bytesConvertToHexString(byte[] bytes) {StringBuffer sb = new StringBuffer();for (byte aByte : bytes) {String s = Integer.toHexString(0xff & aByte);if (s.length() == 1) {sb.append("0" + s);} else {sb.append(s);}}return sb.toString();}
}
转载于:https://my.oschina.net/glfei/blog/2961201
Android 加密解密相关推荐
- android rsa加密工具类,GitHub - Lerist/encrypt: Android 加密解密工具包。
Encrypt(加密工具) 字符串,byte[],文件等对象的加密和解密工具集合,包含了多种加密方案. 加密类型 摘要 相关方法 简单加密 换一种编码格式 Base64Util 单向加密 只能加密,不 ...
- android加密字符串,Android 加密解密字符串详解
加密和解密的字符串: package eoe.demo; import java.security.SecureRandom; import javax.crypto.Cipher; import j ...
- java之php、Android、JAVA、C# 3DES加密解密
异常如下 1.javax.crypto.BadPaddingException: Given final block not properly padded 1)要确认下是否加密和解密都是使用相同的填 ...
- 【Android 安全】DEX 加密 ( Java 工具开发 | 加密解密算法 API | 编译代理 Application 依赖库 | 解压依赖库 aar 文件 )
文章目录 一.加密解密算法 API 二.编译代理 Application 依赖库 三.解压代理 Application 依赖库 aar 文件 参考博客 : [Android 安全]DEX 加密 ( 常 ...
- android、ios、php之间AES加密解密
使用原因: 因为在项目中,需要在与客户端(IOS,Android)交互的时候,保存一些私有信息,不被别人看到,所以,使用了比较流行的可以反向加解密的AES. PHP 源码 <?php$aes = ...
- Android AES 文件加密解密
几番折磨终有结果,现将Demo整理出来... [java] view plain copy package com.king.zjc; import java.io.File; import ja ...
- Android RSA加密解密
转载: http://blog.csdn.net/bbld_/article/details/38777491 概述 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素 ...
- Android RSA加密解密的 工具类的使用
RSA 比较特殊,我们首先要生成私钥和公钥,然后在加密的时候,使用私钥加密,在解密的时候使用公钥解密. //RSA 的初始化,获得私钥和密钥public void rsaInit(){try {Key ...
- android java加密_Android、iOS和Java通用的AES128加密解密示例代码
前言 移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如android和iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输. 这篇文章给大家分享AES的加密和解密. ...
最新文章
- 项目发布后 Tomcat中只有web-INF文件夹
- golang使用http client发起get和post请求示例
- PPC常用软件4/6(PPC播放rmvb)
- kettle基础入门(一)kettle下载、安装
- Win10在Dev-C++配置Npcap
- 【黄啊码】微信小程序+php实现即时通讯聊天功能
- 如何提高BT的下载速度?
- mac上的微信小助手WeChatPlugin
- 不允许sam账户和共享的匿名枚举_不允许SAM 帐户匿名枚举是什么意思?
- 《面向对话的融入交互信息的实体关系抽取》--中文信息学报
- python,你也和小猪佩奇一样社会了!
- javascript 关于年龄计算
- 如何使用阿里云服务器快速搭建个人网站?
- 浅谈从信息化到数字化时代下的业财一体化
- 亲爱的老狼-opacity、rgba、transparent的应用
- IT教育培训机构套路揭秘,这样的学校不能选,擦亮眼睛不上当
- php 微商城 开源,一款基于ThinkPHP3.2开发的微信O2O开源微商城系统_微信开源微商城WEMALL...
- 树莓派3B+日常使用记录
- JS -- 作用域、作用域链以及延长作用域链的方法
- 360流量监控独立版