古代密码学
替换
移位
凯撒加密
凯撒解密
频率分析法
https://blog.csdn.net/Leon_Jinhai_Sun/article/details/108691183
现代加密
对称加密
加密和解密用的同一把钥匙
流加密 块解密
DES
Data Encryption Standard
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;
class Solution {
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//加密
Cipher cipherEncrypt = Cipher.getInstance("DES");
//DES加密 密钥必须是8位
cipherEncrypt.init(Cipher.ENCRYPT_MODE, new SecretKeySpec("12345678".getBytes(), "DES"));
byte[] bytes = cipherEncrypt.doFinal("fuck".getBytes());
//输出乱码
System.out.println(new String(bytes));
//单独打印每个字节 发现有负数 在ASCII表找不到对应的字符 所以乱码
for (byte aByte : bytes) {
System.out.println(aByte);
}
//用base64转码 用apache的包
String encode = Base64.encode(bytes);
System.out.println(encode);
//解密
Cipher cipherDecrypt = Cipher.getInstance("DES");
cipherDecrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec("12345678".getBytes(), "DES"));
byte[] decodeBytes = cipherDecrypt.doFinal(Base64.decode(encode));
System.out.println(new String(decodeBytes));
}
}
base64
AES
Advanced Encryption Standard