JAVA实现AES加密
JAVA实现AES加密
java实现AES加密,直接上代码:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;/**
*这个示例程序展示了如何用Java实现AES加密和解密。请注意
*密钥和加密的文本是不可读的二进制文件,因此在下面的程序中,我们
*以基础字节的十六进制格式显示。
*/
public class AESEncryption {private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();/*** 1. Generate a plain text for encryption 2. Get a secret key (printed in hexadecimal form). In actual use this must by encrypted and kept safe. The same key is required for decryption.*/public static void main(String[] args) throws Exception {String plainText = "Hello World";SecretKey secKey = getSecretEncryptionKey();byte[] cipherText = encryptText(plainText, secKey);String decryptedText = decryptText(cipherText, secKey);System.out.println("Original Text:" + plainText);System.out.println("AES Key (Hex Form):" + bytesToHex(secKey.getEncoded()));System.out.println("Encrypted Text (Hex Form):" + bytesToHex(cipherText));System.out.println("Descrypted Text:" + decryptedText);}/*** gets the AES encryption key. In your actual programs, this should be safely stored.** @return secKey (Secret key that we encrypt using it)* @throws NoSuchAlgorithmException (from KeyGenrator)*/public static SecretKey getSecretEncryptionKey() throws NoSuchAlgorithmException {KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES");aesKeyGenerator.init(128); // The AES key size in number of bitsSecretKey secKey = aesKeyGenerator.generateKey();return secKey;}/*** Encrypts plainText in AES using the secret key** @param plainText* @param secKey* @return byteCipherText (The encrypted text)* @throws NoSuchPaddingException (from Cipher)* @throws NoSuchAlgorithmException (from Cipher)* @throws InvalidKeyException (from Cipher)* @throws BadPaddingException (from Cipher)* @throws IllegalBlockSizeException (from Cipher)*/public static byte[] encryptText(String plainText, SecretKey secKey)throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,IllegalBlockSizeException, BadPaddingException {// AES defaults to AES/ECB/PKCS5Padding in Java 7Cipher aesCipher = Cipher.getInstance("AES");aesCipher.init(Cipher.ENCRYPT_MODE, secKey);byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());return byteCipherText;}/*** Decrypts encrypted byte array using the key used for encryption.** @param byteCipherText* @param secKey* @return plainText* @throws NoSuchPaddingException* @throws NoSuchAlgorithmException* @throws InvalidKeyException* @throws BadPaddingException* @throws IllegalBlockSizeException*/public static String decryptText(byte[] byteCipherText, SecretKey secKey)throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,IllegalBlockSizeException, BadPaddingException {// AES defaults to AES/ECB/PKCS5Padding in Java 7Cipher aesCipher = Cipher.getInstance("AES");aesCipher.init(Cipher.DECRYPT_MODE, secKey);byte[] bytePlainText = aesCipher.doFinal(byteCipherText);return new String(bytePlainText);}/*** Convert a binary byte array into readable hex form Old library is deprecated on OpenJdk 11 and** @param hash (in binary)* @return hexHash*/public static String bytesToHex(byte[] bytes) {char[] hexChars = new char[bytes.length * 2];for (int j = 0; j < bytes.length; j++) {int v = bytes[j] & 0xFF;hexChars[j * 2] = HEX_ARRAY[v >>> 4];hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];}return new String(hexChars);}
}
JAVA实现AES加密相关推荐
- .NET对JAVA进行AES加密(SHA1PRNG)及解密方法
第一步:生成对应JAVA AES加密方法的KEY值 private static byte[] encryptJavaAes(string aesKey) { ...
- java php aes加密解密_php aes 加密解密可与java对接
php aes 加密解密可与java对接 博主:liu1693 发表时间:2017-02-23 16:52:27 浏览量:100 class Encrypt{ //加密方法 public static ...
- Java使用AES加密解密
AES加密机制: 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准. 这个标准用来替代原先的 ...
- java android aes加密解密_AES加密解密在JAVA和ANDROID下互通
昨天外包安卓的那个人说AES的加解密结果不一样.于是百度搜索发现还真是! 贴上AES加密核心: Cipher cipher = Cipher.getInstance("AES/CBC/PKC ...
- ios java aes_PHP7 AES加密解密函数_兼容ios/andriod/java对等加解密
**PHP7.0 7.1 7.2 7.3 AES对等加解密类 函数文件_兼容ios/andriod/java等** 由于新项目规划要求使用PHP7.2开发环境,但在部分新系统中仍需使用AES加解密方式 ...
- java aes加密 base64_【java】AES加密解密|及Base64的使用
AES加解密算法,使用Base64做转码以及辅助加密: package com.wintv.common; import javax.crypto.Cipher; import javax.crypt ...
- java使用aes加密文件内容
aes是什么? aes是对称加密的一种,什么是对称加密呢?就是加密和解密使用相同的秘钥的加密算法,显而易见,aes加密和md5不同,aes是可逆的,aes加密算法用于替代以前的des加密算法. 使用场 ...
- java aes iv 24位_【JAVA】AES加密 简单实现 AES-128/ECB/PKCS5Padding
AES加密 AES 是一种可逆加密算法,对用户的敏感信息加密处理. 本文暂不深入AES原理,仅关注JAVA代码实现AES加解密. JAVA代码实现 这是一个在线AES加密网站.从页面上我们可以看到如下 ...
- java实现AES加密解密
public class PartnerSign {private PartnerSign() {}/*** AES加密方法** @param content 明文内容* @param passwor ...
最新文章
- HTTP基础认证Basic Authentication
- 前端学习(2881):初始化dom和数据化线程池
- HistCite 的使用方法
- 打通JAVA与内核系列之一ReentrantLock锁的实现原理
- 《梦断代码》读书笔记1
- Redis的主从复制和 哨兵模式
- java 数据挖掘 开源_5个开源数据挖掘工具,收下这波干货
- 使用Python的Arcpy.mapping 模块自动化制图
- 阿里巴巴android图标素材网,阿里巴巴矢量图标库
- mobile的一些link
- c语言题库打不开软件,编写题库程序_想把一个老师编的做题练习的软件里面的题库弄出来貌似是用VB60编写的要怎么做啊_淘题吧...
- 程序员年薪百万,原来是吃到了这样的红利!
- 为什么要学习凸优化?
- 情景式领导力学习(2) - 实践案例及思考
- Windows下etc文件夹
- 对keep-alive的理解是什么?
- TCP/UDP/IP/Socket的定义
- The World is Flat 世界是平坦的
- go语言工具_Concurrent Map
- OCJP(1Z0-851) 模拟题分析(一)
热门文章
- 引用 教你 如何映像劫持杀毒软件
- Android携程高级用法,Android真正仿携程首页view实现
- EXP-00091: Exporting questionable statistics.错误解决方案
- python itchat_Python使用微信itchat接口实现查看自己微信的信息功能详解
- 数据挖掘-Task4:建模与调参
- JSP 虚拟路径设置
- Photoscan/Metashape与Contextcapture联合建模
- 变色龙扑克牌游戏V1.9.0.253正式版基本秘籍(Basic Cheats)
- php判断是否支持rewrite,从PHP $_SERVER相关参数判断是否支持Rewrite模块
- 程序员小灰2020年整理