Java常用的安全算法

Java常用的安全算法

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code and KPI, Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

安全算法主要包含摘要算法,对称加密算法,非对称加密算法,信息编码以及数字签名,数字证书等。
数字摘要也要成为消息摘要,他是一个唯一的对应的一个消息或文本的固定的数值,由一个单项的hash函数对消息进行计算 而产生的。对应的生产过程如下图所示:
在这里插入图片描述
有如下特点:
计算出来的消息摘要的长度是固定的,应用MD5,计算出长度是128个比特位,使用SHA-1算法计算出来的。
使用160个比特位;
(2)只能进行正向的信息摘要,而无法从摘要中恢复原来的消息。
常用的算法:
MD5以及SHA算法:

public class DigestTest {
    public static byte[] testMD5(String content) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes = md.digest(content.getBytes("UTF-8"));
        return bytes;
    }
 
    public static byte[] testSHA1(String content) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        byte[] bytes = md.digest(content.getBytes("UTF-8"));
        return bytes;
    }
 
    public static void main(String args[]) throws Exception {
        String content = "123";
        byte[] bytes =  testMD5(content);
        String digest = DatatypeConverter.printHexBinary(bytes);
        System.out.println(digest);
 
        bytes =  testSHA1(content);
        digest = DatatypeConverter.printHexBinary(bytes);
        System.out.println(digest);
    }
 
}

2。对称加密算法
在对称加密算法中,数据发送方将明文和加密秘钥经过特殊加密算法出来后,生成复杂的加密密文进行发送,数据接收方收到密文后,必须用相同的算法以及秘钥进行解密。起过程如下:
明文–》秘钥经过密文–》秘钥明文;
加密 解密:

特点是算法公开,计算量小,加密速度快,加密效率高。
常用的加密算法包括是:DES算法,3DES算法,AES算法。

public class AESTest {
    public static String genKeyAES() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey key = keyGenerator.generateKey();
        String base64Key =  DatatypeConverter.printBase64Binary(key.getEncoded());
        return base64Key;
    }
    public static SecretKey loadKeyAES(String base64Key){
        byte[] bytes = DatatypeConverter.parseBase64Binary(base64Key);
        SecretKey key = new SecretKeySpec(bytes, "AES");
        return key;
    }
 
    public static byte[] encryptDES(byte[] source, SecretKey key) throws Exception {
        Cipher ciper = Cipher.getInstance("AES");
        ciper.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = ciper.doFinal(source);
        return bytes;
    }
 
    public static byte[] decrytDES(byte[] source, SecretKey key) throws Exception{
        Cipher ciper = Cipher.getInstance("AES");
        ciper.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = ciper.doFinal(source);
        return bytes;
    }
 
    public static void main(String args[]) throws Exception {
        String test = "1234";
        String content = genKeyAES();//"12345678901234567890123456789012";
        System.out.println(content);
 
        SecretKey secretKey = loadKeyAES(content);
 
        byte bytes[] =  encryptDES(test.getBytes(), secretKey);
        System.out.println("secretKey String:" + DatatypeConverter.printHexBinary(bytes));
 
        bytes = decrytDES (bytes, secretKey);
        System.out.println("decrytDES String:" + new String(bytes));
 
 
    }
}

3.非对称加密
称为非对称加密算法,需要2个秘钥,一个是公开秘钥,一个是私有秘钥(PK),公钥和私钥需要配对使用。

基本过程:
甲方生成一对秘钥,并将其中的一把作为公钥向其他人公开,得到该公钥的乙方使用该秘钥对机密信息进行加密后发送给甲方。
对甲方而言:
在这里插入图片描述

常用的算法:RSA算法:
public class RSATest {
public static KeyPair getKeyPairt() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(“RSA”);
keyPairGenerator.initialize(512);
return keyPairGenerator.generateKeyPair();
}
public static String getPublicKey(KeyPair keyPair){
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return DatatypeConverter.printBase64Binary(bytes);
}
public static String getPrivateKey(KeyPair keyPair){
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return DatatypeConverter.printBase64Binary(bytes);
}
public static PublicKey String2Publickey(String pubStr) throws Exception {
byte[] bytes = DatatypeConverter.parseBase64Binary(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey String2Privatekey(String privateStr) throws Exception {
byte[] bytes = DatatypeConverter.parseBase64Binary(privateStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PrivateKey privatekey = keyFactory.generatePrivate(keySpec);
return privatekey;
}

public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return cipher.doFinal(content);
}

public static byte[] privateEncrypt(byte[] content,  PrivateKey privatekey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, privatekey);
    return cipher.doFinal(content);
}

public static byte[] publicDecrypt(byte[] content, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    return cipher.doFinal(content);
}
public static byte[] privateDecrypt(byte[] content, PrivateKey privatekey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privatekey);
    return cipher.doFinal(content);
}


