android 安全之——文件加密AES加密算法、

来源:互联网 发布:汇丰银行软件开发级别 编辑:程序博客网 时间:2024/05/22 15:24

AES加密算法是目前比较流行加密方式,目前还没有针对AES有效的破解方式,比较靠谱。AES又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

AES加密数据块和密钥长度可以是128比特、192比特、256比特中的任意一个。

AES加密有很多轮的重复和变换。大致步骤如下:

1、密钥扩展(KeyExpansion),
2、初始轮(Initial Round),
3、重复轮(Rounds),每一轮又包括:SubBytes、ShiftRows、MixColumns、AddRoundKey,
4、最终轮(Final Round),最终轮没有MixColumns。


jdk中算法的支持:

 密钥长度:128位
工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
填充方式:Nopadding/PKCS5Padding/ISO10126Padding/


这里需要注意一个问题,在创建Cipher对象是需要一个第三方Provider来提供算法实现,在标准JDK中只是规定了JCE(JCE (Java Cryptography Extension) 是一组包,它们提供用于加密、密钥生成和协商以及 Message Authentication Code(MAC)算法的框架和实现。)接口,但是内部实现需要自己或者第三方提供,因此我们这里使用bouncycastle的开源的JCE实现包,下载地址:http://bouncycastle.org/latest_releases.html,我使用的是bcprov-jdk15on-150.jar  jdk1.6。

