Android--AES加密解密
发布日期:2021-05-10 12:49:08 浏览次数:20 分类:精选文章

本文共 6782 字,大约阅读时间需要 22 分钟。

AES������������������

AES���������������������

AES���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������AES���������������������������������������

  • ������Cipher������������������������������������������������������������
  • ���������������������Cipher������������������������������
  • ������������������������������������������������������
  • AES������������������

    AES������������������������������������������������������

  • ECB������������������������������������������������������
  • CBC������������������������������������������������������������������������������������������
  • CFB���������������������������������������������������������
  • OFB������������������������������������������������������������������������������
  • AES���������������������������16������������������������������������������ECB���������������������16���������������������

    AES���������������

    ������������������������AES������������������������

    package com.example.xiaobai.aes;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.spec.InvalidKeySpecException;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto NoSuchPaddingException;
    import javax.crypto SecretKey;
    import javax.crypto SecretKeyFactory;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    public class AES {
    private final String Algorithm = "PBEWITHSHAANDTWOFISH-CBC";
    private final int IterationCount = 10000;
    private final int KeyLength = 128;
    private char[] HumanPassphrase = {
    'P', 'e', 'r', ' ', 'v', 'a', 'l', 'l',
    'u', 'm', ' ', 'd', 'u', 'c', 'e', 's',
    ' ', 'L', 'a', 'b', 'a', 'n', 't'
    };
    private byte[] Salt = {
    0x00, 0x01, 0x02, 0x03,
    0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0xA, 0xB,
    0xC, 0xD, 0xE, 0xF
    };
    private IvParameterSpec Iv;
    private SecretKeyFactory KeyFactory;
    private SecretKey SecretKey;
    private SecretKeySpec SecretKeySpec;
    public AES() {
    try {
    KeyFactory = SecretKeyFactory.getInstance(Algorithm);
    SecretKey = KeyFactory.generateSecretKey(HumanPassphrase, Salt, IterationCount, KeyLength);
    } catch (NoSuchAlgorithmException e) {
    // ������������������������
    Log.e("AES������", "������" + Algorithm + "������������");
    } catch (InvalidKeySpecException e) {
    // ���������������������
    Log.e("AES������", "������������" + Algorithm + "������");
    }
    try {
    byte[] KeyByteArray = SecretKey.getEncoded();
    SecretKeySpec = new SecretKeySpec(KeyByteArray, "AES");
    Iv = new IvParameterSpec("1234567890123456".getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
    Log.e("AES������", "������������������", e);
    }
    }
    public byte[] Encrypt(byte[] Plaintext, String CipherMode) {
    try {
    Cipher cipher = Cipher.getInstance(CipherMode);
    cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec, Iv);
    return cipher.doFinal(Plaintext);
    } catch (NoSuchAlgorithmException e) {
    // ���������������������
    Log.e("AES������", "������������" + CipherMode + "���������");
    } catch (NoSuchPaddingException e) {
    Log.e("AES������", "���������������������");
    } catch (InvalidKeyException e) {
    Log.e("AES������", "������������");
    } catch (InvalidAlgorithmParameterException e) {
    Log.e("AES������", "������������������");
    } catch (IllegalBlockSizeException e) {
    Log.e("AES������", "������������������");
    } catch (BadPaddingException e) {
    Log.e("AES������", "���������������");
    }
    return null;
    }
    public byte[] Decrypt(byte[] Ciphertext, String CipherMode) {
    try {
    Cipher cipher = Cipher.getInstance(CipherMode);
    cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec, Iv);
    return cipher.doFinal(Ciphertext);
    } catch (NoSuchAlgorithmException e) {
    Log.e("AES������", "������������" + CipherMode + "���������");
    } catch (NoSuchPaddingException e) {
    Log.e("AES������", "���������������������");
    } catch (InvalidKeyException e) {
    Log.e("AES������", "������������");
    } catch (InvalidAlgorithmParameterException e) {
    Log.e("AES������", "������������������");
    } catch (IllegalBlockSizeException e) {
    Log.e("AES������", "������������������");
    } catch (BadPaddingException e) {
    Log.e("AES������", "���������������");
    }
    return null;
    }
    }
    ## ������������
    ```java
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import java.io.UnsupportedEncodingException;
    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
    String Plaintext = "hello AES!";
    byte[] Bytes = Plaintext.getBytes("UTF-8");
    AES AESUtil = new AES();
    String Encrypted = AESUtil.Encrypt(Bytes, "AES/CBC/PKCS7Padding");
    Log.i("AES������������", Encrypted);
    String Decrypted = AESUtil.Decrypt(AESUtil.Base64Decode(Encrypted), "AES/CBC/PKCS7Padding");
    Log.i("AES������������", Decrypted);
    } catch (NoSuchAlgorithmException e) {
    Log.e("AES������", "������������", e);
    } catch (UnsupportedEncodingException e) {
    Log.e("AES������", "���������������", e);
    }
    }
    }
    public class Base64 {
    private static byte[] decode(String s, OutputStream os) throws IOException {
    int len = s.length();
    int[] decoded = new int[4 * (len / 3) + 3];
    int index = 0;
    for (int i = 0; i < len; i++) {
    switch (s.charAt(i)) {
    case 'A'-'Z': case 'a'-'z': case '0'-'9': case '+': case '/': case '=':
    break;
    default:
    throw new RuntimeException("������������");
    }
    }
    return null;
    }
    }

    ���������������������������������������AES���������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Android--RSA加密解密
    下一篇:Android--DES加密解密

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月06日 21时54分36秒