初学密码学要学那些呢,个人分成这些
本篇文章主要是加密的代码,大家可以通过代码一点点查有什么用
熟悉凯撒密码类似的密码加密破解
什么是对称加密,
什么是非对称加密,
什么是不可逆加密,()
信息摘要技术,
数字证书工作原理,
https,open ssl,ssl,tls协议,
对称加密
个人总结笔记:
加密秘钥 = 解密秘钥 完成对称的行为
初等
常用的对称加密 DES
3DES
ASE
PEB
IDEA
DES:数据加密标准
秘钥长度 56,64
AES:DES的进阶
PBE(Password Based Encryption):基于口令的加密 加盐
对已有算法的包装
JDK,BC
盐
PBEWithMD5AndDES
DES
注意这个Hex是 org.apache.commons.codec.binary.Hex
可以导包
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
他有jdkDES还有bcDES,代码差距都不大
DESTest
package symmetricPackage.DES;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
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;
import java.security.Security;
public class DESTest {
private static String src="feige011 is back";
public static void main(String[] args) {
// jdkDES();
test();
}
public static void jdkDES(){
// 生成KEY
try {
KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey= keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println(Hex.encodeHexString(bytesKey));
// KEY的转换
DESKeySpec desKeySpec=new DESKeySpec(bytesKey);
//加密方式
SecretKeyFactory factory=SecretKeyFactory.getInstance("DES");
Key convertSecretKey=factory.generateSecret(desKeySpec);
//加密 加密方式/工作方式/填充方式
Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
//模式+key
cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println("jdk des encrypt: "+ Hex.encodeHexString(result));
// Key convertSecretKey=factory.generateSecret()
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
result=cipher.doFinal(result);
System.out.println("jdk des decrypt: "+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test(){
String s="6130617661feeb1697dd4c248e59fdf6569afb639ae3dc51";
String s2="4575dcefabfed35b";
// =s.getBytes();
try {
byte[] b= Hex.decodeHex(s);
System.out.println("jdk des encrypt: "+ Hex.encodeHexString(b));
KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
keyGenerator.init(56);
byte[] bytesKey =Hex.decodeHex(s2);
// 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.DECRYPT_MODE,convertSecretKey);
byte[] result=cipher.doFinal(b);
System.out.println("jdk des decrypt: "+new String(result));
}catch (Exception e){
e.printStackTrace();
}
}
public static void bcDES(){
try {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator keyGenerator=KeyGenerator.getInstance("symmetricPackage/DES","BC");
keyGenerator.init(56);
SecretKey secretKey= keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println(Hex.encodeHexString(bytesKey));
// KEY的转换
DESKeySpec desKeySpec=new DESKeySpec(bytesKey);
//加密方式
SecretKeyFactory factory=SecretKeyFactory.getInstance("symmetricPackage/DES");
Key convertSecretKey=factory.generateSecret(desKeySpec);
//加密 加密方式/工作方式/填充方式
Cipher cipher=Cipher.getInstance("symmetricPackage/DES/ECB/PKCS5Padding");
//模式+key
cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println("bc des encrypt: "+ Hex.encodeHexString(result));
// Key convertSecretKey=factory.generateSecret()
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
result=cipher.doFinal(result);
System.out.println("bc des decrypt: "+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3DES
和DES差距不大,就是key转换,加密方式,填充方式内个地方有点改变
package symmetricPackage.DES;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;
import java.security.SecureRandom;
public class DES3Test {
private static String src="feige011 is back";
public static void main(String[] args) {
jdk3DES();
}
static public void jdk3DES(){
// 生成KEY
try {
KeyGenerator keyGenerator=KeyGenerator.getInstance("DESede");
// keyGenerator.init(168);
keyGenerator.init(new SecureRandom());
SecretKey secretKey= keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
System.out.println(Hex.encodeHexString(bytesKey));
// KEY的转换
DESedeKeySpec desKeySpec=new DESedeKeySpec(bytesKey);
//加密方式
SecretKeyFactory factory=SecretKeyFactory.getInstance("DESede");
Key convertSecretKey=factory.generateSecret(desKeySpec);
//加密 加密方式/工作方式/填充方式
Cipher cipher=Cipher.getInstance("DESede/ECB/PKCS5Padding");
//模式+key
cipher.init(Cipher.ENCRYPT_MODE,convertSecretKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println("jdk des encrypt: "+ Hex.encodeHexString(result));
// Key convertSecretKey=factory.generateSecret()
//解密
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
差距也不大,就是原来DES的地方改成AES
package symmetricPackage.AES;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
public class AESTest {
private static String src = "feige011 is back";
public static String myKey="QCfdszi2t2B5amt8jysutA==";
public static void main(String[] args) {
jdkAES();
// bcAES();
}
public static void jdkAES() {
try {
//生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey=keyGenerator.generateKey();
// byte[] keyBytes=secretKey.getEncoded();
byte[] keyBytes=Base64.decodeBase64(myKey.getBytes());
System.out.println("jdk aes encrypt: "+ Base64.encodeBase64String(keyBytes));
//KEY的转换
Key key=new SecretKeySpec(keyBytes, "AES");
//加密
Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] bytes=cipher.doFinal(src.getBytes());
System.out.println("jdk aes encrypt: "+ Base64.encodeBase64String(bytes));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
bytes=cipher.doFinal(bytes);
System.out.println("jdk aes desrypt: "+new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void bcAES(){
try {
Security.addProvider(new BouncyCastleProvider());
//生成KEY
KeyGenerator keyGenerator = KeyGenerator.getInstance("symmetricPackage/AES","BC");
keyGenerator.init(128);
SecretKey secretKey=keyGenerator.generateKey();
byte[] keyBytes=secretKey.getEncoded();
//KEY的转换
Key key=new SecretKeySpec(keyBytes, "symmetricPackage/AES");
//加密
Cipher cipher=Cipher.getInstance("symmetricPackage/AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] bytes=cipher.doFinal(src.getBytes());
System.out.println("jdk aes encrypt: "+ Base64.encodeBase64String(bytes));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
bytes=cipher.doFinal(bytes);
System.out.println("jdk aes desrypt: "+new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
}
非对称加密
高级
双保险
公钥,私钥
DH()秘钥交换算法
RSA-基于因子分解
ELGamal--基于离散对数
ECC-椭圆曲线加密
DH:
初始化发送方密钥
KeyPairGenerator//声称keyPair实例
KeyPair //能够得到公钥和私钥
PublicKey //公钥
初始化接收方密钥
KeyFactory //声成密钥(还原密钥)
X509EncodedKeySpec //根据ASN.1标准进行编码
DHPublicKey
DHParameterSpec //在DH算法中参数集合
KeyPairGenerator
PrivateKey
密钥构建
KeyAgreement //提供密钥协定协议的功能
SecretKey //秘密密钥,存在目的声成分组密钥
KeyFactory
X509EncodedKeySpec
PublicKey
加密,解密:
Cipher //核心内容
DH
package AsymmetricPackage;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.interfaces.DHPublicKey;
import javax.crypto.spec.DHParameterSpec;
import java.security.*;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;
public class DHTest {
private static String src="feige011 is good";
public static void main(String[] args) {
jdkDH();
}
public static void jdkDH(){
try {
System.getProperties().setProperty("jdk.crypto.KeyAgreement.legacyKDF", "true");
//1.初始化发送发密钥
KeyPairGenerator senderKeyPairGenerator=KeyPairGenerator.getInstance("DH");
senderKeyPairGenerator.initialize(512);
KeyPair senderKeyPair=senderKeyPairGenerator.generateKeyPair();
//发送方公钥,需要发送给接受方
byte[] senderPublicKeyEnc=senderKeyPair.getPublic().getEncoded();
//2.初始化接收方密钥
KeyFactory receiverKeyFactory=KeyFactory.getInstance("DH");
X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(senderPublicKeyEnc);
PublicKey receiverPublicKey=receiverKeyFactory.generatePublic(x509EncodedKeySpec);
DHParameterSpec dhParameterSpec=((DHPublicKey)receiverPublicKey).getParams();
KeyPairGenerator receiverKeyPairGenerator=KeyPairGenerator.getInstance("DH");
receiverKeyPairGenerator.initialize(dhParameterSpec);
KeyPair receiverKeypair=receiverKeyPairGenerator.generateKeyPair();
PrivateKey receiverPrivateKey=receiverKeypair.getPrivate();
byte[] receiverPublicKeyEnc=receiverKeypair.getPublic().getEncoded();
//3.密钥构建
KeyAgreement receiverKeyAgreement=KeyAgreement.getInstance("DH");
receiverKeyAgreement.init(receiverPrivateKey);
receiverKeyAgreement.doPhase(receiverPublicKey,true);
SecretKey receiverDesKey=receiverKeyAgreement.generateSecret("DES");
KeyFactory senderKeyFactory= KeyFactory.getInstance("DH");
x509EncodedKeySpec=new X509EncodedKeySpec(receiverPublicKeyEnc);
PublicKey senderPublicKey=senderKeyFactory.generatePublic(x509EncodedKeySpec);
KeyAgreement senderKeyAgreement=KeyAgreement.getInstance("DH");
senderKeyAgreement.init(senderKeyPair.getPrivate());
senderKeyAgreement.doPhase(senderPublicKey,true);
SecretKey senderDesKey=senderKeyAgreement.generateSecret("DES");
if(Objects.equals(receiverDesKey,senderDesKey)){
System.out.println("双方密钥相同");
}
//4,加密
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,senderDesKey);
byte[] result=cipher.doFinal(src.getBytes());
System.out.println("jdk dh eccrypt: "+Base64.encodeBase64String(result));
//5.解密
cipher.init(Cipher.DECRYPT_MODE,receiverDesKey);
result=cipher.doFinal(result);
System.out.println("jdk dh decrypt: "+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
不可逆加密
不可逆加密经典就是md5加密
md5
package InformationSummary;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
public class MDTest {
private static String src="feige011 is back";
public static void main(String[] args) {
jdkMD5();
jdkMD2();
bcMD4();
bcMD5();
ccMD5();
ccMD2();
}
public static void jdkMD5(){
try {
MessageDigest md=MessageDigest.getInstance("MD5");
byte[] md5Bytes=md.digest(src.getBytes());
System.out.println("JDK MD5: "+ Hex.encodeHexString(md5Bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void jdkMD2(){
try {
MessageDigest md=MessageDigest.getInstance("MD2");
byte[] md5Bytes=md.digest(src.getBytes());
System.out.println("JDK MD2: "+ Hex.encodeHexString(md5Bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void bcMD5(){
Digest digest=new MD5Digest();
digest.update(src.getBytes(),0,src.getBytes().length);
byte[] md5Bytes=new byte[digest.getDigestSize()];
digest.doFinal(md5Bytes,0);
System.out.println("BC MD4: "+org.bouncycastle.util.encoders.Hex.toHexString(md5Bytes));
}
public static void bcMD4(){
try {
Security.addProvider(new BouncyCastleProvider());
MessageDigest md=MessageDigest.getInstance("MD4");
byte[] md4Bytes=md.digest(src.getBytes());
System.out.println("BC MD4: "+ Hex.encodeHexString(md4Bytes));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// Digest digest=new MD4Digest();
// digest.update(src.getBytes(),0,src.getBytes().length);
// byte[] md4Bytes=new byte[digest.getDigestSize()];
// digest.doFinal(md4Bytes,0);
// System.out.println("BC MD4: "+org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
}
public static void ccMD5(){
System.out.println("ccMD5:" +DigestUtils.md5Hex(src.getBytes()));
}
public static void ccMD2(){
System.out.println("ccMD2:" +DigestUtils.md2Hex(src.getBytes()));
}
}
数字证书
证明你的公钥属于你,
发证机构
所有人
公钥: 有效期
数字签名:
作用:
数字签名
HTTPS
认证所有人和公钥关系
避免中间人攻击
基于第三方的信任
https原理
安全的http
ssl证书,用在https下的证书.