常用加解密原理及实际使用

打印 上一主题 下一主题

主题 877|帖子 877|积分 2631

AES加解密

在Java中,可以使用javax.crypto包来实现AES-256加密和解密,使用java.security包来实现RSA-2048加密和解密。以下是一个简单的示例,展示了如何使用AES-256和RSA-2048进行加密和解密。
样例

  1. import javax.crypto.Cipher;
  2. import javax.crypto.KeyGenerator;
  3. import javax.crypto.SecretKey;
  4. import java.util.Base64;
  5. public class AESExample {
  6.     public static void main(String[] args) throws Exception {
  7.         String originalText = "Hello, AES-256!";
  8.         // 生成AES密钥
  9.         KeyGenerator keyGen = KeyGenerator.getInstance("AES");
  10.         keyGen.init(128); // 256位密钥,长度限制,换128【确保你的Java运行环境支持AES-256和RSA-2048。某些旧版本的Java可能需要安装额外的安全策略文件(如JCE Unlimited Strength Jurisdiction Policy Files)来支持256位密钥。】
  11.         SecretKey secretKey = keyGen.generateKey();
  12.         // 加密
  13.         String encryptedText = encrypt(originalText, secretKey);
  14.         System.out.println("Encrypted: " + encryptedText);
  15.         // 解密
  16.         String decryptedText = decrypt(encryptedText, secretKey);
  17.         System.out.println("Decrypted: " + decryptedText);
  18.     }
  19.     public static String encrypt(String plainText, SecretKey secretKey) throws Exception {
  20.         Cipher cipher = Cipher.getInstance("AES");
  21.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  22.         byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
  23.         return Base64.getEncoder().encodeToString(encryptedBytes);
  24.     }
  25.     public static String decrypt(String encryptedText, SecretKey secretKey) throws Exception {
  26.         Cipher cipher = Cipher.getInstance("AES");
  27.         cipher.init(Cipher.DECRYPT_MODE, secretKey);
  28.         byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
  29.         return new String(decryptedBytes);
  30.     }
  31. }
复制代码
WEB中RSA加解密

在 Web 开发中,使用 RSA 实现安全信息传输的核心原理是使用 RSA 的非对称加密特性,确保数据在传输过程中的机密性、完整性和身份验证。以下是 RSA 在 Web 开发中的应用原理和实现步调:
1. RSA 的根本原理

RSA 是一种非对称加密算法,它使用一对密钥:


  • 公钥(Public Key):用于加密数据,可以公开分享。
  • 私钥(Private Key):用于解密数据,必须严格保密。
RSA 的核心特性:


  • 用公钥加密的数据,只能用对应的私钥解密。
  • 用私钥加密的数据,只能用对应的公钥解密(通常用于数字签名)。
2. Web 开发中 RSA 的应用场景

在 Web 开发中,RSA 通常用于以了局景:

  • 加密敏感数据

    • 客户端使用服务器的公钥加密敏感数据(如暗码、名誉卡号),确保数据在传输过程中不被窃取。
    • 服务器使用私钥解密数据。

  • 数字签名

    • 服务器使用私钥对数据进行签名,客户端使用公钥验证签名,确保数据的完整性和泉源真实性。

  • 密钥交换

    • RSA 可以用于加密对称密钥(如 AES 密钥),然后通过安全通道传输对称密钥,后续通信使用对称加密。

3. RSA 实现安全信息传输的流程

以下是一个典型的 RSA 加密传输流程:
(1) 服务器天生 RSA 密钥对



  • 服务器天生一对 RSA 密钥(公钥和私钥)。
  • 公钥可以公开分享,私钥必须严格保密。
(2) 客户端获取服务器的公钥



  • 客户端通过 HTTPS 请求从服务器获取公钥。
  • 公钥可以以 PEM 或 JWK 格式传输。
(3) 客户端加密数据



  • 客户端使用服务器的公钥加密敏感数据(如用户暗码)。
  • 加密后的数据称为密文。
(4) 客户端发送密文到服务器



  • 客户端将密文通过 HTTPS 发送到服务器。
