Java实现AES加密解密
2017-07-06 17:07
399 查看
转自http://blog.csdn.net/heqiangflytosky/article/details/51721122
public class AESUtils {
private static final String KEY_ALGORITHM = “AES”;
}
public class AESUtils {
private static final String KEY_ALGORITHM = “AES”;
private static final Charset charset = Charset.forName( "utf-8" ); private static String decrypt( byte[] data, byte[] key ) { try { Key k = new SecretKeySpec( key, KEY_ALGORITHM ); Cipher cipher = Cipher.getInstance( KEY_ALGORITHM ); cipher.init( Cipher.DECRYPT_MODE, k ); return new String( cipher.doFinal( data ), charset ); } catch( Exception e ) { throw new RuntimeException( e ); } } public static String decrypt( String data, String key ) { return decrypt( parseHexStr2Byte( data ), key.getBytes( charset ) ); } public static byte[] encrypt( byte[] data, byte[] key ) { try { Key k = new SecretKeySpec( key, KEY_ALGORITHM ); Cipher cipher = Cipher.getInstance( KEY_ALGORITHM ); cipher.init( Cipher.ENCRYPT_MODE, k ); return cipher.doFinal( data ); } catch( Exception e ) { throw new RuntimeException( e ); } } public static String encrypt( String data, String key ) { byte[] dataBytes = data.getBytes( charset ); byte[] keyBytes = key.getBytes( charset ); return parseByte2HexStr( encrypt( dataBytes, keyBytes ) ); } public static byte[] parseHexStr2Byte( String hexStr ) { 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; } public static String parseByte2HexStr( byte buf[] ) { StringBuilder sb = new StringBuilder(); 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(); }
}
相关文章推荐
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- AES加密解密->java实现
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
- java实现对称加密AES和DES的加密、解密
- java和js实现aes加密解密
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- java实现AES加密解密
- java实现AES加密解密
- AES加密php,java,.net三种语言同步实现加密、解密
- RSA AES 前端JS与后台JAVA的加密解密的是实现
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现[转载]
- JAVA实现AES加密和解密
- AES 加密解密的JAVA实现方式【一】已调通
- C# 实现 JAVA AES加密解密[原创]
- PHP、Java、.Net和Javascript的AES加密解密实现
- java和js实现aes加密解密
- java和js实现aes加密解密
- Vickate_AES加密与解密(Objective-C版),暂未实现与java的交互(求大神帮助)
- Go语言解密上篇中用java aes实现的加密
- [Java 实现AES加密解密]