zoukankan      html  css  js  c++  java
  • AES加密解密在JAVA和ANDROID下互通

    <span style="font-family: Arial, Helvetica, sans-serif;">昨天外包安卓的那个人说AES的加解密结果不一样。于是百度搜索发现还真是!</span>

    贴上AES加密核心:

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv)
    ;

    CBC是工作模式,AES一共同拥有电子password本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种模式。PKCS5Padding是填充模式,还有其他的填充模式:然后,cipher.init()一共同拥有三个參数:Cipher.ENCRYPT_MODE, key, zeroIv,zeroIv就是初始化向量,一个8为字符数组。工作模式、填充模式、初始化向量这三种因素一个都不能少。否则,假设你不指定的话。那么就要程序就要调用默认实现。

    知道原因就好办,各种调试測试之后完毕AES在JAVA和安桌互通。

    现贴上核心代码:

    /** 填充模式 */
    	private static final String transformation = "AES/CBC/PKCS5Padding";	
    	/**
    	 * 加密
    	 * 
    	 * @param content 须要加密的内容
    	 * @param password 加密密码
    	 * @return
    	 */
    	public static String encrypt(String content, String password) {
    		try {
    			IvParameterSpec zeroIv = new IvParameterSpec(password.getBytes());
    			SecretKeySpec key1 = new SecretKeySpec(password.getBytes(),"AES");
    			Cipher cipher = Cipher.getInstance(transformation);
    			cipher.init(Cipher.ENCRYPT_MODE, key1, zeroIv);
    			byte[] encryptedData = cipher.doFinal(content.getBytes());
    	        String encryptResultStr = parseByte2HexStr(encryptedData);
    	        return encryptResultStr;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	/**
    	 * 解密
    	 * 
    	 * @param content 待解密内容
    	 * @param password 解密密钥
    	 * @return
    	 */
    	public static String decrypt(String content, String password) {
    		try {
    			
    			byte[] decryptFrom = parseHexStr2Byte(content);
    			IvParameterSpec zeroIv = new IvParameterSpec(password.getBytes());
    			SecretKeySpec key1 = new SecretKeySpec(password.getBytes(),"AES");
    			Cipher cipher = Cipher.getInstance(transformation);
    			cipher.init(Cipher.DECRYPT_MODE, key1, zeroIv);
    			byte decryptedData[] = cipher.doFinal(decryptFrom);
    			 return new String(decryptedData);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    	
    	/**将二进制转换成16进制 
    	 * @param buf 
    	 * @return 
    	 */  
    	public static String parseByte2HexStr(byte buf[]) {  
            StringBuffer sb = new StringBuffer();  
            for (int i = 0; i < buf.length; i++) {  
                    String hex = Integer.toHexString(buf[i] & 0xFF);  
                    if (hex.length() == 1) {  
                            hex = '0' + hex;  
                    }  
                    sb.append(hex.toUpperCase());  
            }  
            return sb.toString();  
    	}  
    	
    	/**将16进制转换为二进制 
    	 * @param hexStr 
    	 * @return 
    	 */  
    	public static byte[] parseHexStr2Byte(String hexStr) {  
            if (hexStr.length() < 1)  
                    return null;  
            byte[] result = new byte[hexStr.length()/2];  
            for (int i = 0;i< hexStr.length()/2; i++) {  
                    int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
                    int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
                    result[i] = (byte) (high * 16 + low);  
            }  
            return result;  
    	}  
    最后我想说一下,花了积分最后还是解决不了互通。我仅仅想说不带这样骗积分的。



  • 相关阅读:
    JavaScript中的闭包
    SQL 备忘
    SqlServer 2005 升级至SP2过程中出现"身份验证"无法通过的问题
    unable to start debugging on the web server iis does not list an application that matches the launched url
    Freebsd 编译内核
    Freebsd 6.2中关于无线网络的设定
    【Oracle】ORA01219
    【Linux】Windows到Linux的文件复制
    【Web】jar命令行生成jar包
    【Linux】CIFS挂载Windows共享
  • 原文地址:https://www.cnblogs.com/llguanli/p/8416992.html
Copyright © 2011-2022 走看看