from:// http://blog.csdn.net/xinzheng_wang/article/details/9159969

AES加解密算法在Android中的应用及Android4.2以上版本调用问题

分类:Android 328人阅读 评论(3) 收藏 举报

  

  密码学中的高级加密标准(Advanced Encryption Standard,AES),又称高级加密标准Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael之命名之。

要区别4.2以上版本的调用方法,否则将会出现意想不到的问题。

AESCipher.java

[java] view plain copy
  1. packagecom.test;
  2. importjava.security.SecureRandom;
  3. importjavax.crypto.Cipher;
  4. importjavax.crypto.KeyGenerator;
  5. importjavax.crypto.SecretKey;
  6. importjavax.crypto.spec.SecretKeySpec;
  7. publicclassAESCipher{
  8. publicstaticStringencrypt(Stringkey,Stringsrc)throwsException{
  9. byte[]rawKey=getRawKey(key.getBytes());
  10. byte[]result=encrypt(rawKey,src.getBytes());
  11. returntoHex(result);
  12. }
  13. publicstaticStringdecrypt(Stringkey,Stringencrypted)throwsException{
  14. byte[]rawKey=getRawKey(key.getBytes());
  15. byte[]enc=toByte(encrypted);
  16. byte[]result=decrypt(rawKey,enc);
  17. returnnewString(result);
  18. }
  19. privatestaticbyte[]getRawKey(byte[]seed)throwsException{
  20. KeyGeneratorkgen=KeyGenerator.getInstance("AES");
  21. //SHA1PRNG强随机种子算法,要区别4.2以上版本的调用方法
  22. SecureRandomsr=null;
  23. if(android.os.Build.VERSION.SDK_INT>=17){
  24. sr=SecureRandom.getInstance("SHA1PRNG","Crypto");
  25. }else{
  26. sr=SecureRandom.getInstance("SHA1PRNG");
  27. }
  28. sr.setSeed(seed);
  29. kgen.init(256,sr);//256bitsor128bits,192bits
  30. SecretKeyskey=kgen.generateKey();
  31. byte[]raw=skey.getEncoded();
  32. returnraw;
  33. }
  34. privatestaticbyte[]encrypt(byte[]key,byte[]src)throwsException{
  35. SecretKeySpecskeySpec=newSecretKeySpec(key,"AES");
  36. Ciphercipher=Cipher.getInstance("AES");
  37. cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
  38. byte[]encrypted=cipher.doFinal(src);
  39. returnencrypted;
  40. }
  41. privatestaticbyte[]decrypt(byte[]key,byte[]encrypted)throwsException{
  42. SecretKeySpecskeySpec=newSecretKeySpec(key,"AES");
  43. Ciphercipher=Cipher.getInstance("AES");
  44. cipher.init(Cipher.DECRYPT_MODE,skeySpec);
  45. byte[]decrypted=cipher.doFinal(encrypted);
  46. returndecrypted;
  47. }
  48. publicstaticStringtoHex(Stringtxt){
  49. returntoHex(txt.getBytes());
  50. }
  51. publicstaticStringfromHex(Stringhex){
  52. returnnewString(toByte(hex));
  53. }
  54. publicstaticbyte[]toByte(StringhexString){
  55. intlen=hexString.length()/2;
  56. byte[]result=newbyte[len];
  57. for(inti=0;i<len;i++)
  58. result[i]=Integer.valueOf(hexString.substring(2*i,2*i+2),16).byteValue();
  59. returnresult;
  60. }
  61. publicstaticStringtoHex(byte[]buf){
  62. if(buf==null)
  63. return"";
  64. StringBufferresult=newStringBuffer(2*buf.length);
  65. for(inti=0;i<buf.length;i++){
  66. appendHex(result,buf[i]);
  67. }
  68. returnresult.toString();
  69. }
  70. privatefinalstaticStringHEX="0123456789ABCDEF";
  71. privatestaticvoidappendHex(StringBuffersb,byteb){
  72. sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  73. }
  74. }


TestAES.java

