java 加密 rsa_前后端分离密码登陆加密RSA方案(java后端)

前言:密码加密有很多种方案,这里不做过多讨论,本篇文章是基于RSA加密实现。

首先在前端工程中需要引入加密js: "jsencrypt": "2.3.1",(注意单独导入可能报错,可以删除整个node_modules,然后重新npm install)

然后在登陆提交表单的地方代码修改如下:

// 引入js

import {JSEncrypt} from 'jsencrypt'

// 提交表单方法

dataFormSubmit () {

this.dataForm.password = this.passwordEncryption(this.dataForm.password + ',' + new Date().getTime())

this.$http({

url: this.$http.adornUrl('/sys/login'),

method: 'post',

data: this.$http.adornData({

'username': this.dataForm.userName,

'password': this.dataForm.password,

'uuid': this.dataForm.uuid,

'captcha': this.dataForm.captcha

})

},

//密码加密方法

passwordEncryption (passwordUser) {

c

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA算法是一种非对称加密算法,其中公钥可以用于加密数据,私钥则用于解密数据。而OAEP(Optimal Asymmetric Encryption Padding)是RSA算法加密时的一种填充方式,用于增加加密的安全性。在Java中,可以使用Java Cryptography Extension(JCE)提供的API来实现RSA算法和OAEP填充方式的加密与解密操作。以下是一个使用RSA算法和OAEP填充方式进行加密Java示例代码: ```java import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PublicKey; import java.security.PrivateKey; import javax.crypto.Cipher; public class RSAEncryptor { private static final String RSA_ALGORITHM = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding"; public static void main(String[] args) throws Exception { String plainText = "Hello, world!"; KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); byte[] encryptedData = encrypt(plainText.getBytes(), publicKey); byte[] decryptedData = decrypt(encryptedData, privateKey); System.out.println("Plaintext: " + plainText); System.out.println("Encrypted data: " + new String(encryptedData)); System.out.println("Decrypted data: " + new String(decryptedData)); } public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance(RSA_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } } ``` 在这个示例代码中,我们使用KeyPairGenerator生成一个RSA密钥对,然后使用公钥加密一个字符串,最后使用私钥解密该字符串。注意,我们使用的RSA算法的填充方式为“OAEPWithSHA-256AndMGF1Padding”,这是一种较为安全的填充方式,可以提高加密的安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值