下面是Android中一种实现方式:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class AESKeyModel {  
  2.   
  3.     public static final String KEY_ALGORITHM = "AES";    
  4.     private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";    
  5.     private String srcFile="",destionFile="";  
  6.     /**  
  7.      * 初始化密钥  
  8.      *   
  9.      * @return byte[] 密钥   
  10.      * @throws Exception  
  11.      */    
  12.     public byte[] initSecretKey() {    
  13.         //返回生成指定算法的秘密密钥的 KeyGenerator 对象    
  14.         KeyGenerator kg = null;    
  15.         try {    
  16.             kg = KeyGenerator.getInstance(KEY_ALGORITHM);    
  17.         } catch (NoSuchAlgorithmException e) {    
  18.             e.printStackTrace();    
  19.             return new byte[0];    
  20.         }    
  21.         //初始化此密钥生成器,使其具有确定的密钥大小    
  22.         //AES 要求密钥长度为 128    
  23.         kg.init(128);    
  24.         //生成一个密钥    
  25.         SecretKey  secretKey = kg.generateKey();    
  26.         return secretKey.getEncoded();    
  27.     }    
  28.       
  29.     public void setDestionFile(String destionFile) {  
  30.         this.destionFile = destionFile;  
  31.     }  
  32.   
  33.     public void setSrcFile(String srcFile) {  
  34.         this.srcFile = srcFile;  
  35.     }  
  36.     /**  
  37.      * 转换密钥  
  38.      *   
  39.      * @param key   二进制密钥  
  40.      * @return 密钥  
  41.      */    
  42.     private static Key toKey(byte[] key){    
  43.         //生成密钥    
  44.         return new SecretKeySpec(key, KEY_ALGORITHM);    
  45.     }    
  46.         
  47.     /**  
  48.      * 加密  
  49.      *   
  50.      * @param data  待加密数据  
  51.      * @param key   密钥  
  52.      * @return byte[]   加密数据  
  53.      * @throws Exception  
  54.      */    
  55.     public static byte[] encrypt(byte[] data,Key key) throws Exception{    
  56.         return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);    
  57.     }    
  58.         
  59.     /**  
  60.      * 加密  
  61.      *   
  62.      * @param data  待加密数据  
  63.      * @param key   二进制密钥  
  64.      * @return byte[]   加密数据  
  65.      * @throws Exception  
  66.      */    
  67.     public static byte[] encrypt(byte[] data,byte[] key) throws Exception{    
  68.         return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);    
  69.     }    
  70.         
  71.         
  72.     /**  
  73.      * 加密  
  74.      *   
  75.      * @param data  待加密数据  
  76.      * @param key   二进制密钥  
  77.      * @param cipherAlgorithm   加密算法/工作模式/填充方式  
  78.      * @return byte[]   加密数据  
  79.      * @throws Exception  
  80.      */    
  81.     public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{    
  82.         //还原密钥    
  83.         Key k = toKey(key);    
  84.         return encrypt(data, k, cipherAlgorithm);    
  85.     }    
  86.         
  87.     /**  
  88.      * 加密  
  89.      *   
  90.      * @param data  待加密数据  
  91.      * @param key   密钥  
  92.      * @param cipherAlgorithm   加密算法/工作模式/填充方式  
  93.      * @return byte[]   加密数据  
  94.      * @throws Exception  
  95.      */    
  96.     public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{    
  97.         //实例化    
  98.         Cipher cipher = Cipher.getInstance(cipherAlgorithm);    
  99.         //使用密钥初始化,设置为加密模式    
  100.         cipher.init(Cipher.ENCRYPT_MODE, key);    
  101.         //执行操作    
  102.         return cipher.doFinal(data);    
  103.     }    
  104.         
  105.     /**  
  106.      * 解密  
  107.      *   
  108.      * @param data  待解密数据  
  109.      * @param key   二进制密钥  
  110.      * @return byte[]   解密数据  
  111.      * @throws Exception  
  112.      */    
  113.     public static byte[] decrypt(byte[] data,byte[] key) throws Exception{    
  114.         return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);    
  115.     }    
  116.         
  117.     /**  
  118.      * 解密  
  119.      *   
  120.      * @param data  待解密数据  
  121.      * @param key   密钥  
  122.      * @return byte[]   解密数据  
  123.      * @throws Exception  
  124.      */    
  125.     public static byte[] decrypt(byte[] data,Key key) throws Exception{    
  126.         return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);    
  127.     }    
  128.         
  129.     /**  
  130.      * 解密  
  131.      *   
  132.      * @param data  待解密数据  
  133.      * @param key   二进制密钥  
  134.      * @param cipherAlgorithm   加密算法/工作模式/填充方式  
  135.      * @return byte[]   解密数据  
  136.      * @throws Exception  
  137.      */    
  138.     public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{    
  139.         //还原密钥    
  140.         Key k = toKey(key);    
  141.         return decrypt(data, k, cipherAlgorithm);    
  142.     }    
  143.     
  144.     /**  
  145.      * 解密  
  146.      *   
  147.      * @param data  待解密数据  
  148.      * @param key   密钥  
  149.      * @param cipherAlgorithm   加密算法/工作模式/填充方式  
  150.      * @return byte[]   解密数据  
  151.      * @throws Exception  
  152.      */    
  153.     public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{    
  154.         //实例化    
  155.         Cipher cipher = Cipher.getInstance(cipherAlgorithm);    
  156.         //使用密钥初始化,设置为解密模式    
  157.         cipher.init(Cipher.DECRYPT_MODE, key);    
  158.         //执行操作    
  159.         return cipher.doFinal(data);    
  160.     }    
  161.         
  162.     public void encryptionFile(Key sessionKey) throws Exception {  
  163.         int len = 0;  
  164.         byte[] buffer = new byte[5 * 1024];  
  165.         byte[] cipherbuffer = null;  
  166.           
  167.         // 使用会话密钥对文件加密。  
  168.         Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM,new BouncyCastleProvider());  
  169.         IvParameterSpec iv = new IvParameterSpec("0000000000123456".getBytes());   
  170.         cipher.init(Cipher.ENCRYPT_MODE, sessionKey,iv);  
  171.   
  172.         FileInputStream fis = new FileInputStream(new File(srcFile));  
  173.         FileOutputStream fos = new FileOutputStream(new File(destionFile));  
  174.           
  175.         // 读取原文,加密并写密文到输出文件。  
  176.         while ((len = fis.read(buffer)) != -1) {  
  177.             cipherbuffer = cipher.update(buffer, 0, len);  
  178.             fos.write(cipherbuffer);  
  179.             fos.flush();  
  180.         }  
  181.         cipherbuffer = cipher.doFinal();  
  182.         fos.write(cipherbuffer);  
  183.         fos.flush();  
  184.           
  185.         if (fis != null)  
  186.             fis.close();  
  187.         if (fos != null)  
  188.             fos.close();  
  189.     }  
  190.       
  191.     public void descryptionFile(Key sessionKey) throws Exception{  
  192.         int len = 0;  
  193.         byte[] buffer = new byte[5 * 1024];  
  194.         byte[] plainbuffer = null;  
  195.           
  196.         Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM,new BouncyCastleProvider());  
  197.         IvParameterSpec iv = new IvParameterSpec("0000000000123456".getBytes());   
  198.         cipher.init(Cipher.DECRYPT_MODE,sessionKey,iv);  
  199.           
  200.         FileInputStream fis = new FileInputStream(new File(srcFile));  
  201.         FileOutputStream fos = new FileOutputStream(new File(destionFile));  
  202.           
  203.         while ((len = fis.read(buffer)) != -1){  
  204.             plainbuffer = cipher.update(buffer,0,len);  
  205.             fos.write(plainbuffer);  
  206.             fos.flush();  
  207.         }  
  208.           
  209.         plainbuffer = cipher.doFinal();  
  210.         fos.write(plainbuffer);  
  211.         fos.flush();  
  212.           
  213.         if(fis!=null)  
  214.             fis.close();  
  215.         if(fos!=null)  
  216.             fos.close();  
  217.     }  
  218.     
  219.       
  220.       
  221. }  