[java] view plain copy
  1. packagecom.test;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.view.View;
  5. importandroid.view.View.OnClickListener;
  6. importandroid.widget.Button;
  7. importandroid.widget.EditText;
  8. importandroid.widget.TextView;
  9. publicclassTestAESextendsActivityimplementsOnClickListener{
  10. privateTextViewtvTip=null;
  11. privateEditTextetKey=null;
  12. privateEditTextetStr=null;
  13. privateButtonbtnEncrypt=null;
  14. privateButtonbtnDecrypt=null;
  15. //
  16. Stringsrc=null;
  17. Stringkey=null;
  18. Stringdest=null;
  19. /**Calledwhentheactivityisfirstcreated.*/
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. tvTip=(TextView)findViewById(R.id.tvTip);
  25. etKey=(EditText)findViewById(R.id.etKey);
  26. etStr=(EditText)findViewById(R.id.etStr);
  27. btnEncrypt=(Button)findViewById(R.id.btnEncrypt);
  28. btnEncrypt.setOnClickListener(this);
  29. btnDecrypt=(Button)findViewById(R.id.btnDecrypt);
  30. btnDecrypt.setOnClickListener(this);
  31. btnEncrypt.setEnabled(true);
  32. btnDecrypt.setEnabled(false);
  33. }
  34. @Override
  35. publicvoidonClick(Viewv){
  36. //TODOAuto-generatedmethodstub
  37. if(v==btnEncrypt){
  38. src=etStr.getText().toString().trim();
  39. key=etKey.getText().toString().trim();
  40. if(!src.equals("")&&!key.equals("")){
  41. try{
  42. dest=AESCipher.encrypt(key,src);
  43. tvTip.setText("Encrypted:");
  44. etStr.setText(dest);
  45. btnEncrypt.setEnabled(false);
  46. btnDecrypt.setEnabled(true);
  47. }catch(Exceptione){
  48. //TODOAuto-generatedcatchblock
  49. e.printStackTrace();
  50. }
  51. }
  52. }elseif(v==btnDecrypt){
  53. src=etStr.getText().toString().trim();
  54. key=etKey.getText().toString().trim();
  55. if(!src.equals("")&&!key.equals("")){
  56. try{
  57. dest=AESCipher.decrypt(key,src);
  58. tvTip.setText("Decrypted:");
  59. etStr.setText(dest);
  60. btnDecrypt.setEnabled(false);
  61. btnEncrypt.setEnabled(true);
  62. }catch(Exceptione){
  63. //TODOAuto-generatedcatchblock
  64. e.printStackTrace();
  65. }
  66. }else{
  67. tvTip.setText("Source:");
  68. btnDecrypt.setEnabled(false);
  69. btnEncrypt.setEnabled(true);
  70. }
  71. }
  72. }
  73. }


main.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <LinearLayout
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10. <TextView
  11. android:layout_width="100dp"
  12. android:layout_height="wrap_content"
  13. android:text="Key:"/>
  14. <EditText
  15. android:id="@+id/etKey"
  16. android:layout_width="200dp"
  17. android:layout_height="40dp"
  18. android:maxLength="32"
  19. android:hint="Inputthekey"/>
  20. </LinearLayout>
  21. <LinearLayout
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:orientation="vertical">
  25. <TextView
  26. android:id="@+id/tvTip"
  27. android:layout_width="100dp"
  28. android:layout_height="wrap_content"
  29. android:text="Source:"/>
  30. <EditText
  31. android:id="@+id/etStr"
  32. android:layout_width="400dp"
  33. android:layout_height="200dp"
  34. android:hint="Inputthesource"
  35. android:inputType="textMultiLine"
  36. android:maxLength="4096"
  37. android:maxLines="100"
  38. android:scrollHorizontally="false"/>
  39. </LinearLayout>
  40. <LinearLayout
  41. android:layout_width="fill_parent"
  42. android:layout_height="wrap_content"
  43. android:orientation="horizontal">
  44. <Button
  45. android:id="@+id/btnEncrypt"
  46. android:layout_width="120dp"
  47. android:layout_height="50dp"
  48. android:text="加密字符串"/>
  49. <Button
  50. android:id="@+id/btnDecrypt"
  51. android:layout_width="120dp"
  52. android:layout_height="50dp"
  53. android:text="解密字符串"/>
  54. </LinearLayout>
  55. </LinearLayout>

更多相关文章

  1. 2013.12.23 (2)——— android 代码调用shell
  2. Android(安卓)MD5加密
  3. android中Sha256加密算法
  4. Android(安卓)加密解密
  5. Android: MediaScanner生成thumbnail的算法
  6. android 学习视频
  7. Android开发起步
  8. C#/IOS/Android通用加密解密方法
  9. Notes on the implementation of encryption in Android(安卓)3.

随机推荐

  1. 我的第一个Android应用程序HelloWorld
  2. Cursor与Adapter结合使用
  3. Xamarin的Mono for Android目前可运行于M
  4. Ubuntu14.04 Android(安卓)源码编译
  5. Samsung Galaxy Nexus 正式發佈,4.65 吋 H
  6. Android中矢量图形的那些事 - SVG or Vec
  7. Android(安卓)反射机制
  8. Android集结号
  9. flutter项目的打包和发布 主要是Android
  10. [置顶] android 自己创建一个注释模板