Java code lib aes 加解密
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java code lib aes 加解密,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2945字,纯文字阅读大概需要5分钟。
内容图文
![Java code lib aes 加解密](/upload/InfoBanner/zyjiaocheng/741/ab0f2e0293d246809063e43cbc8fc2ce.jpg)
Java aes 加解密
/** * Created by LvJianwei on 2018/2/8. */ import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import java.security.NoSuchAlgorithmException; import java.util.Arrays; /** * @program: reflection * @description: AES * @author: LvJianwei * @create: 2018-02-08 15:07 **/ public class AESDemo { public static void main(String[] args) { String key="i will always love you"; byte[] keyBytes=initSecretKey(); String plainText="have a nice day"; System.out.println("plainText:"+plainText); byte[] encryptedBytes=encrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,plainText); System.out.println("encryptedText:"+Arrays.toString(encryptedBytes)); byte[] decryptedBytes=decrypt(DEFAULT_CIPHER_ALGORITHM,keyBytes,KEY_ALGORITHM,encryptedBytes); System.out.println("decryptedText:"+new String(decryptedBytes)); } /** * algorithm */ private static final String KEY_ALGORITHM = "AES"; private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * generate key * @return byte[] key * @throws Exception */ public static byte[] initSecretKey() { //返回生成指定算法的秘密密钥的 KeyGenerator 对象 KeyGenerator kg = null; try { kg = KeyGenerator.getInstance(KEY_ALGORITHM); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return new byte[0]; } //初始化此密钥生成器,使其具有确定的密钥大小 //AES 要求密钥长度为 128 kg.init(128); //生成一个密钥 SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } /** * AESEncrypt * @param cipherAlgorithm transformation * @param keyBytes key byte array * @param keyAlgorithm SecretKeySpec's algorithm * @param plainText text to be encrypted * @return */ public static byte[] encrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, String plainText) { try { Cipher cipher = Cipher.getInstance(cipherAlgorithm); String keyStr= DatatypeConverter.printBase64Binary(keyBytes); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encyptedBytes = cipher.doFinal(plainText.getBytes()); return encyptedBytes; } catch (Exception e) { e.printStackTrace(); } return null; } /** * decrypt * @param cipherAlgorithm transformation * @param keyBytes key * @param keyAlgorithm SecretKeySpec's algorithm * @param encyptedBytes encypted byte array * @return */ public static byte[] decrypt(String cipherAlgorithm, byte[] keyBytes, String keyAlgorithm, byte[] encyptedBytes) { try { Cipher cipher = Cipher.getInstance(cipherAlgorithm); String keyStr= DatatypeConverter.printBase64Binary(keyBytes); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, keyAlgorithm); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decryptedBytes = cipher.doFinal(encyptedBytes); return decryptedBytes; } catch (Exception e) { e.printStackTrace(); } return null; } }
转载于:https://www.cnblogs.com/lvjianwei/p/8432679.html
内容总结
以上是互联网集市为您收集整理的Java code lib aes 加解密全部内容,希望文章能够帮你解决Java code lib aes 加解密所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。