public static void main(String[] args) throws Exception {
    String str = "Hello, I am lai xiao hong";
    byte[] bytes = str.getBytes();
    KeyPair keyPair = getKeyPairt();
    String publicKey = getPublicKey(keyPair);
    String privateKey = getPrivateKey(keyPair);
    System.out.println("publicKey:" + publicKey);
    System.out.println("privateKey:" + privateKey);

    //byte[] encBytes = publicEncrypt(bytes, String2Publickey(publicKey));
    //byte[] decBytes = privateDecrypt(encBytes,String2Privatekey(privateKey));
    byte[] encBytes = privateEncrypt(bytes, String2Privatekey(privateKey));
    byte[] decBytes = publicDecrypt(encBytes, String2Publickey(publicKey));

    System.out.println("Decrypt String:" + new String(decBytes));

}

}

4.数字签名
签名认证是对非对称加密技术和数字摘要技术的综合运用,指的是将通信内容的商要信息使用发送者的私钥进行加密,然后将密文与原文一起传输给信息的接收者,接收者通过发送者的公钥解密被加密的摘要信息,然后使用与发送者相同的摘要算法,对接收的内容采用相同的方式产生摘要串,与解密的摘要串进行对比,如果相同,则说明接收到的内容是完整的,在传输过程中没有受到第五方篡改,否则说明通信内容已被第三方修改。

只有信息的发送者才能产生别人无法篡改的数字签名串,这个串能对信息发送者所发送的内容完整 性和发送者的身份进行校验和签别,通信正文经过相应的摘要算法生成摘要后,使用消息发送者的私钥进行加密,生成数字签名如下图所示:
在这里插入图片描述
签名认证是对非对称加密和数字摘要技术的综合运用,指的是将通信内容的摘要信息使用发送者的私钥进行加密,然后将密文与原文一起传输给信息的接收者。接受者通过发送者的公钥解密被解密的信息,然后使用与发送者相同的摘要算法,对接收的内容采用相同的方式产生摘要穿,与解密的摘要进行对比,如果相同,则说明接收到的内容是完整的,在传输的过程中没有收到第三方纂改。

只有信息的发送者才能产生别人无法篡改的数字签名穿,这个串数字,能对信息放着所发送的内容完整性和发送者的身份进行校验和鉴别,通信正文经过相应的摘要算法生成摘要后,使用消息发送者的私钥进行加密生成数字签名如下图所示:
在这里插入图片描述
消息的接收端收到信息的正文和对应的数字签名后,使用与发送端相同的摘要算法,生成通信正文的摘要,并且使用发送者的公钥对数字签名进行解密,得到发送商生成的摘要,进行比较后即可验证发送者的身份是否合法,正文内容是否被篡改,如下图所示:
消息的接收端收到信息的正文和对应的数字签名后,使用与发送端相同的摘要算法,生成通信正文的摘要,并且使用发送者的公钥对数字签名进行解密,得到发送商生成的摘要,进行比较后即可验证发送者的身份是否合法,正文内容是否被篡改,如下图所示:

常见的数字签名算法包括:
MD%withRSA、SHA1withRSA等.

public class SignatureTest {
    private static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(content);
        return  signature.sign();
    }
    private static boolean verify(byte[] content,byte[] sign, PublicKey publicKey)  throws Exception{
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(content);
        return signature.verify(sign);
    }
}
public class SignatureTest {
    private static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(content);
        return  signature.sign();
    }
    private static boolean verify(byte[] content,byte[] sign, PublicKey publicKey)  throws Exception{
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(content);
        return signature.verify(sign);
    }
}

5.数字证书
也称为电子证书,通常包含如下内容:
对象的名称
证书的过期时间
颁发机构
签名算法
对象的公钥
大多数证书采用X.509的格式来保存证书,数字证书的签发过程实际上就是对数字证书的内容,包括证书所代表对象的公钥进行数字签名,而验证证书的过程,实际上是检验证书的数字签名。
在现实中,网络用户的数字证书需要数字认证机构(Certificate Authority,CA)来进行颁发,只有经过CA颁布的数字证书在网络中才具备认证性。
在这里插入图片描述

要获得数字证书,首先需要使用数字证书管理工具,如keytool,OpenSSL等,然后构建CSR(Certificate Signing Request)数字证书签发申请,提交给数字证书认证机构进行签名,最终形成数字证书。

`





# 小结
讲述了对应的安全算法问题:
# 参考资料和推荐阅读
1.链接: [参考资料](https://blog.csdn.net/qq_26418435/article/details/102601603).


	

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值