加密文件:

Key key_AES;
private srcjiamiFile,outjiemiFile;

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void testAESJia(String srcPath){  
  2.             File f=new File(srcPath);  
  3.                 if(!f.exists()||f.isDirectory())  
  4.                     Toast.makeText(getApplicationContext(), "该文件不合法!", Toast.LENGTH_SHORT).show();  
  5.                 else{  
  6.                     String prefix=f.getName().substring(0, f.getName().indexOf('.'));  
  7.                     String suffix=f.getName().substring(f.getName().indexOf('.'));  
  8.                     srcjiamiFile=Environment.getExternalStorageDirectory()+File.separator+prefix+"AES_jiAMi"+suffix;  
  9.                       
  10.                     AESKeyModel model_aes=new AESKeyModel();  
  11.                     model_aes.setSrcFile(srcPath);  
  12.                     model_aes.setDestionFile(srcjiamiFile);  
  13.                       
  14.                     try {  
  15.                         key_AES=new SecretKeySpec(model_aes.initSecretKey(),AESKeyModel.KEY_ALGORITHM);  
  16.                         model_aes.encryptionFile(key_AES);  
  17.                     } catch (Exception e) {  
  18.                         e.printStackTrace();  
  19.                     }  
  20.                 }  
  21.     }  

解密文件:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void testAESJieMi(String JiamiFilePath){  
  2.             File f=new File(JiamiFilePath);  
  3.                 if(!f.exists()||f.isDirectory())  
  4.                     Toast.makeText(getApplicationContext(), "该文件不合法!", Toast.LENGTH_SHORT).show();  
  5.                 else{  
  6.                     String prefix=f.getName().substring(0, f.getName().indexOf('.'));  
  7.                     String suffix=f.getName().substring(f.getName().indexOf('.'));  
  8.                     outjiemiFile=Environment.getExternalStorageDirectory()+File.separator+prefix+"AES_jieMi"+suffix;  
  9.                       
  10.                     AESKeyModel model_aes=new AESKeyModel();  
  11.                     model_aes.setSrcFile(JiamiFilePath);  
  12.                     model_aes.setDestionFile(outjiemiFile);  
  13.                       
  14.                     try {  
  15.                         model_aes.descryptionFile(key_AES);  
  16.                     } catch (Exception e) {  
  17.                         e.printStackTrace();  
  18.                     }  
  19.                 }  
  20.     }  
0 0
原创粉丝点击