赞
踩
对称加密算法AES是目前广泛使用的一种加密技术,它采用相同的密钥来进行数据的加密和解密。
AES的优点
AES的注意事项
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; /** * AES 加密和解密 */ public class AesTest { // 秘钥 private static String KEY = "aaccB1123Mopac31"; // 向量 private static String IV = "CdmccsaCCcdsADf"; /** * 加密 * @param content 待加密内容 * */ public static String encrypt(String content) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8))); return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8))); } /** * 解密 * @param content 加密内容 * */ public static String decrypt(String content) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8))); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(decryptedBytes, StandardCharsets.UTF_8); } public static void main(String[] args) throws Exception { String text = "加密内容"; String encryptText = encrypt(text); System.out.println("加密后内容:" + encryptText); System.out.println("解密后内容:" + decrypt(encryptText)); } }
RSA是一种非对称加密算法,使用一对公钥和私钥来进行加密和解密操作,公钥用于加密数据,私钥用于解密数据。
RSA的优点
RSA的注意事项
加密场景
验签场景
import javax.crypto.Cipher; import java.security.*; import java.util.Base64; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; /** * RSA 加密解密和签名验签 */ public class RsaTest { // 公钥 private static String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHDYWXIMltI+zia0lrxm1dJu96" + "QkogDVLsaFXxnjCZl72evCU0CAlWGDPc3gzJYFNw1KZvNRuTTiKbGa0enFc8hDoG" + "Sjg37Na8OqMzLZZ/N6/DCAUywk1JNhMfYDr0GSYNgV37NXWvKD3rbvfxCkoH3ojn" + "B3zUgr31VQfryzXq+QIDAQAB"; // 私钥 private static String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMcNhZcgyW0j7OJr" + "SWvGbV0m73pCSiANUuxoVfGeMJmXvZ68JTQICVYYM9zeDMlgU3DUpm81G5NOIpsZ" + "rR6cVzyEOgZKODfs1rw6ozMtln83r8MIBTLCTUk2Ex9gOvQZJg2BXfs1da8oPetu" + "9/EKSgfeiOcHfNSCvfVVB+vLNer5AgMBAAECgYBNTSADJ9VxkBEPgRC7TQztrxAz" + "T4kP13zLmF0//unqy/X0riZYhYJyr+V8MbOPhNwCpmKYL2ytERv5VchYaeiqRBsQ" + "vVqVhz5oBGmVxyrHsfiRl4F+3vUA/qeewmNl/rFTxXBH5T3J0SzppSD6Z9+gdPiy" + "wl51fut+FqggqbiH/QJBAP9ETAF+qKM5pW2KHhNbeKwxc2e0y3fvxAEfFKD6GakT" + "d38QMTULN6kmZl3vhJXSxY/Xt8OjZVOvOTWj4yoqXycCQQDHn+O9viCin1HGtv1W" + "LIMxYFIj3m+3CgE2RltgVErmxS5Jo3bh86UFk8t+/JRRhatpxKaPBlOzN4WsmbkW" + "b7jfAkAWuTOy5l2Iujb5u1PWEUx6t6Q4VSXPtW5TUzr44YqrPGpky0I2IJuB2UM/" + "lXTOCYdAuMlJByCcBHspRT+Rn1f9AkEAibZAqxXtd6KHDJoSXI9qlfaH09zQnqoV" + "OfcBdIvR/C9NBlnxWmD1l8pkmR7vxul63gVejc5kkX8dBXwq1lKOawJBAKeVg9VB" + "hj7WkjgoIVnuI8CIrO6IpboS+ySV/CZalkyNNVRWq2rOJbpIHe4baXqPomZ8mFEA" + "4SGoEW3AiyFiqb8="; /** * 获取密钥对 */ public static KeyPair getKeyPair() throws Exception { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(1024); return generator.generateKeyPair(); } /** * 实例化公钥 * @param publicKey 公钥 */ public static PublicKey getPublicKey(String publicKey) throws Exception { byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey.getBytes()); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(keySpec); } /** * 实例化私钥 * @param privateKey 私钥 */ public static PrivateKey getPrivateKey(String privateKey) throws Exception { byte[] privateKeyBytes = Base64.getDecoder().decode(privateKey.getBytes()); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(keySpec); } /** * 公钥加密 * @param content 待加密内容 */ public static String encrypt(String content, String publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey)); return new String(Base64.getEncoder().encode(cipher.doFinal(content.getBytes("UTF-8")))); } /** * 私钥解密 * @param content 加密内容 */ public static String decrypt(String content, String privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKey)); return new String(cipher.doFinal(Base64.getDecoder().decode(content))); } /** * 私钥签名 * @param content 待签名内容 */ public static String sign(String content, String privateKey) throws Exception { Signature signature = Signature.getInstance("SHA1WithRSA"); signature.initSign(getPrivateKey(privateKey)); signature.update(content.getBytes("UTF-8")); return new String(Base64.getEncoder().encode(signature.sign())); } /** * 公钥验签 * @param content 原始内容 * @param sign 签名内容 */ public static boolean verify(String content, String sign, String publicKey) throws Exception { Signature signature = Signature.getInstance("SHA1WithRSA"); signature.initVerify(getPublicKey(publicKey)); signature.update(content.getBytes("UTF-8")); return signature.verify(Base64.getDecoder().decode(sign)); } public static void main(String[] args) throws Exception { KeyPair keyPair = getKeyPair(); System.out.println("私钥"); System.out.println(new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()))); System.out.println("公钥"); System.out.println(new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()))); String content = "消息内容"; String encrypt = encrypt(content,PUBLIC_KEY); System.out.println("加密内容"); System.out.println(encrypt); String decryp = decrypt(encrypt,PRIVATE_KEY); System.out.println("解密内容"); System.out.println(decryp); String sign = sign(content,PRIVATE_KEY); System.out.println("签名内容"); System.out.println(sign); boolean verify = verify(content,sign,PUBLIC_KEY); System.out.println("验证结果"); System.out.println(verify); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。