Android 加密解密字符串详解

加密和解密的字符串:


代码如下:

package eoe.demo;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Usage:
* <pre>
* String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
* ...
* String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
* </pre>
* @author ferenc.hechler
*/
public class SimpleCrypto {
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));
}
}

(0)

相关推荐

  • Android TextView字体颜色设置方法小结

    本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

  • android开发教程之framework增加字符串资源和图片等resource资源

    增加String 在Android frameworks/base/core/res/res/values中增加String,举例来说在string.xml增加一个String 复制代码 代码如下: <string name="serviceEnabledFor">Service was enabled for:</string> 也需要在 frameworks/base/core/res/res/values的public.xml文件中增加一个 复制代码 代码

  • Android编程实现TextView字体颜色设置的方法小结

    本文实例讲述了Android编程实现TextView字体颜色设置的方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. 复制代码 代码如下: tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android t

  • Android中判断字符串中必须包含字母或者数字

    public static boolean isLetterDigit(String str){ boolean isDigit = false;//定义一个boolean值,用来表示是否包含数字 boolean isLetter = false;//定义一个boolean值,用来表示是否包含字母 for(int i=0 ; i if(Character.isDigit(str.charAt(i))){ //用char包装类中的判断数字的方法判断每一个字符 isDigit = true; } i

  • Android 字符串中某个字段可点击和设置颜色的方法

    在android开发中,我们时常会遇到对字符串中某些固定的字段实现可点击和颜色的设置,现粘贴处我在开发中如何设置这些属性的. 代码如下: private TextView mContactNone; public void showRequestFailInviteRecord(){ String mRefresh = "系统开小差,请尝试刷新一下"; //创建 SpannableString 对象 SpannableString mStyledText = new Spannable

  • Android字符串转Ascii码实例代码

    复制代码 代码如下: package com.my.page;public class StringToAscii { private static String toHexUtil(int n){        String rt="";        switch(n){        case 10:rt+="A";break;        case 11:rt+="B";break;        case 12:rt+="C

  • Android字符串资源文件format方法使用实例

    很多时候我们感性Google在设计Android时遵守了大量MVC架构方式,可以让写公共代码.美工和具体逻辑开发人员独立出来.有关Android 的资源文件values/strings.xml中如何实现格式化字符串呢? 这里Android123举个简单的例子,以及最终可能会用到哪些地方. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <resources> <string name=

  • TextVie获取显示字符串的宽度之Android开发

    此文通过判断textview要显示的字符串的宽度是否超过我设定的宽度,若超过则执行换行,具体代码讲解如下: 项目中的其他地方也有这样的需求,故直接使用了那一块的代码. public float getTextWidth(Context Context, String text, int textSize){ TextPaint paint = new TextPaint(); float scaledDensity = Context.getResource().getDisplayMetric

  • Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法

    本文实例讲述了Android获取手机屏幕宽高.状态栏高度以及字符串宽高信息的方法.分享给大家供大家参考.具体如下: 首先定义TextView对象commentText 获取文字的宽高: TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize(commentText.getTextSize()); textPaint.setColor(Color.WHITE); FontMetrics fo

  • Android字符串和十六进制相互转化出现的中文乱码问题

    废话不读说了,直接给大家贴代码了,代码附有注释,可以说明一切,本文写的不好,还请见谅. import java.io.ByteArrayOutputStream; /** * Created by Administrator on 2016/2/2. * -----------16进制和字符串互转--------- * ------------解决中文乱码问题--------- */ public class StringToSixthUtils { private static String

随机推荐