人人都懂密码学】一篇最易懂的Java密码学入门教程(下)

网友投稿 675 2022-05-29

2.4.2 证书从哪里来

“证书中心”(certificate authority,简称CA),为公钥做认证。证书中心用自己的私钥,对公钥和一些相关信息一起加密,生成“数字证书”(Digital Certificate)。

拿到数字证书以后,就可以放心了。以后只要在签名的同时,再附上数字证书就行了。

用CA的公钥解开数字证书,就可以拿到真实的公钥了,然后就能证明“数字签名”是否真的是公司签的。

修改之前的RSAdemo代码:

/*  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.  */ package com.huawei.it.jalor.boot.test; import com.sun.org.apache.xml.internal.security.utils.Base64; import org.apache.commons.io.FileUtils; import javax.crypto.Cipher; import java.io.File; import java.nio.charset.Charset; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; /**  * RSAdemo  *  * @Author: 陈志强  * @CreateTime: 2020-10-12  * @Description:  */ public class RSAdemo {     public static void main(String[] args) throws Exception {         String input = "硅谷";         // 加密算法         String algorithm = "RSA";         PrivateKey privateKey = getPrivateKey("a.pri", algorithm);         PublicKey publicKey = getPublicKey("a.pub", algorithm);         String s = encryptRSA(algorithm, privateKey, input);         String s1 = decryptRSA(algorithm, publicKey, s);         System.out.println(s);         System.out.println(s1);     }     public static PublicKey getPublicKey(String pulickPath,String algorithm) throws Exception{         // 将文件内容转为字符串         String publicKeyString = FileUtils.readFileToString(new File(pulickPath), Charset.defaultCharset());         // 获取密钥工厂         KeyFactory keyFactory = KeyFactory.getInstance(algorithm);         // 构建密钥规范 进行Base64解码         X509EncodedKeySpec spec = new X509EncodedKeySpec(Base64.decode(publicKeyString));         // 生成公钥         return keyFactory.generatePublic(spec);     }     public static PrivateKey getPrivateKey(String priPath,String algorithm) throws Exception{         // 将文件内容转为字符串         String privateKeyString = FileUtils.readFileToString(new File(priPath), Charset.defaultCharset());         // 获取密钥工厂         KeyFactory keyFactory = KeyFactory.getInstance(algorithm);         // 构建密钥规范 进行Base64解码         PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.decode(privateKeyString));         // 生成私钥         return keyFactory.generatePrivate(spec);     }     /**      * 生成密钥对并保存在本地文件中      *      * @param algorithm : 算法      * @param pubPath   : 公钥保存路径      * @param priPath   : 私钥保存路径      * @throws Exception      */     public static void generateKeyToFile(String algorithm, String pubPath, String priPath) throws Exception {         // 获取密钥对生成器         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);         // 获取密钥对         KeyPair keyPair = keyPairGenerator.generateKeyPair();         // 获取公钥         PublicKey publicKey = keyPair.getPublic();         // 获取私钥         PrivateKey privateKey = keyPair.getPrivate();         // 获取byte数组         byte[] publicKeyEncoded = publicKey.getEncoded();         byte[] privateKeyEncoded = privateKey.getEncoded();         // 进行Base64编码         String publicKeyString = Base64.encode(publicKeyEncoded);         String privateKeyString = Base64.encode(privateKeyEncoded);         // 保存文件         FileUtils.writeStringToFile(new File(pubPath), publicKeyString, Charset.forName("UTF-8"));         FileUtils.writeStringToFile(new File(priPath), privateKeyString, Charset.forName("UTF-8"));     }     /**      * 解密数据      *      * @param algorithm      : 算法      * @param encrypted      : 密文      * @param key            : 密钥      * @return : 原文      * @throws Exception      */     public static String decryptRSA(String algorithm,Key key,String encrypted) throws Exception{         // 创建加密对象         // 参数表示加密算法         Cipher cipher = Cipher.getInstance(algorithm);         // 私钥进行解密         cipher.init(Cipher.DECRYPT_MODE,key);         // 由于密文进行了Base64编码, 在这里需要进行解码         byte[] decode = Base64.decode(encrypted);         // 对密文进行解密,不需要使用base64,因为原文不会乱码         byte[] bytes1 = cipher.doFinal(decode);         return new String(bytes1);     }     /**      * 使用密钥加密数据      *      * @param algorithm      : 算法      * @param input          : 原文      * @param key            : 密钥      * @return : 密文      * @throws Exception      */     public static String encryptRSA(String algorithm,Key key,String input) throws Exception{         // 创建加密对象         // 参数表示加密算法         Cipher cipher = Cipher.getInstance(algorithm);         // 初始化加密         // 第一个参数:加密的模式         // 第二个参数:使用私钥进行加密         cipher.init(Cipher.ENCRYPT_MODE,key);         // 私钥加密         byte[] bytes = cipher.doFinal(input.getBytes());         // 对密文进行Base64编码         return Base64.encode(bytes);     }     /**      * 从文件中加载公钥      *      * @param algorithm : 算法      * @param filePath  : 文件路径      * @return : 公钥      * @throws Exception      */     public static PublicKey loadPublicKeyFromFile(String algorithm, String filePath) throws Exception {         // 将文件内容转为字符串         String keyString = FileUtils.readFileToString(new File(filePath), Charset.forName("UTF-8"));         return loadPublicKeyFromString(algorithm, keyString);     }     /**      * 从字符串中加载公钥      *      * @param algorithm : 算法      * @param keyString : 公钥字符串      * @return : 公钥      * @throws Exception      */     public static PublicKey loadPublicKeyFromString(String algorithm, String keyString) throws Exception {         // 进行Base64解码         byte[] decode = Base64.decode(keyString);         // 获取密钥工厂         KeyFactory keyFactory = KeyFactory.getInstance(algorithm);         // 构建密钥规范         X509EncodedKeySpec keyspec = new X509EncodedKeySpec(decode);         // 获取公钥         return keyFactory.generatePublic(keyspec);     }     /**      * 从文件中加载私钥      *      * @param algorithm : 算法      * @param filePath  : 文件路径      * @return : 私钥      * @throws Exception      */     public static PrivateKey loadPrivateKeyFromFile(String algorithm, String filePath) throws Exception {         // 将文件内容转为字符串         String keyString = FileUtils.readFileToString(new File(filePath), Charset.forName("UTF-8"));         return loadPrivateKeyFromString(algorithm, keyString);     }     /**      * 从字符串中加载私钥      *      * @param algorithm : 算法      * @param keyString : 私钥字符串      * @return : 私钥      * @throws Exception      */     public static PrivateKey loadPrivateKeyFromString(String algorithm, String keyString) throws Exception {         // 进行Base64解码         byte[] decode = Base64.decode(keyString);         // 获取密钥工厂         KeyFactory keyFactory = KeyFactory.getInstance(algorithm);         // 构建密钥规范         PKCS8EncodedKeySpec keyspec = new PKCS8EncodedKeySpec(decode);         // 生成私钥         return keyFactory.generatePrivate(keyspec);     } } 写一个验证数字签名的类: /*  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.  */ package com.huawei.it.jalor.boot.test; import com.sun.org.apache.xml.internal.security.utils.Base64; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; /**  * 功能描述: 验证数字签名  *  * @author cWX970190  * @since 2020-10-11  */ public class SignatureDemo {     public static void main(String[] args) throws Exception {         String a = "123";         PublicKey publicKey =RSAdemo.loadPublicKeyFromFile("RSA", "a.pub");         PrivateKey privateKey = RSAdemo.loadPrivateKeyFromFile("RSA", "a.pri");         String signaturedData = getSignature(a, "sha256withrsa", privateKey);         boolean b = verifySignature(a, "sha256withrsa", publicKey, signaturedData);         System.out.println(b);     }     /**      * 生成签名      *      * @param input      : 原文      * @param algorithm  : 算法      * @param privateKey : 私钥      * @return : 签名      * @throws Exception      */     private static String getSignature(String input, String algorithm, PrivateKey privateKey) throws Exception {         // 获取签名对象         Signature signature = Signature.getInstance(algorithm);         // 初始化签名         signature.initSign(privateKey);         // 传入原文         signature.update(input.getBytes());         // 开始签名         byte[] sign = signature.sign();         // 对签名数据进行Base64编码         return Base64.encode(sign);     }     /**      * 校验签名      *      * @param input          : 原文      * @param algorithm      : 算法      * @param publicKey      : 公钥      * @param signaturedData : 签名      * @return : 数据是否被篡改      * @throws Exception      */     private static boolean verifySignature(String input, String algorithm, PublicKey publicKey, String signaturedData) throws Exception {         // 获取签名对象         Signature signature = Signature.getInstance(algorithm);         // 初始化签名         signature.initVerify(publicKey);         // 传入原文         signature.update(input.getBytes());         // 校验数据         return signature.verify(Base64.decode(signaturedData));     } }

运行,验证成功:

________________________________________

【人人都懂密码学】一篇最易懂的Java密码学入门教程(下)

拓展: 2.5 Byte和bit

Byte : 字节. 数据存储的基本单位,比如移动硬盘1T , 单位是byte

bit : 比特, 又叫位. 一个位要么是0要么是1. 数据传输的单位 , 比如家里的宽带100MB,下载速度并没有达到100MB,一般都是12-13MB,那么是因为需要使用 100 / 8

关系: 1Byte = 8bit

2.5.1 获取字符串byte

/**  * ByteBit  *  * @Author: 陈志强  * @CreateTime: 2020-10-12  * @Description:  */ public class ByteBit {     public static void main(String[] args) {         String a = "a";         byte[] bytes = a.getBytes();         for (byte b : bytes) {             int c=b;             // 打印发现byte实际上就是ascii码             System.out.println(c);         }     } }

运行结果:

和ascii码表一致

2.5.2 byte对应bit

public class ByteBit {     public static void main(String[] args) {         String a = "a";         byte[] bytes = a.getBytes();         for (byte b : bytes) {             int c=b;             // 打印发现byte实际上就是ascii码             System.out.println(c);             // 我们在来看看每个byte对应的bit,byte获取对应的bit             String s = Integer.toBinaryString(c);             System.out.println(s);         }     } }

运行结果

2.5.3 中文对应的字节

package com.huawei.it.jalor.boot.test; /**  * 功能描述  *  * @author cWX970190  * @since 2020-10-11  */ public class ByteBitDemo {     public static void main(String[] args) throws Exception{         String a = "华";         byte[] bytes = a.getBytes();         for (byte b : bytes) {             System.out.print(b + "   ");             String s = Integer.toBinaryString(b);             System.out.println(s);         }     } }

运行程序,我们发现一个中文是有 3 个字节组成:

我们修改 编码格式 , 编码格式改成 GBK

修改代码

// UTF-8:编码格式占3个字节         byte[] bytes = a.getBytes("GBK");

再运行发现变成了 2 个字节

2.5.4 英文对应的字节

/**  * ByteBit  *  * @Author: 陈志强  * @CreateTime: 2020-10-12  * @Description:  */ public class ByteBit {     public static void main(String[] args) throws Exception{         String a = "a";         byte[] bytes = a.getBytes();         // 在中文情况下,不同的编码格式,对应不同的字节 //        byte[] bytes = a.getBytes("GBK");         for (byte b : bytes) {             System.out.print(b + "   ");             String s = Integer.toBinaryString(b);             System.out.println(s);         }     } }

运行程序

________________________________________

三、如何设置密码才安全

通过上述密码学发展史的介绍,以及对常见加密算法的阐述,相信大家对密码应该有了较为理性的认识,那么,如何设置密码才安全呢?这里给出一点小建议:

密码不要太常见,不要使用类似于123456式的常用密码。

各应用软件密码建议不同,避免出现一个应用数据库被脱库,全部应用密码崩塌,

可在设置密码时增加注册时间、注册地点、应用特性等方法。例如tianjin123456,表示在天津注册的该应用

参考文献:

现代密码学之对称加密-DES及AES算法- element ui

http://element-ui.cn/article/show-97007.aspx

Java Base64 编码与解码----三种实现方式的代码实例

https://blog.csdn.net/qq_27093465/article/details/93977519

网络安全之密码学:信息安全

https://www.bilibili.com/video/av583369085/

好了,本期的分享到此就跟大家saygoodbye了,密码学博大精深,本文只是浅尝辄止,关于密码学的知识一直都在更新,希望下次可以给大家带来更前沿、更实用的密码学相关知识,喜欢的老铁欢迎关注,笔芯!

Java

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用户体验+数字平台:华为数字化转型的双引擎
下一篇:android studio 帮助文档(一)探索android studio (3) 迁移至 Android Studio
相关文章