urlencode
urlencode_百度百科
Base64是一种二进制到文本的编码方式,而且编码出的字符串只包含ASCII基础字符
下图是Base64码表,可以看到从0到63的每个数字都对应一个上面的一个字符。
文件-base64字符串互转
sun.misc包中的类
try (FileOutputStream fos = new FileOutputStream(hdfsConfig.getTempDir() + fileName)) {
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(baseStr);
fos.write(b);
fos.flush();
}
java.util包中的类
String content = "";
byte[] decode = Base64.getDecoder().decode(content);
FileUtils.writeByteArrayToFile(new File(localZipFilePath), decode);
md5
commons-codec.jar
import org.apache.commons.codec.digest.DigestUtils;
/** * MD5加密之方法一 * @explain 借助apache工具类DigestUtils实现 * @param str * 待加密字符串 * @return 16进制加密字符串 */
public static String encryptToMD5(String str) {
return DigestUtils.md5Hex(str);
}
java.security.MessageDigest
/** * MD5加密之方法二 * @explain java实现 * @param str * 待加密字符串 * @return 16进制加密字符串 */
public static String encrypt2ToMD5(String str) {
// 加密后的16进制字符串
String hexStr = "";
try {
// 此 MessageDigest 类为应用程序提供信息摘要算法的功能
MessageDigest md5 = MessageDigest.getInstance("MD5");
// 转换为MD5码
byte[] digest = md5.digest(str.getBytes("utf-8"));
hexStr = ByteUtils.toHexString(digest);
} catch (Exception e) {
e.printStackTrace();
}
return hexStr;
}
spring核心包org.springframework.util.DigestUtils
/** * MD5加密之方法三 * @explain springboot自带MD5加密 * @param str * 待加密字符串 * @return 16进制加密字符串 */
public static String encrypt3ToMD5(String str) {
log.debug("MD5待加密字符串:\n"+str);
String md5 = " ";
try {
md5 = DigestUtils.md5DigestAsHex(text.getBytes("utf-8"))
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
log.degbug("MD5加密结果:\n"+md5)
return md5;
}
SHA1
对称加密算法
DES
package org.cc.lipiao;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
public class EncritAndDecrit {
private static String src = "imooc security des";
public static void main(String[] args) {
desDemo();
}
private static void desDemo(){
try {
//生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
//KEY转换
DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
Key convertSecretKey = factory.generateSecret(desKeySpec);
//加密
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] result = cipher.doFinal(src.getBytes());
//解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
result = cipher.doFinal(result);
System.out.println("jdk des decrypt : " + new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
AES
非对称加密算法
RSA
加密算法是目前最有影响力的 公钥加密算法,并且被普遍认为是目前 最优秀的公钥方案 之一。RSA
是第一个能同时用于 加密 和 数字签名 的算法,它能够 抵抗 到目前为止已知的 所有密码攻击,已被 ISO
推荐为公钥数据加密标准。
基于JDK RSA算法实现非对称加密解密
package org.cc.lipiao;
import org.apache.tomcat.util.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class EncritAndDecrit {
private static final String RSA = "RSA";
private static byte[] privateKeyBytes ;
private static byte[] publicKeyBytes ;
private static String src = "imooc security rsa";
public static void main(String[] args) throws Exception {
String s = publicKeyCrypto(src);
privateKeyDecrypto(s);
String s1 = privateKeyCrypto(src);
publicKeyDecrypto(s1);
}
static {
try {
KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance(RSA);
senderKeyPairGenerator.initialize(512);
KeyPair keyPair = senderKeyPairGenerator.generateKeyPair();
privateKeyBytes = keyPair.getPrivate().getEncoded();
publicKeyBytes = keyPair.getPublic().getEncoded();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 公钥加密
* @param originStr
* originStr 需要加密的原始字符串
* @return 经过公钥加密并base64编码的字符串
*/
public static String publicKeyCrypto(String originStr) throws Exception{
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(originStr.getBytes());
System.out.println("公钥加密后base64编码:" + Base64.encodeBase64String(result));
return Base64.encodeBase64String(result);
}
/**
* 私钥解密
* @param publicKeyCryptoStr
* publicKeyCryptoStr 经过公钥加密并base64编码的字符串
* @return 未加密的原始字符串
*/
public static String privateKeyDecrypto(String publicKeyCryptoStr) throws Exception {
byte [] originStrBytes = Base64.decodeBase64(publicKeyCryptoStr.getBytes());
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Cipher cipher = Cipher.getInstance(RSA);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(originStrBytes);
System.out.println("解密得到的字符串:" + new String(result));
return new String(result);
}
/**
* 私钥加密
* @param originStr
* originStr 需要加密的原始字符串
* @return 经过私钥加密并base64编码的字符串
*/
public static String privateKeyCrypto(String originStr) throws Exception {
String resultStr = "";
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(originStr.getBytes());
System.out.println("加密结果经过base64编码:" + Base64.encodeBase64String(result));
resultStr = Base64.encodeBase64String(result);
return resultStr;
}
/**
* 公钥解密
* @param privateKeyCryptoStr
* privateKeyCryptoStr 经过私钥加密后并base64编码的字符串
* @return 未加密的原始字符串
*/
public static String publicKeyDecrypto(String privateKeyCryptoStr) throws Exception {
byte [] originStrBytes = Base64.decodeBase64(privateKeyCryptoStr.getBytes());
String resultStr = "";
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Cipher cipher = Cipher.getInstance(RSA);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(originStrBytes);
System.out.println("公钥解密后:" + new String(result));
resultStr = new String(result);
return resultStr;
}
}
DSA
BouncyCastle
相关参考链接:
国密算法介绍 - 知乎国密算法,即国家商用密码算法。是由国家密码管理局认定和公布的密码算法标准及其应用规范,其中部分密码算法已经成为国际标准。如SM系列密码,SM代表商密,即商业密码,是指用于商业的、不涉及国家秘密的密码技术…https://zhuanlan.zhihu.com/p/132352160
浅谈常见的七种加密算法及实现 - 知乎前言 数字签名、信息加密 是前后端开发都经常需要使用到的技术,应用场景包括了用户登入、交易、信息通讯、oauth 等等,不同的应用场景也会需要使用到不同的签名加密算法,或者需要搭配不一样的 签名加密算法 来达…https://zhuanlan.zhihu.com/p/347114235
api中文文档
API Reference Documenthttps://www.apiref.com/