1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class MyKeyGenerator { public static void main(String[] args) { try { String word = "要加密的" ; String ALGORITHM= "AES" ; System.out.println(byteToHexString(word.getBytes())); KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init( 128 , new SecureRandom()); //默认是128 AES要求密钥长度为128,192,256位 SecretKey secretKey = keyGenerator.generateKey(); //生成密钥 byte [] bytes = secretKey.getEncoded(); String key = byteToHexString(bytes);<br> //String key = HexBin.encode(bytes); System.out.println( "16进制的密钥:" +key); //String key2 = toHexString(bytes); //System.out.println(key2); //AES加密 SecretKey secretKey2 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey2); byte [] cipherByte = cipher.doFinal(word.getBytes()); //加密 String result = byteToHexString(cipherByte); System.out.println( "AES加密结果:" +result); //AES解密 SecretKey secretKey3 = new SecretKeySpec(hexStringToBytes(key), ALGORITHM); //恢复密钥<br> //SecretKey secretKey2 = new SecretKeySpec(HexBin.decode(key), ALGORITHM); Cipher cipher2 = Cipher.getInstance(ALGORITHM); //Cipher完成加密或解密工作类 cipher2.init(Cipher.DECRYPT_MODE, secretKey3); //对Cipher初始化,解密模式 byte [] cipherByte2 = cipher2.doFinal(hexStringToBytes(result)); //解密data System.out.println( "AES解密结果:" +byteToHexString(cipherByte2)); System.out.println( new String(cipherByte2)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * byte数组转化为16进制字符串 * @param bytes * @return */ public static String byteToHexString( byte [] bytes) { StringBuffer sb = new StringBuffer(); for ( int i = 0 ; i < bytes.length; i++) { String strHex=Integer.toHexString(bytes[i]); if (strHex.length() > 3 ) { sb.append(strHex.substring( 6 )); } else { if (strHex.length() < 2 ) { sb.append( "0" + strHex); } else { sb.append(strHex); } } } return sb.toString(); } /** * 16进制字符串转为byte数组 * @param hexString * @return */ public static byte [] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals( "" )) { return null ; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2 ; char [] hexChars = hexString.toCharArray(); byte [] d = new byte [length]; for ( int i = 0 ; i < length; i++) { int pos = i * 2 ; d[i] = ( byte ) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1 ])); } return d; } private static byte charToByte( char c) { return ( byte ) "0123456789ABCDEF" .indexOf(c); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端