AES加解密算法在Android中的应用及Android4.2以上版本调用问题
16lz
2021-12-04
from:// http://blog.csdn.net/xinzheng_wang/article/details/9159969
AES加解密算法在Android中的应用及Android4.2以上版本调用问题
分类:Android 2013-06-24 11:25 328人阅读 评论(3) 收藏 举报
密码学中的高级加密标准(Advanced Encryption Standard,AES),又称高级加密标准Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael之命名之。
要区别4.2以上版本的调用方法,否则将会出现意想不到的问题。
AESCipher.java
[java] view plain copy- packagecom.test;
- importjava.security.SecureRandom;
- importjavax.crypto.Cipher;
- importjavax.crypto.KeyGenerator;
- importjavax.crypto.SecretKey;
- importjavax.crypto.spec.SecretKeySpec;
- publicclassAESCipher{
- publicstaticStringencrypt(Stringkey,Stringsrc)throwsException{
- byte[]rawKey=getRawKey(key.getBytes());
- byte[]result=encrypt(rawKey,src.getBytes());
- returntoHex(result);
- }
- publicstaticStringdecrypt(Stringkey,Stringencrypted)throwsException{
- byte[]rawKey=getRawKey(key.getBytes());
- byte[]enc=toByte(encrypted);
- byte[]result=decrypt(rawKey,enc);
- returnnewString(result);
- }
- privatestaticbyte[]getRawKey(byte[]seed)throwsException{
- KeyGeneratorkgen=KeyGenerator.getInstance("AES");
- //SHA1PRNG强随机种子算法,要区别4.2以上版本的调用方法
- SecureRandomsr=null;
- if(android.os.Build.VERSION.SDK_INT>=17){
- sr=SecureRandom.getInstance("SHA1PRNG","Crypto");
- }else{
- sr=SecureRandom.getInstance("SHA1PRNG");
- }
- sr.setSeed(seed);
- kgen.init(256,sr);//256bitsor128bits,192bits
- SecretKeyskey=kgen.generateKey();
- byte[]raw=skey.getEncoded();
- returnraw;
- }
- privatestaticbyte[]encrypt(byte[]key,byte[]src)throwsException{
- SecretKeySpecskeySpec=newSecretKeySpec(key,"AES");
- Ciphercipher=Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
- byte[]encrypted=cipher.doFinal(src);
- returnencrypted;
- }
- privatestaticbyte[]decrypt(byte[]key,byte[]encrypted)throwsException{
- SecretKeySpecskeySpec=newSecretKeySpec(key,"AES");
- Ciphercipher=Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE,skeySpec);
- byte[]decrypted=cipher.doFinal(encrypted);
- returndecrypted;
- }
- publicstaticStringtoHex(Stringtxt){
- returntoHex(txt.getBytes());
- }
- publicstaticStringfromHex(Stringhex){
- returnnewString(toByte(hex));
- }
- publicstaticbyte[]toByte(StringhexString){
- intlen=hexString.length()/2;
- byte[]result=newbyte[len];
- for(inti=0;i<len;i++)
- result[i]=Integer.valueOf(hexString.substring(2*i,2*i+2),16).byteValue();
- returnresult;
- }
- publicstaticStringtoHex(byte[]buf){
- if(buf==null)
- return"";
- StringBufferresult=newStringBuffer(2*buf.length);
- for(inti=0;i<buf.length;i++){
- appendHex(result,buf[i]);
- }
- returnresult.toString();
- }
- privatefinalstaticStringHEX="0123456789ABCDEF";
- privatestaticvoidappendHex(StringBuffersb,byteb){
- sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
- }
- }
TestAES.java
- packagecom.test;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- publicclassTestAESextendsActivityimplementsOnClickListener{
- privateTextViewtvTip=null;
- privateEditTextetKey=null;
- privateEditTextetStr=null;
- privateButtonbtnEncrypt=null;
- privateButtonbtnDecrypt=null;
- //
- Stringsrc=null;
- Stringkey=null;
- Stringdest=null;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tvTip=(TextView)findViewById(R.id.tvTip);
- etKey=(EditText)findViewById(R.id.etKey);
- etStr=(EditText)findViewById(R.id.etStr);
- btnEncrypt=(Button)findViewById(R.id.btnEncrypt);
- btnEncrypt.setOnClickListener(this);
- btnDecrypt=(Button)findViewById(R.id.btnDecrypt);
- btnDecrypt.setOnClickListener(this);
- btnEncrypt.setEnabled(true);
- btnDecrypt.setEnabled(false);
- }
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- if(v==btnEncrypt){
- src=etStr.getText().toString().trim();
- key=etKey.getText().toString().trim();
- if(!src.equals("")&&!key.equals("")){
- try{
- dest=AESCipher.encrypt(key,src);
- tvTip.setText("Encrypted:");
- etStr.setText(dest);
- btnEncrypt.setEnabled(false);
- btnDecrypt.setEnabled(true);
- }catch(Exceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }elseif(v==btnDecrypt){
- src=etStr.getText().toString().trim();
- key=etKey.getText().toString().trim();
- if(!src.equals("")&&!key.equals("")){
- try{
- dest=AESCipher.decrypt(key,src);
- tvTip.setText("Decrypted:");
- etStr.setText(dest);
- btnDecrypt.setEnabled(false);
- btnEncrypt.setEnabled(true);
- }catch(Exceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }else{
- tvTip.setText("Source:");
- btnDecrypt.setEnabled(false);
- btnEncrypt.setEnabled(true);
- }
- }
- }
- }
main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:text="Key:"/>
- <EditText
- android:id="@+id/etKey"
- android:layout_width="200dp"
- android:layout_height="40dp"
- android:maxLength="32"
- android:hint="Inputthekey"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tvTip"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:text="Source:"/>
- <EditText
- android:id="@+id/etStr"
- android:layout_width="400dp"
- android:layout_height="200dp"
- android:hint="Inputthesource"
- android:inputType="textMultiLine"
- android:maxLength="4096"
- android:maxLines="100"
- android:scrollHorizontally="false"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnEncrypt"
- android:layout_width="120dp"
- android:layout_height="50dp"
- android:text="加密字符串"/>
- <Button
- android:id="@+id/btnDecrypt"
- android:layout_width="120dp"
- android:layout_height="50dp"
- android:text="解密字符串"/>
- </LinearLayout>
- </LinearLayout>
更多相关文章
- 2013.12.23 (2)——— android 代码调用shell
- Android(安卓)MD5加密
- android中Sha256加密算法
- Android(安卓)加密解密
- Android: MediaScanner生成thumbnail的算法
- android 学习视频
- Android开发起步
- C#/IOS/Android通用加密解密方法
- Notes on the implementation of encryption in Android(安卓)3.