(5) 服务器解密数据



  • 服务器使用私钥解密密文,获取原始数据。
4. 代码示例

  1. import javax.crypto.Cipher;
  2. import java.security.KeyPair;
  3. import java.security.KeyPairGenerator;
  4. import java.security.PrivateKey;
  5. import java.security.PublicKey;
  6. import java.security.interfaces.RSAPrivateKey;
  7. import java.security.interfaces.RSAPublicKey;
  8. import java.util.Base64;
  9. public class RSAExample {
  10.     public static void main(String[] args) throws Exception {
  11.         String originalText = "Hello, RSA-2048!";
  12.         // 生成RSA密钥对
  13.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  14.         keyPairGen.initialize(2048); // 2048位密钥
  15.         KeyPair keyPair = keyPairGen.generateKeyPair();
  16.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  17.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  18.         // 加密
  19.         String encryptedText = encrypt(originalText, publicKey);
  20.         System.out.println("Encrypted: " + encryptedText);
  21.         // 解密
  22.         String decryptedText = decrypt(encryptedText, privateKey);
  23.         System.out.println("Decrypted: " + decryptedText);
  24.     }
  25.     public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
  26.         Cipher cipher = Cipher.getInstance("RSA");
  27.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  28.         byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
  29.         return Base64.getEncoder().encodeToString(encryptedBytes);
  30.     }
  31.     public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
  32.         Cipher cipher = Cipher.getInstance("RSA");
  33.         cipher.init(Cipher.DECRYPT_MODE, privateKey);
  34.         byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
  35.         return new String(decryptedBytes);
  36.     }
  37. }
复制代码
gitlab使用RSA机制

GitLab 使用 SSH 协议来实现与 Git 仓库的安全通信。通过 SSH,用户可以在不输入用户名和暗码的环境下,安全地克隆、推送和拉代替码。以下是 GitLab 的 SSH 机制具体解析:
1. SSH 的根本概念

SSH(Secure Shell)是一种加密的网络协议,用于在不安全的网络中安全地传输数据。GitLab 使用 SSH 来实现以下功能:


  • 用户认证:通过 SSH 密钥对用户进行身份验证。
  • 数据传输:加密全部 Git 使用(如 git clone、git push、git pull)中的数据。
2. GitLab 的 SSH 认证流程

GitLab 使用 SSH 公钥认证机制,具体流程如下:
(1) 用户天生 SSH 密钥对

用户在本地天生一对 SSH 密钥(公钥和私钥):


  • 私钥:存储在用户本地,必须严格保密。
  • 公钥:可以公开,必要上传到 GitLab。
    1. # 生成 SSH 密钥对
    2. ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    复制代码
  • -t rsa:指定密钥范例为 RSA。
  • -b 4096:指定密钥长度为 4096 位。
  • -C:添加注释(通常是用户的邮箱)。
天生的密钥默认存储在 ~/.ssh/ 目次下: - 私钥:~/.ssh/id_rsa - 公钥:~/.ssh/id_rsa.pub
(2) 用户将公钥上传到 GitLab


  • 复制公钥内容: bash cat ~/.ssh/id_rsa.pub
  • 登录 GitLab,进入 Profile Settings -> SSH Keys
  • 将公钥内容粘贴到输入框中,并生存。
(3) GitLab 存储公钥

GitLab 会将用户的公钥存储在数据库中,并与用户的账户关联。
(4) 用户通过 SSH 访问 Git 仓库

当用户实验 Git 使用时(如 git clone),GitLab 会使用 SSH 协议进行认证:

  • 用户本地 Git 客户端通过 SSH 连接到 GitLab 服务器。
  • GitLab 服务器向客户端发送一个随机天生的字符串。
  • 客户端使用本地私钥对字符串进行签名,并将签名发送回服务器。
  • 服务器使用存储的公钥验证签名。
  • 假如验证乐成,用户被答应访问 Git 仓库。
3. GitLab 的 SSH URL

GitLab 仓库的 SSH URL 格式如下:
  1. git@gitlab.example.com:username/project.git
