新闻详情 资讯动态

全面了解最新资讯与建站知识,洞察行业趋势。

行业资讯

JAVA各种加密与解密方式

发布时间:2026/9/1 12:01:09
JAVA各种加密与解密方式 一、凯撒加密在密码学中凯撒加密是一种最简单且最广为人知的加密技术。它是一种替换加密的技术明文中的所有字母都在字母表上向后或向前按照一个固定数目进行偏移后被替换成密文。这个加密方法是以罗马共和时期恺撒的名字命名的当年恺撒曾用此方法与其将军们进行联系。public class caesarCipher { public static void main(String[] args) { String show ABCDEFGHIJKLMNOPQRSTUVWXYZ~~; int key 3; String ciphertext encryption(show, key, true); System.out.println(ciphertext); String showText encryption(ciphertext, key, false); System.out.println(showText); } /** * param text 明文/密文 * param key 位移 * param mode 加密/解密 true/false * return 密文/明文 */ private static String encryption(String text, int key, boolean mode) { char[] chars text.toCharArray(); StringBuffer sb new StringBuffer(); for (char aChar : chars) { int a mode ? aChar key : aChar - key; char newa (char) a; sb.append(newa); } return sb.toString(); } }明文字母表ABCDEFGHIJKLMNOPQRSTUVWXYZ~~密文字母表DEFGHIJKLMNOPQRSTUVWXYZ[\]注意当字符的ASCII码偏移量127密文转化出来会乱码~波浪号1263129二、Base64Base64是网络上最常见的用于传输8Bit字节码的编码方式之一Base64就是一种基于64个可打印字符来表示二进制数据的方法。base64 : A-Z a-z 0-9 /Base64要求把每三个8Bit的字节转换为四个6Bit的字节3*8 4*6 24然后把6Bit再添两位高位0组成四个8Bit的字节也就是说转换后的字符串理论上将要比原来的长1/3。import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; import com.sun.org.apache.xml.internal.security.utils.Base64; import java.nio.charset.StandardCharsets; public class base64Demo { public static void main(String[] args) throws Base64DecodingException { //MQ 一个字节补两个 System.out.println(Base64.encode(1.getBytes(StandardCharsets.UTF_8))); //MTE 两个字节补一个 System.out.println(Base64.encode(11.getBytes(StandardCharsets.UTF_8))); //MTEx System.out.println(Base64.encode(111.getBytes(StandardCharsets.UTF_8))); //解密11 System.out.println(new String(Base64.decode(MTE))); } }三、信息摘要算法MD5 或 SHA信息摘要是安全的单向哈希函数它接收任意大小的数据并输出固定长度的哈希值。import com.alibaba.fastjson.JSON; import com.sun.org.apache.xml.internal.security.utils.Base64; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; //信息摘要是安全的单向哈希函数它接收任意大小的数据并输出固定长度的哈希值。 public class DigestDemo { /** * param input 明文 * param algorithm 算法 MD5 | sha-1 SHA-256 | * return 密文 Base64 Hex */ private static String toHexOrBase64(String input, String algorithm) throws NoSuchAlgorithmException { MessageDigest digest MessageDigest.getInstance(algorithm); byte[] digest1 digest.digest(input.getBytes(StandardCharsets.UTF_8)); String base64 Base64.encode(digest1); StringBuffer haxValue new StringBuffer(); for (byte b : digest1) { //0xff是16进制数这个刚好8位都是1的二进制数而且转成int类型的时候高位会补0 int val ((int) b) 0xff;//只取得低八位 //在正数byte值的话对数值不会有改变 在负数数byte值的话对数值前面补位的1会变成0 if (val 16) { haxValue.append(0);//位数不够高位补0 } haxValue.append(Integer.toHexString(val)); } HashMapString, String DigestMap new HashMap(); DigestMap.put(Base64, base64); DigestMap.put(Hex, String.valueOf(haxValue)); return JSON.toJSONString(DigestMap); } }加密原文123456算法Base64MD54QrcOUm6WauVuBX8gIPgsha-1fEqNCco3Yq9h5ZUglD3CZJT4lBssha-256jZae727K08KaOmKSgOaGzww/XVqGr/PKEgIMkjrcbJI算法HexMD5e10adc3949ba59abbe56e057f20f883esha-17c4a8d09ca3762af61e59520943dc26494f8941bsha-2568d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92四、对称加密DesTriple DesAES采用单钥密码系统的加密方法同一个密钥可以同时用作信息的加密和解密这种加密方法称为对称加密也称为单密钥加密。常用的单向加密算法DESData Encryption Standard数据加密标准速度较快适用于加密大量数据的场合3DESTriple DES是基于DES对一块数据用三个不同的密钥进行三次加密强度更高AESAdvanced Encryption Standard高级加密标准是下一代的加密算法标准速度快安全级别高支持128、192、256位密钥的加密加密原文你好世界import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class desOrAesDemo { public static void main(String[] args) throws Exception { String text 你好世界; String key 12345678;//des必须8字节 // 算法/模式/填充 默认 DES/ECB/PKCS5Padding String transformation DES; String key1 1234567812345678;//aes必须16字节 String transformation1 AES; String key2 123456781234567812345678;//TripleDES使用24字节的key String transformation2 TripleDes; String extracted extracted(text, key, transformation, true); System.out.println(DES加密 extracted); String extracted1 extracted(extracted, key, transformation, false); System.out.println(解密 extracted1); String extracted2 extracted(text, key1, transformation1, true); System.out.println(AES加密 extracted2); String extracted3 extracted(extracted2, key1, transformation1, false); System.out.println(解密 extracted3); String extracted4 extracted(text, key2, transformation2, true); System.out.println(Triple Des加密 extracted4); String extracted5 extracted(extracted, key2, transformation2, false); System.out.println(解密 extracted5); } /** * param text 明文/base64密文 * param key 密钥 * param transformation 转换方式 * param mode 加密/解密 */ private static String extracted(String text, String key, String transformation, boolean mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { Cipher cipher Cipher.getInstance(transformation); // key 与给定的密钥内容相关联的密钥算法的名称 SecretKeySpec secretKeySpec new SecretKeySpec(key.getBytes(), transformation); //Cipher 的操作模式,加密模式ENCRYPT_MODE、 解密模式DECRYPT_MODE、包装模式WRAP_MODE 或 解包装UNWRAP_MODE cipher.init(mode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKeySpec); byte[] bytes cipher.doFinal(mode ? text.getBytes(StandardCharsets.UTF_8) : Base64.decode(text)); return mode ? Base64.encode(bytes) : new String(bytes); } }算法密匙密文DES12345678 8位jtPzTH7ttEeKFrJaLY8OwmOezdN8hFAES12345678*2 16位/cq03JhyvrTIJyYvWwc2Dc/bFUBNKelKPSANnWgsAwTripleDes12345678*3 24位jtPzTH7ttEeKFrJaLY8OwmOezdN8hF五、非对称加密公钥加密也叫非对称密钥加密public key encryption属于通信科技下的网络安全二级学科指的是由对应的一对唯一性密钥即公开密钥和私有密钥组成的加密方法。它解决了密钥的发布和管理问题是商业密码的核心。在公钥加密体制中没有公开的是私钥公开的是公钥。常用的算法RSA、ElGamal、背包算法、Rabin(Rabin的加密法可以说是RSA方法的特例)、Diffie-Hellman (D-H) 密钥交换协议中的公钥加密算法、Elliptic Curve CryptographyECC,椭圆曲线加密算法。1.生成公钥和私钥文件目前JDK1.8支持 RSA、DSA、DIFFIEHELLMAN、EC/** * 生成公钥和私钥文件 * param algorithm 算法 * param privatePath 私钥路径 * param publicPath 公钥路径 */ private static void generateKeyFile(String algorithm, String privatePath, String publicPath) throws NoSuchAlgorithmException, IOException { //返回生成指定算法的 public/private 密钥对的 KeyPairGenerator 对象 KeyPairGenerator keyPairGenerator KeyPairGenerator.getInstance(algorithm); //生成一个密钥对 KeyPair keyPair keyPairGenerator.generateKeyPair(); //私钥 PrivateKey privateKey keyPair.getPrivate(); //公钥 PublicKey publicKey keyPair.getPublic(); byte[] privateKeyEncoded privateKey.getEncoded(); byte[] publicKeyEncoded publicKey.getEncoded(); String privateEncodeString Base64.encode(privateKeyEncoded); String publicEncodeString Base64.encode(publicKeyEncoded); //需导入commons-io FileUtils.writeStringToFile(new File(privatePath), privateEncodeString, StandardCharsets.UTF_8); FileUtils.writeStringToFile(new File(publicPath), publicEncodeString, StandardCharsets.UTF_8); }2.使用RSA进行加密、解密package cryptography; import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; import com.sun.org.apache.xml.internal.security.utils.Base64; import org.apache.commons.io.FileUtils; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; public class RSADemo { public static void main(String[] args) throws Exception { String text 你好世界; String algorithm RSA; PublicKey publicKey getPublicKey(algorithm, rsaKey/publicKey2.txt); PrivateKey privateKey getPrivateKey(algorithm, rsaKey/privateKey2.txt); String s RSAEncrypt(text, algorithm, publicKey); String s1 RSADecrypt(s, algorithm, privateKey); System.out.println(s); System.out.println(s1); //generateKeyFile(DSA,D:\\privateKey2.txt,D:\\publicKey2.txt); } /** * 获取公钥,key * param algorithm 算法 * param publicPath 密匙文件路径 * return */ private static PublicKey getPublicKey(String algorithm, String publicPath) throws IOException, NoSuchAlgorithmException, Base64DecodingException, InvalidKeySpecException { String publicEncodeString FileUtils.readFileToString(new File(publicPath), StandardCharsets.UTF_8); //返回转换指定算法的 public/private 关键字的 KeyFactory 对象。 KeyFactory keyFactory KeyFactory.getInstance(algorithm); //此类表示根据 ASN.1 类型 SubjectPublicKeyInfo 进行编码的公用密钥的 ASN.1 编码 X509EncodedKeySpec x509EncodedKeySpec new X509EncodedKeySpec(Base64.decode(publicEncodeString)); return keyFactory.generatePublic(x509EncodedKeySpec); } /** * 获取私钥key * param algorithm 算法 * param privatePath 密匙文件路径 * return */ private static PrivateKey getPrivateKey(String algorithm, String privatePath) throws IOException, NoSuchAlgorithmException, Base64DecodingException, InvalidKeySpecException { String privateEncodeString FileUtils.readFileToString(new File(privatePath), StandardCharsets.UTF_8); //返回转换指定算法的 public/private 关键字的 KeyFactory 对象。 KeyFactory keyFactory KeyFactory.getInstance(algorithm); //创建私钥key的规则 此类表示按照 ASN.1 类型 PrivateKeyInfo 进行编码的专用密钥的 ASN.1 编码 PKCS8EncodedKeySpec pkcs8EncodedKeySpec new PKCS8EncodedKeySpec(Base64.decode(privateEncodeString)); //私钥对象 return keyFactory.generatePrivate(pkcs8EncodedKeySpec); } /** * 加密 * param text 明文 * param algorithm 算法 * param key 私钥/密钥 * return 密文 */ private static String RSAEncrypt(String text, String algorithm, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException { Cipher cipher Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] bytes cipher.doFinal(text.getBytes(StandardCharsets.UTF_8)); return Base64.encode(bytes); } /** * 解密 * param extracted 密文 * param algorithm 算法 * param key 密钥/私钥 * return String 明文 */ private static String RSADecrypt(String extracted, String algorithm, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, Base64DecodingException, NoSuchProviderException { Cipher cipher Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, key); byte[] bytes1 cipher.doFinal(Base64.decode(extracted)); return new String(bytes1); } /** * 生成公钥和私钥文件 * param algorithm 算法 * param privatePath 私钥路径 * param publicPath 公钥路径 */ private static void generateKeyFile(String algorithm, String privatePath, String publicPath) throws NoSuchAlgorithmException, IOException { //返回生成指定算法的 public/private 密钥对的 KeyPairGenerator 对象 KeyPairGenerator keyPairGenerator KeyPairGenerator.getInstance(algorithm); //生成一个密钥对 KeyPair keyPair keyPairGenerator.generateKeyPair(); //私钥 PrivateKey privateKey keyPair.getPrivate(); //公钥 PublicKey publicKey keyPair.getPublic(); byte[] privateKeyEncoded privateKey.getEncoded(); byte[] publicKeyEncoded publicKey.getEncoded(); String privateEncodeString Base64.encode(privateKeyEncoded); String publicEncodeString Base64.encode(publicKeyEncoded); //需导入commons-io FileUtils.writeStringToFile(new File(privatePath), privateEncodeString, StandardCharsets.UTF_8); FileUtils.writeStringToFile(new File(publicPath), publicEncodeString, StandardCharsets.UTF_8); } }密文明文你好世界ZBadyYCIck2iYV8RtsY35T1GbaYt9aLS51dcws5H4IcrOHi6/8AIEdgtwJO3p1ccqKP6XTwQAWmceJ7kpsk76nvFD8Hg2pLYzH2oEEoy07bLBdBiEzVFkP0DLnrsHO4elQxc9BSslj5wGLQqbb1Mxh9Tcpf5zJEOxdBZvE六、查看系统支持的算法public static void main(String[] args) throws Exception { System.out.println(列出加密服务提供者:); Provider[] proSecurity.getProviders(); for(Provider p:pro){ System.out.println(Provider:p.getName() - version:p.getVersion()); System.out.println(p.getInfo()); } System.out.println(); System.out.println(列出系统支持的消息摘要算法); for(String s:Security.getAlgorithms(MessageDigest)){ System.out.println(s); } System.out.println(); System.out.println(列出系统支持的生成公钥和私钥对的算法); for(String s:Security.getAlgorithms(KeyPairGenerator)){ System.out.println(s); } }最推荐的方案是Hutool Bouncy Castle。这个组合既有Bouncy Castle的强大算法支持又有Hutool提供的简洁APIdependency groupIdcn.hutool/groupId artifactIdhutool-all/artifactId version5.8.40/version /dependency dependency groupIdorg.bouncycastle/groupId !--JDK ≤ 8bcprov-jdk15to18 -- !--JDK ≥ 9优先 bcprov-jdk18on -- artifactIdbcprov-jdk15to18/artifactId version1.85.2/version /dependency七、国密算法20260831补充‌国密算法‌是由中国国家密码管理局认定的自主可控国产密码算法体系主要用于保障国家信息安全涵盖对称加密、非对称加密、哈希算法及流密码等类型 。它旨在减少对外部密码产品的依赖。1.非对称加密SM2国家标准委SM2密码算法使用规范import cn.hutool.core.util.HexUtil; import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.SM2; import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; public class sm2Test { public static void main(String[] args) { // 注册 Bouncy Castle 安全提供者Hutool 会自动注册但显式注册更稳妥 Security.addProvider(new BouncyCastleProvider()); // 方式一使用 SmUtil.sm2() 生成密钥对 SM2 sm2 SmUtil.sm2(); // 获取私钥的 D 值32字节 byte[] privateKeyD sm2.getD(); // 获取公钥点 Q非压缩格式以 04 开头 byte[] publicKeyQ sm2.getQ(false); // 打印 Hex 格式16进制字符串 String privateKeyHex HexUtil.encodeHexStr(privateKeyD); String publicKeyHex HexUtil.encodeHexStr(publicKeyQ); System.out.println( Hex 格式 ); System.out.println(私钥 (Hex): privateKeyHex); System.out.println(私钥长度: privateKeyHex.length() 字符 (对应 privateKeyD.length 字节)); System.out.println(公钥 (Hex): publicKeyHex); System.out.println(公钥长度: publicKeyHex.length() 字符 (对应 publicKeyQ.length 字节)); // 方式二获取标准 X.509 / PKCS#8 格式 // 公钥为 X.509 格式私钥为 PKCS#8 格式 String publicKeyBase64 sm2.getPublicKeyBase64(); String privateKeyBase64 sm2.getPrivateKeyBase64(); System.out.println(\n Base64 格式 (X.509 / PKCS#8) ); System.out.println(公钥 (Base64): publicKeyBase64); System.out.println(私钥 (Base64): privateKeyBase64); // 验证加密和解密 String plainText Hello, SM2!; System.out.println(\n 加解密验证 ); System.out.println(原文: plainText); // 公钥加密 String encrypted sm2.encryptHex(plainText, KeyType.PublicKey); System.out.println(密文 (Hex): encrypted); // 私钥解密 String decrypted sm2.decryptStr(encrypted, KeyType.PrivateKey); System.out.println(解密后: decrypted); /* Hex 格式 私钥 (Hex): 008c9ad32cc71375f71f9589c764e59c6f5e45a032afd0193cdbb8714e20bff304 私钥长度: 66 字符 (对应 33 字节) 公钥 (Hex): 0425aa02efe62ed52c42d83024c536ce48a4568c237521bc2cdcdcd3f0c36e1d2db68798c385660f6183650f0a6db65a6079adac14aafe178f5faa59fd89002515 公钥长度: 130 字符 (对应 65 字节) */ } }八、一些概念1.数字信封Digital Envelope场景A 公司要把自己的 SM2 私钥绝密数据安全地传输给 B 公司。痛点私钥本身是绝密的不能直接在互联网上明文传输。解决办法A 公司使用 B 公司的公钥B公司自己生成给这个私钥加一层“保护壳”数字信封。只有 B 公司用自己的私钥才能拆开这个壳拿到里面的私钥。2.非对称加密、解密与签名、验签加密与解密保护数据机密性加密用公钥Q发送方使用接收方的公钥Q来加密消息解密用私钥d接收方使用自己的私钥d来解密。签名与验签确认身份与完整性签名用私钥d签名者使用自己的私钥d对消息的哈希值进行运算生成签名(r, s)。验签用公钥Q验证者使用签名者的公钥Q来验证签名。总而言之公钥Q和私钥d是SM2算法的一体两面。Q是公开的“身份标识”和“锁”用于加密和验签而d是保密的“钥匙”用于解密和签名。①. 厘清核心原则请记住这个铁律私钥 身份证明只能由生成者本人持有永不公开。公钥 印章或锁可以公开分发给任何人。②. 真实的通信场景发送方 → 接收方假设你是发送方Alice对方是接收方Bob。你要给Bob发一份既加密别人看不了又签名证明是你发的的数据。流程是这样的Bob接收方要解密数据Bob会把自己的公钥Q_bob提前公开给你或给你一个人。你用Bob的公钥加密数据。数据发过去后Bob用自己的私钥d_bob解密。Bob的私钥全程只在他自己的电脑里绝对不会发给你。Alice发送方要签名数据你需要用你自己的私钥d_alice对数据进行签名。然后你把“加密后的数据 你的签名”一起发给Bob。Bob接收方要验证签名Bob收到数据后为了确认这确实是你发的他会去获取你的公钥Q_alice你提前公开在官网、名片或证书上的。Bob用你的公钥来验证这个签名是否有效。签名用自己的私钥自己留着验签用对方的公钥对方公开加密用对方的公钥对方公开解密用自己的私钥自己留着。整个链条中没有任何一个环节需要你把私钥交给对方或让对方把私钥交给你。

想做一个「会获客」的企业网站?

留下需求,1 小时内获取专属建站方案与透明报价。

免费咨询方案