Android NAES加密解决方案 使用方式如下:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c6E5UlI1N/article/details/83593011

     上一篇文章已经发布了一种方案,但是对一些机型还是不能够适配,现在说一种终极方案:

    通过bcprov-jdk16-139.jar 使用AES/CBC/PKCS7Padding 加解密字符串

所以需要一个jar 来支持。bcprov-jdk16-139.jar

下载地址:http://www.bouncycastle.org/archive/139/bcprov-jdk16-139.jar

这种方式 对于 N以下版本也适用。

使用方式如下:

 1/**
2 * 
4 * AES128 算法
5 * 
6 * CBC 模式
7 * 
8 * PKCS7Padding 填充模式
9 * 
10
14 */

15public class AES {
16 // 算法名称
17 final String KEY_ALGORITHM = "AES";
18 // 加解密算法/模式/填充方式
19 final String algorithmStr = "AES/CBC/PKCS7Padding";
20 //
21 private Key key;
22 private Cipher cipher;
23 boolean isInited = false;
24
25 byte[] iv = { 0x300x310x300x320x300x330x300x340x300x350x300x360x300x370x300x38 };
26 public void init(byte[] keyBytes{
27
28  // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
29  int base = 16;
30  if (keyBytes.length % base != 0) {
31   int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
32   byte[] temp = new byte[groups * base];
33   Arrays.fill(temp, (byte0);
34   System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
35   keyBytes = temp;
36  }
37  // 初始化
38  Security.addProvider(new BouncyCastleProvider());
39  // 转化成JAVA的密钥格式
40  key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
41  try {
42   // 初始化cipher
43   cipher = Cipher.getInstance(algorithmStr, "BC");
44  } catch (NoSuchAlgorithmException e) {
45   // TODO Auto-generated catch block
46   e.printStackTrace();
47  } catch (NoSuchPaddingException e) {
48   // TODO Auto-generated catch block
49   e.printStackTrace();
50  } catch (NoSuchProviderException e) {
51   // TODO Auto-generated catch block
52   e.printStackTrace();
53  }
54 }
55 /**
56  * 加密方法
57  * 
58  * @param content
59  *            要加密的字符串
60  * @param keyBytes
61  *            加密密钥
62  * @return
63  */

64 public byte[] encrypt(byte[] content, byte[] keyBytes{
65  byte[] encryptedText = null;
66  init(keyBytes);
67  System.out.println("IV:" + new String(iv));
68  try {
69   cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
70   encryptedText = cipher.doFinal(content);
71  } catch (Exception e) {
72   // TODO Auto-generated catch block
73   e.printStackTrace();
74  }
75  return encryptedText;
76 }
77 /**
78  * 解密方法
79  * 
80  * @param encryptedData
81  *            要解密的字符串
82  * @param keyBytes
83  *            解密密钥
84  * @return
85  */

86 public byte[] decrypt(byte[] encryptedData, byte[] keyBytes{
87  byte[] encryptedText = null;
88  init(keyBytes);
89  System.out.println("IV:" + new String(iv));
90  try {
91   cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
92   encryptedText = cipher.doFinal(encryptedData);
93  } catch (Exception e) {
94   // TODO Auto-generated catch block
95   e.printStackTrace();
96  }
97  return encryptedText;
98 }
99}

测试方法:

1public class Test {
2 public static void main(String[] args{
3  AES aes = new AES();
4//   加解密 密钥
5  byte[] keybytes = { 0x310x320x330x340x350x360x370x38 };
6  String content = "1";
7  // 加密字符串
8  System.out.println("加密前的:" + content);
9  System.out.println("加密密钥:" + new String(keybytes));
10  // 加密方法
11  byte[] enc = aes.encrypt(content.getBytes(), keybytes);
12  System.out.println("加密后的内容:" + new String(Hex.encode(enc)));
13  // 解密方法
14  byte[] dec = aes.decrypt(enc, keybytes);
15  System.out.println("解密后的内容:" + new String(dec));
16 }
17
18}

                        喜欢 就关注吧,欢迎投稿!

640?wx_fmt=jpeg



猜你喜欢

转载自blog.csdn.net/c6E5UlI1N/article/details/83593011