<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
在android设备上使用AES加密
?
?
?
基本和在PC上使用无区别
注意标红部分。
在PC上使用时,SecureRandom可以直接使用?new关键字创建
不需要指定provider?
系统会自动使用注册的第一个provider创建SecureRandom
?
在PC上使用时
系统提供的provider如下:
SUN?(DSA?key/parameter?generation;?DSA?signing;?SHA-1,?MD5?digests;?SecureRandom;?X.509?certificates;?JKS?keystore;?PKIX?CertPathValidator;?PKIX?CertPathBuilder;?LDAP,?Collection?CertStores,?JavaPolicy?Policy;?JavaLoginConfig?Configuration)
Sun?RSA?signature?provider
Sun?JSSE?provider(PKCS12,?SunX509?key/trust?factories,?SSLv3,?TLSv1)
SunJCE?Provider?(implements?RSA,?DES,?Triple?DES,?AES,?Blowfish,?ARCFOUR,?RC2,?PBE,?Diffie-Hellman,?HMAC)
Sun?(Kerberos?v5,?SPNEGO)
Sun?SASL?provider(implements?client?mechanisms?for:?DIGEST-MD5,?GSSAPI,?EXTERNAL,?PLAIN,?CRAM-MD5;?server?mechanisms?for:?DIGEST-MD5,?GSSAPI,?CRAM-MD5)
XMLDSig?(DOM?XMLSignatureFactory;?DOM?KeyInfoFactory)
Sun?PC/SC?provider
Sun's?Microsoft?Crypto?API?provider
?
会默认使用第一个provider
?
注意:android设备上系统内注册的provider??与?PC上完全不同???不同版本的android系统之间注册的provider也不同。请手动指定需要的provider。
Android?2.2以上默认使用?androidOpenSSL
Android?2.1及以下系统不支持?androidOpenSSL?
?
通过?Security.getProviders()?方法可以获取系统内注册的所有provider信息
?
?
?
package?dk.tools;
import?java.io.UnsupportedEncodingException;
import?java.security.InvalidKeyException;
import?java.security.NoSuchAlgorithmException;
import?java.security.Provider;
import?java.security.SecureRandom;
import?java.security.Security;
?
import?javax.crypto.BadPaddingException;
import?javax.crypto.Cipher;
import?javax.crypto.IllegalBlockSizeException;
import?javax.crypto.KeyGenerator;
import?javax.crypto.NoSuchPaddingException;
import?javax.crypto.SecretKey;
import?javax.crypto.spec.SecretKeySpec;
?
/**
?*?进行128位AES加密解密的工具类
?*?指定provider为BC
?*?
?*?此类未进行线程同步
?*
?*/
public?class?AESTools?{
/**
?*?默认加密/解密密码
?*/
private?String?password="com.inventec.dreye.dict.SRD00";
//KeyGenerator对象实例
private?KeyGenerator?kgen;
/**
?*?构造方法
?*?需消耗一定时间
?*/
public?AESTools(String?password)
{
if(password!=null?&&?password.trim().length()>0)
{
this.password=password;
}
}
/**
?*?构造方法
?*?需消耗一定时间
?*/
public?AESTools()
{
}
/**
?*?加密
[email protected]需加密字符串
[email protected]加密后的byte[]
?*/
?public?byte[]?encrypt(String?content)?{?????
????????try?{???
????????kgen?=?KeyGenerator.getInstance("AES");
????????Provider?p=Security.getProvider("BC");
????????SecureRandom?s=SecureRandom.getInstance("AES",?p);
????????s.setSeed(password.getBytes());
????????kgen.init(128,?s);//?使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。
????? SecretKey?secretKey?=?kgen.generateKey();
byte[]?enCodeFormat?=?secretKey.getEncoded();
SecretKeySpec?key?=?new?SecretKeySpec(enCodeFormat,?"AES");//?使用SecretKeySpec类来根据一个字节数组构造一个SecretKey
Cipher?cipher?=?Cipher.getInstance("AES/ECB/PKCS5Padding");//?创建密码器?。
byte[]?byteContent?=?content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE,?key);//?初始化
byte[]?result?=?cipher.doFinal(byteContent);?//执行操作
return?result;?//?加密
}?catch?(NoSuchAlgorithmException?e)?{
e.printStackTrace();
}?catch?(NoSuchPaddingException?e)?{
e.printStackTrace();
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}?catch?(IllegalBlockSizeException?e)?{
e.printStackTrace();
}?catch?(BadPaddingException?e)?{
e.printStackTrace();
}?catch?(InvalidKeyException?e)?{
e.printStackTrace();
}
return?null;
}
?
?/**
??*?解密
[email protected]需解密的byte[]
[email protected]解密后的byte[]
??*/
?public?byte[]?decrypt(byte[]?content)?{?????
????????try?{???
???????? ?kgen?=?KeyGenerator.getInstance("AES");
? ????????Provider?p=Security.getProvider("BC");
? ????????SecureRandom?s=SecureRandom.getInstance("AES",?p);
? ????????s.setSeed(password.getBytes());
? ????????kgen.init(128,?s);//?使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。
? ????????SecretKey?secretKey?=?kgen.generateKey();?????
????????????byte[]?enCodeFormat?=?secretKey.getEncoded();?????
????????????SecretKeySpec?key?=?new?SecretKeySpec(enCodeFormat,?"AES");?????????????????
????????????Cipher?cipher?=?Cipher.getInstance("AES/ECB/PKCS5Padding");//?创建密码器?????
????????????cipher.init(Cipher.DECRYPT_MODE,?key);//?初始化?????
????????????byte[]?result?=?cipher.doFinal(content);???
????????????System.out.println(bytesToHexString(result));
????????????return?result;???
????????}?catch?(NoSuchAlgorithmException?e)?{?????
????????????e.printStackTrace();?????
????????}?catch?(NoSuchPaddingException?e)?{?????
????????????e.printStackTrace();?????
????????}?catch?(InvalidKeyException?e)?{?????
????????????e.printStackTrace();?????
????????}?catch?(IllegalBlockSizeException?e)?{?????
????????????????e.printStackTrace();?????
????????}?catch?(BadPaddingException?e)?{?????
????????????????e.printStackTrace();?????
????????}?????
????????return?null;?????
????}????
?
?
?/**
??*?将byte[]转换为16进制显示
??*?注意:此方法是非线程安全的
??*/
?public?String?bytesToHexString(byte[]?src){???
?????StringBuilder?stringBuilder?=?new?StringBuilder("");???
?????if?(src?==?null?||?src.length?<=?0)?{???
?????????return?null;???
?????}???
?????for?(int?i?=?0;?i?<?src.length;?i++)?{???
?????????int?v?=?src[i]?&?0xFF;???
?????????String?hv?=?Integer.toHexString(v);???
?????????if?(hv.length()?<?2)?{???
?????????????stringBuilder.append(0);???
?????????}???
?????????stringBuilder.append(hv);???
?????}???
?????return?stringBuilder.toString().toUpperCase();???
?}??
// ?
// ?public?static?void?main(String[]?args)?throws?UnsupportedEncodingException
// ?{
// ?String?test="hello";
// ?AESTools?tools=new?AESTools();
// ?byte[]?ret=tools.encrypt(test);
// ?
// ?System.out.println(new?String(tools.decrypt(ret),"utf-8"));
// ?}
}