复制代码


  • git:GitLab 的 SSH 用户名。
  • gitlab.example.com:GitLab 服务器地点。
  • username/project.git:仓库路径。
    例如:
  1. git clone git@gitlab.example.com:john/my-project.git
复制代码
4. SSH 配置文件

为了简化 SSH 连接,可以在本地配置 ~/.ssh/config 文件。例如:
  1. Host gitlab
  2.     HostName gitlab.example.com
  3.     User git
  4.     IdentityFile ~/.ssh/id_rsa
复制代码
配置后,可以使用别名访问 GitLab
  1. git clone gitlab:username/project.git
复制代码
5. 多 SSH 密钥管理

假如必要在同一台呆板上使用多个 SSH 密钥(例如,同时访问 GitLab 和 GitHub),可以通过以下方式管理:
(1) 天生多个密钥对

  1. # 生成 GitLab 密钥
  2. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_gitlab
  3. # 生成 GitHub 密钥
  4. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_github
复制代码
(2) 配置 ~/.ssh/config

  1. # GitLabHost gitlab
  2.     HostName gitlab.example.com
  3.     User git
  4.     IdentityFile ~/.ssh/id_rsa
  5. _gitlab# GitHubHost github    HostName github.com    User git    IdentityFile ~/.ssh/id_rsa_github
复制代码
(3) 使用差别的别名访

  1. # 访问 GitLabgit clone gitlab:username/project.git
  2. # 访问 GitHubgit clone github:username/project.git
复制代码
6. 常见问题及办理方法

(1) 权限问题



  • 确保私钥文件的权限为 600:
    1. chmod 600 ~/.ssh/id_rsa
    复制代码
  • 确保 ~/.ssh 目次的权限为 700:
    1. chmod 700 ~/.ssh
    复制代码
(2) SSH 认证失败



  • 检查公钥是否已正确上传到 GitLab。
  • 检查 SSH 配置文件是否正确。
  • 使用 ssh -T 测试连接:
    1. ssh -T git@gitlab.example.com
    复制代码
假如乐成,会显示欢迎信息。
(3) 多个 Git 服务冲突



  • 确保为每个 Git 服务配置了差别的 SSH 密钥和别名。
7. 总结

GitLab 的 SSH 机制通过公钥认证实现了安全的 Git 使用。用户必要在本地天生 SSH 密钥对,并将公钥上传到 GitLab。通过 SSH,用户可以方便地克隆、推送和拉代替码,而无需每次都输入用户名和暗码。
样例

  1. import javax.crypto.Cipher;
  2. import java.security.KeyPair;
  3. import java.security.KeyPairGenerator;
  4. import java.security.PrivateKey;
  5. import java.security.PublicKey;
  6. import java.security.interfaces.RSAPrivateKey;
  7. import java.security.interfaces.RSAPublicKey;
  8. import java.util.Base64;
  9. public class RSAExample2 {
  10.     public static void main(String[] args) throws Exception {
  11.         String originalText = "Hello, RSA-2048!";
  12.         // 生成RSA密钥对
  13.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  14.         keyPairGen.initialize(2048); // 2048位密钥
  15.         KeyPair keyPair = keyPairGen.generateKeyPair();
  16.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  17.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  18.         // 加密
  19.         String encryptedText = encrypt(originalText, privateKey);
  20.         System.out.println("Encrypted: " + encryptedText);
  21.         // 解密
  22.         String decryptedText = decrypt(encryptedText, publicKey);
  23.         System.out.println("Decrypted: " + decryptedText);
  24.     }
  25.     public static String encrypt(String plainText, PrivateKey privateKey) throws Exception {
  26.         Cipher cipher = Cipher.getInstance("RSA");
  27.         cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  28.         byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
  29.         return Base64.getEncoder().encodeToString(encryptedBytes);
  30.     }
  31.     public static String decrypt(String encryptedText, PublicKey publicKey) throws Exception {
  32.         Cipher cipher = Cipher.getInstance("RSA");
  33.         cipher.init(Cipher.DECRYPT_MODE, publicKey);
  34.         byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
  35.         return new String(decryptedBytes);
  36.     }
  37. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表