Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 對稱加密與 Base64 加密實戰

編輯:關於 Android 程式設計

對稱加密是最快速、最簡單的一種加密方式,它的加密(encryption)與解密(decryption)使用的是同一把密鑰(secret key)。常用的對稱加密方式有 DES 和 AES。

DES 加密解密實例

先看界面交互部分的 MainActivity:

  1. public class MainActivity extends AppCompatActivity {
  2. private EditText des_input;
  3. private EditText des_password;
  4. private TextView show_des_encrypt;
  5. private TextView show_des_decrypt;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. initView();
  11. }
  12. private void initView() {
  13. des_input = ((EditText) findViewById(R.id.des_input));
  14. des_password = ((EditText) findViewById(R.id.des_password));
  15. show_des_encrypt = ((TextView) findViewById(R.id.show_des_encrypt));
  16. show_des_decrypt = ((TextView) findViewById(R.id.show_des_decrypt));
  17. }
  18. /**
  19. * DES 加密
  20. * @param view
  21. */
  22. public void btnDESEncrypt(View view) {
  23. // 將 DES 加密寫成工具類
  24. // 要加密內容
  25. byte[] data = des_input.getText().toString().getBytes();
  26. byte[] password = des_password.getText().toString().getBytes();
  27. // 工具類 DESUtils 中 encrypt 方法返回 byte[]
  28. byte[] result = DESUtils.encrypt(data, password);
  29. // 處理加密結果
  30. byte[] encode = Base64.encode(result, Base64.DEFAULT);
  31. // 顯示加密的結果
  32. show_des_encrypt.setText(new String(encode));
  33. }
  34. /**
  35. * DES 進行解密
  36. * @param view
  37. */
  38. public void btnDESDecrypt(View view) {
  39. // 獲取解密的數據
  40. byte[] data = show_des_encrypt.getText().toString().getBytes();
  41. byte[] decode = Base64.decode(data, Base64.DEFAULT);
  42. // 獲取密碼
  43. byte[] password = des_password.getText().toString().getBytes();
  44. byte[] result = DESUtils.decrypt(decode, password);
  45. // 對解密數據進行處理
  46. show_des_decrypt.setText(new String(result));
  47. }
  48. }

