本文共 3365 字,大约阅读时间需要 11 分钟。
上一篇文章已经发布了一种方案,但是对一些机型还是不能够适配,现在说一种终极方案:
通过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 = { 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37, 0x30, 0x38 }; 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, (byte) 0); 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 = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 }; 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}
喜欢 就关注吧,欢迎投稿!
转载地址:http://nfdtb.baihongyu.com/