zl程序教程

您现在的位置是:首页 >  工具

当前栏目

AndroidAES加密工具类分享

工具加密 分享
2023-06-13 09:15:29 时间

1、AES加密工具类

java不支持PKCS7Padding,只支持PKCS5Padding。我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AES加密,本篇文章使用PKCS5Padding加密方式。

packagecom.example.aesdemo;

importjava.io.UnsupportedEncodingException;
importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;

///**AES对称加密解密类**/
publicclassAESHelper{

///**算法/模式/填充**/
privatestaticfinalStringCipherMode="AES/ECB/PKCS5Padding";

///**创建密钥**/
privatestaticSecretKeySpeccreateKey(Stringpassword){
byte[]data=null;
if(password==null){
password="";
}
StringBuffersb=newStringBuffer(32);
sb.append(password);
while(sb.length()<32){
sb.append("0");
}
if(sb.length()>32){
sb.setLength(32);
}

try{
data=sb.toString().getBytes("UTF-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
returnnewSecretKeySpec(data,"AES");
}

///**加密字节数据**/
publicstaticbyte[]encrypt(byte[]content,Stringpassword){
try{
SecretKeySpeckey=createKey(password);
System.out.println(key);
Ciphercipher=Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]result=cipher.doFinal(content);
returnresult;
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}

///**加密(结果为16进制字符串)**/
publicstaticStringencrypt(Stringcontent,Stringpassword){
byte[]data=null;
try{
data=content.getBytes("UTF-8");
}catch(Exceptione){
e.printStackTrace();
}
data=encrypt(data,password);
Stringresult=byte2hex(data);
returnresult;
}

///**解密字节数组**/
publicstaticbyte[]decrypt(byte[]content,Stringpassword){
try{
SecretKeySpeckey=createKey(password);
Ciphercipher=Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]result=cipher.doFinal(content);
returnresult;
}catch(Exceptione){
e.printStackTrace();
}
returnnull;
}

///**解密16进制的字符串为字符串**/
publicstaticStringdecrypt(Stringcontent,Stringpassword){
byte[]data=null;
try{
data=hex2byte(content);
}catch(Exceptione){
e.printStackTrace();
}
data=decrypt(data,password);
if(data==null)
returnnull;
Stringresult=null;
try{
result=newString(data,"UTF-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
returnresult;
}

///**字节数组转成16进制字符串**/
publicstaticStringbyte2hex(byte[]b){//一个字节的数,
StringBuffersb=newStringBuffer(b.length*2);
Stringtmp="";
for(intn=0;n<b.length;n++){
//整数转成十六进制表示
tmp=(java.lang.Integer.toHexString(b[n]&0XFF));
if(tmp.length()==1){
sb.append("0");
}
sb.append(tmp);
}
returnsb.toString().toUpperCase();//转成大写
}

///**将hex字符串转换成字节数组**/
privatestaticbyte[]hex2byte(StringinputString){
if(inputString==null||inputString.length()<2){
returnnewbyte[0];
}
inputString=inputString.toLowerCase();
intl=inputString.length()/2;
byte[]result=newbyte[l];
for(inti=0;i<l;++i){
Stringtmp=inputString.substring(2*i,2*i+2);
result[i]=(byte)(Integer.parseInt(tmp,16)&0xFF);
}
returnresult;
}
}

2、使用

新建Android工程

packagecom.example.aesdemo;

importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.util.Log;

publicclassMainActivityextendsActivity{
		
protectedvoidonCreate(BundlesavedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
	StringmasterPassword="a";
	StringoriginalText="于";

	try{
	StringencryptingCode=AESHelper.encrypt(originalText,masterPassword);
//	System.out.println("加密结果为"+encryptingCode);
	Log.i("加密结果为",encryptingCode);
	StringdecryptingCode=AESHelper.decrypt(encryptingCode,masterPassword);
//	System.out.println("解密结果为"+decryptingCode);
	Log.i("解密结果",decryptingCode);
	}catch(Exceptione){
	//TODOAuto-generatedcatchblock
	e.printStackTrace();
	}	
	}

	@Override
	publicbooleanonCreateOptionsMenu(Menumenu){
		//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
		getMenuInflater().inflate(R.menu.main,menu);
		returntrue;
	}
}

3、打印结果

09-1910:41:05.467:I/加密结果为(707):E55C24701F6380478E1940ADDFD08D22
09-1910:41:05.467:I/解密结果(707):于