aes加密

/** 
     * 加密 
     *  
     * @param content 需要加密的内容 
     * @param password  加密密码 
     * @return 
     */  
    public static String encrypt(String content, String password) {  
            try {    
                    KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
                    /**
                     * SecureRandom 实现尝试完全随机化生成器本身的内部状态,
                     * 除非调用方在调用 getInstance 方法之后又调用了setSeed 方法
                     */
                    SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
                    random.setSeed(password.getBytes());
                    kgen.init(128,random);  
                    SecretKey secretKey = kgen.generateKey();  
                    byte[] enCodeFormat = secretKey.getEncoded();  
                    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  
                    Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
                    byte[] byteContent = content.getBytes("utf-8");  
                    cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化   
                    byte[] result = cipher.doFinal(byteContent);  
                    //二进制转换为16
                    StringBuffer sb = new StringBuffer();  
                    for (int i = 0; i < result.length; i++) {  
                            String hex = Integer.toHexString(result[i] & 0xFF);  
                            if (hex.length() == 1) {  
                                    hex = '0' + hex;  
                            }  
                            sb.append(hex.toUpperCase());  
                    }
                    return sb.toString(); // 加密 
            } catch (NoSuchAlgorithmException e) {  
                    e.printStackTrace();  
            } catch (NoSuchPaddingException e) {  
                    e.printStackTrace();  
            } catch (InvalidKeyException e) {  
                    e.printStackTrace();  
            } catch (UnsupportedEncodingException e) {  
                    e.printStackTrace();  
            } catch (IllegalBlockSizeException e) {  
                    e.printStackTrace();  
            } catch (BadPaddingException e) {  
                    e.printStackTrace();  
            }  
            return null;  
    }  
    /**解密 
     * @param content  待解密内容 
     * @param password 解密密钥 
     * @return 
     */  
    public static byte[] decrypt(String content, String password) {  
            try {      
                    //转换为2禁止
                    if(content==null)
                        return null;
                     if (content.length() < 1)  
                            return null;  
                    byte[] con = new byte[content.length()/2];  
                    for (int i = 0;i< content.length()/2; i++) {  
                            int high = Integer.parseInt(content.substring(i*2, i*2+1), 16);  
                            int low = Integer.parseInt(content.substring(i*2+1, i*2+2), 16);  
                            con[i] = (byte) (high * 16 + low);  
                    }  
                     KeyGenerator kgen = KeyGenerator.getInstance("AES");
                     SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
                     random.setSeed(password.getBytes());
                     kgen.init(128, random);  
                     SecretKey secretKey = kgen.generateKey();  
                     byte[] enCodeFormat = secretKey.getEncoded();  
                     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");              
                     Cipher cipher = Cipher.getInstance("AES");// 创建密码器   
                    cipher.init(Cipher.DECRYPT_MODE, key);// 初始化   
                    byte[] result = cipher.doFinal(con);  
                    return result; // 加密   
            } catch (NoSuchAlgorithmException e) {  
                    e.printStackTrace();  
            } catch (NoSuchPaddingException e) {  
                    e.printStackTrace();  
            } catch (InvalidKeyException e) {  
                    e.printStackTrace();  
            } catch (IllegalBlockSizeException e) {  
                    e.printStackTrace();  
            } catch (BadPaddingException e) {  
                    e.printStackTrace();  
            }  
            return null;  
    } 

 

你可能感兴趣的:(aes加密)