再看 DES 工具類 DESUtils:

  1. public class DESUtils {
  2. public static byte[] encrypt(byte[] data, byte[] password) {
  3. byte[] ret = null;
  4. // 判斷參數是否符合條件
  5. if (data != null && data.length > 0) {
  6. if (password != null && password.length == 8) {
  7. try {
  8. // 1、創造加密引擎
  9. Cipher cipher = Cipher.getInstance("DES");
  10. // 2、初始化
  11. SecretKeySpec key = new SecretKeySpec(password, "DES");
  12. cipher.init(Cipher.ENCRYPT_MODE, key);
  13. // 3、對數據進行加密
  14. ret = cipher.doFinal(data);
  15. } catch (NoSuchAlgorithmException e) {
  16. e.printStackTrace();
  17. } catch (NoSuchPaddingException e) {
  18. e.printStackTrace();
  19. } catch (InvalidKeyException e) {
  20. e.printStackTrace();
  21. } catch (BadPaddingException e) {
  22. e.printStackTrace();
  23. } catch (IllegalBlockSizeException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  28. return ret;
  29. }
  30. public static byte[] decrypt(byte[] data, byte[] password) {
  31. byte[] ret = null;
  32. if (data != null && data.length > 0) {
  33. if (password != null && password.length == 8) {
  34. try {
  35. // 1、獲取解密引擎
  36. Cipher cipher = Cipher.getInstance("DES");
  37. // 2、初始化
  38. SecretKeySpec key = new SecretKeySpec(password, "DES");
  39. cipher.init(Cipher.DECRYPT_MODE, key);
  40. // 3、解密
  41. ret = cipher.doFinal(data);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. return ret;
  48. }
  49. }

AES 加密解密實例

注意:輸入的 password 和 ivParams 為 16 位。

  1. public class AESUtils {
  2. public static byte[] encrypt(byte[] data, byte[] password) {
  3. byte[] ret = null;
  4. if (data != null && data.length > 0) {
  5. if (password != null && password.length == 16) {
  6. try {
  7. // 1、創造加密引擎
  8. Cipher cipher = Cipher.getInstance("AES");
  9. // 2、初始化,無法使用 SecretKeyFactory
  10. SecretKeySpec key = new SecretKeySpec(password, "AES");
  11. cipher.init(Cipher.ENCRYPT_MODE, key);
  12. // 3、加密
  13. ret = cipher.doFinal(data);
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }
  19. return ret;
  20. }
  21. public static byte[] decrypt(byte[] data, byte[] password) {
  22. byte[] ret = null;
  23. if (data != null && data.length > 0) {
  24. if (password != null && password.length == 16) {
  25. try {
  26. // 1、創造加密引擎
  27. Cipher cipher = Cipher.getInstance("AES");
  28. // 2、初始化
  29. SecretKeySpec key = new SecretKeySpec(password, "AES");
  30. cipher.init(Cipher.DECRYPT_MODE, key);
  31. // 3、解密
  32. ret = cipher.doFinal(data);
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. /**
  41. * AES 第二種加密算法
  42. * @param data
  43. * @param password
  44. * @param ivParams
  45. * @return
  46. */
  47. public static byte[] encrypt2(byte[] data, byte[] password, byte[] ivParams) {
  48. byte[] ret = null;
  49. if (data != null && data.length > 0) {
  50. if (password != null && password.length == 16 && ivParams != null && ivParams.length == 16) {
  51. try {
  52. // 1、創造加密引擎,CBC:加密模式;PKCS5Padding:填充模式
  53. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  54. // 2、初始化
  55. SecretKeySpec key = new SecretKeySpec(password, "AES");
  56. // 相當於第二個密碼,向量參數,加密級別更高
  57. IvParameterSpec iv_params = new IvParameterSpec(ivParams);
  58. cipher.init(Cipher.ENCRYPT_MODE, key, iv_params);
  59. // 3、加密
  60. ret = cipher.doFinal(data);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. return ret;
  67. }
  68. }

AES 的界面交互部分,這裡採用第二套加密算法,需要一個向量參數,相當於另一套密碼:

  1. public class MainActivity extends AppCompatActivity {
  2. private EditText aes_input;
  3. private EditText aes_password;
  4. private TextView show_aes_decrypt;
  5. private TextView show_aes_encrypt;
  6. private byte[] password;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. initView();
  12. }
  13. public void qqq(View view) {
  14. byte[] data = aes_input.getText().toString().getBytes();
  15. password = aes_password.getText().toString().getBytes();
  16. // 指定向量參數(密碼)
  17. String iv = "0123456789abcdef";
  18. byte[] ivParams = iv.getBytes();
  19. byte[] result = AESUtils.encrypt2(data, password, ivParams);
  20. // 對返回的結果進行處理,顯示
  21. byte[] encode = Base64.encode(result, Base64.DEFAULT);
  22. show_aes_encrypt.setText(new String(encode));
  23. }
  24. public void www(View view) {
  25. byte[] data = show_aes_encrypt.getText().toString().getBytes();
  26. // 對 data 進行處理
  27. data = Base64.decode(data, Base64.DEFAULT);
  28. byte[] result = AESUtils.decrypt(data, password);
  29. if (result != null) {
  30. show_aes_decrypt.setText(new String(result));
  31. } else {
  32. show_aes_decrypt.setText("解密失敗");
  33. }
  34. }
  35. }

Base64 加密與解密

Base64 本質上是一種編碼方式,其特點是可逆、易於傳輸。使用時要注意編碼和解碼需採用一致的格式。

  1. public class Base64Activity extends AppCompatActivity {
  2. private EditText input;
  3. private TextView show_base64_encode;
  4. private TextView show_base64_decode;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_base64);
  9. initView();
  10. }
  11. /**
  12. * 進行 Base64 編碼
  13. * @param view
  14. */
  15. public void btnBase64Encode(View view) {
  16. String input = this.input.getText().toString();
  17. if (input != null) {
  18. byte[] bytes = input.getBytes();
  19. // Base64 編碼時採用什麼格式,解碼時就採用什麼格式
  20. String encodeToString = Base64.encodeToString(bytes, 0, bytes.length, Base64.NO_WRAP);
  21. // 顯示編碼的內容
  22. show_base64_encode.setText(encodeToString);
  23. }
  24. }
  25. /**
  26. * 進行 Base64 解密
  27. * @param view
  28. */
  29. public void btnBase64Decode(View view) {
  30. String str = show_base64_encode.getText().toString();
  31. byte[] decode = Base64.decode(str, Base64.DEFAULT);
  32. show_base64_decode.setText(new String(decode));
  33. }
  34. }

以上就是 Android 中 DES、AES 兩種對稱加密方式以及 Base64 編碼的完整實戰示例。實際開發中,AES 因其更高的安全性而被廣泛採用,而 Base64 則常用於加密結果的可讀化展示與網絡傳輸。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved