DES、3DES、AES加密方式

时间:2023-03-08 17:38:42

详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165

DES 支持8位加密解密,3Des支持24位,Aes支持32位。3Des是Des算法做三次。位数的单位是字节byte,不是bits。

3Des是把24位分成3组,第一组八位用来加密,第二组8位用于解密,第三组8位用于加密,所以,如果秘钥为123456781234567812345678(3组1-8),则相当于做了一次12345678的Des加密。例如:第一次用12345678秘钥对123进行加密得到 "LDiFUdf0iew=",然后用第二组的12345678对其进行解密(逆向加密过程),得到了123,第三次又一次加密得到 "LDiFUdf0iew="。

三种加密方式代码里不同的地方:

byte temp[] = new byte[位数];

SecretKey des_key = new SecretKeySpec(temp, "加密算法");

加密算法名对应的是:Aes Des Desede

改了这两个地方就可以实现不同加密方式。加密方式底层不一样,DES是把原文编程2进制的一串01,然后和密文做运算,交换什么什么位置。

[java] view plaincopyprint?

  1. public class MainActivity extends Activity implements OnClickListener {

  2. private EditText des_key, des_src, des_dst;

  3. private Button btn_encode, btn_decode;

  4. @Override

  5. protected void onCreate(Bundle savedInstanceState) {

  6. super.onCreate(savedInstanceState);

  7. setContentView(R.layout.activity_main);

  8. des_key = (EditText) findViewById(R.id.des_key);

  9. des_src = (EditText) findViewById(R.id.des_src);

  10. des_dst = (EditText) findViewById(R.id.des_dst);

  11. btn_encode = (Button) findViewById(R.id.btn_encode);

  12. btn_decode = (Button) findViewById(R.id.btn_decode);

  13. btn_encode.setOnClickListener(this);

  14. btn_decode.setOnClickListener(this);

  15. }

  16. @Override

  17. public void onClick(View v) {

  18. String key_str = des_key.getText().toString();

  19. if (!TextUtils.isEmpty(key_str)) {

  20. try {

  21. // DES不管长了短了,都变成八位

  22. // AES 长度变为128 new SecretKeySpec(temp, "Aes");

  23. // 3Des 长度变为24 new SecretKeySpec(temp, "Desced");

  24. byte temp[] = new byte[8];

  25. byte b[] = key_str.getBytes("UTF-8");

  26. System.arraycopy(b, 0, temp, 0, Math.min(b.length, temp.length));

  27. // Des只支持八位

  28. SecretKey des_key = new SecretKeySpec(temp, "Des");

  29. Cipher cipher = Cipher.getInstance("Des");

  30. switch (v.getId()) {

  31. case R.id.btn_encode:

  32. String src_str = des_src.getText().toString();

  33. if (!TextUtils.isEmpty(src_str)) {

  34. cipher.init(Cipher.ENCRYPT_MODE, des_key);

  35. byte[] bytes = cipher

  36. .doFinal(src_str.getBytes("UTF-8"));

  37. // 是用Base64编码的二进制字节数组

  38. des_dst.setText(Base64.encodeToString(bytes,

  39. Base64.DEFAULT));

  40. }

  41. break;

  42. case R.id.btn_decode:

  43. String dst_str = des_dst.getText().toString();

  44. if (!TextUtils.isEmpty(dst_str)) {

  45. cipher.init(Cipher.DECRYPT_MODE, des_key);

  46. // 是用Base64编码的二进制字节数组

  47. byte[] bytes = cipher.doFinal(Base64.decode(dst_str,

  48. Base64.DEFAULT));

  49. des_src.setText(new String(bytes, "UTF-8"));

  50. }

  51. break;

  52. }

  53. } catch (Exception e) {

  54. e.printStackTrace();

  55. }

  56. }

  57. }

  58. }


布局:

[java] view plaincopyprint?

  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. xmlns:tools="http://schemas.android.com/tools"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical" >

  6. <EditText

  7. android:id="@+id/des_key"

  8. android:layout_width="wrap_content"

  9. android:layout_height="wrap_content"

  10. android:hint="秘钥" />

  11. <EditText

  12. android:id="@+id/des_src"

  13. android:layout_width="wrap_content"

  14. android:layout_height="wrap_content"

  15. android:hint="原文" />

  16. <EditText

  17. android:id="@+id/des_dst"

  18. android:layout_width="wrap_content"

  19. android:layout_height="wrap_content"

  20. android:hint="密文" />

  21. <Button

  22. android:id="@+id/btn_encode"

  23. android:layout_width="wrap_content"

  24. android:layout_height="wrap_content"

  25. android:text="加密" />

  26. <Button

  27. android:id="@+id/btn_decode"

  28. android:layout_width="wrap_content"

  29. android:layout_height="wrap_content"

  30. android:text="解密" />

  31. </LinearLayout></span>

DES、3DES、AES加密方式