Search in sources :

Example 1 with RSA

use of cn.hutool.crypto.asymmetric.RSA in project netty-mqtt by devhg.

the class AuthService method checkValid.

@Override
public boolean checkValid(String username, String password) {
    if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
        return false;
    }
    RSA rsa = new RSA(privateKey, null);
    String value = rsa.encryptBcd(username, KeyType.PrivateKey);
    return value.equals(password);
}
Also used : RSA(cn.hutool.crypto.asymmetric.RSA)

Example 2 with RSA

use of cn.hutool.crypto.asymmetric.RSA in project netty-mqtt by devhg.

the class PwdUtil method main.

/**
 * 通过用户名和私钥生成密码
 */
public static void main(String[] args) throws IOException {
    System.out.print("输入需要获取密码的用户名: ");
    Scanner scanner = new Scanner(System.in);
    String value = scanner.nextLine();
    RSAPrivateKey privateKey = IoUtil.readObj(PwdUtil.class.getClassLoader().getResourceAsStream("keystore/auth-private.key"));
    RSA rsa = new RSA(privateKey, null);
    System.out.println("用户名: " + value + " 对应生成的密码为: " + rsa.encryptBcd(value, KeyType.PrivateKey));
// 用户名: devhg
// 对应生成的密码为: AFF4FEA3B51EC536631017BC736A8CC55BCF9EB13B179AE4CB35DB212300011C99EAF63368342DAD801245C7BAE78DF6B8BA99E98B74B645EB4CF7F67A838F98
}
Also used : Scanner(java.util.Scanner) RSA(cn.hutool.crypto.asymmetric.RSA) RSAPrivateKey(java.security.interfaces.RSAPrivateKey)

Example 3 with RSA

use of cn.hutool.crypto.asymmetric.RSA in project springboot-templet-start by thedestiny.

the class EncryptUtils method main.

public static void main(String[] args) {
    // 
    // //获得私钥
    // PrivateKey privateKey = rsa.getPrivateKey();
    // //获得公钥
    // PublicKey publicKey = rsa.getPublicKey();
    RSA rsa = new RSA();
    String privateKeyBase64 = rsa.getPrivateKeyBase64();
    String publicKeyBase64 = rsa.getPublicKeyBase64();
    log.info("private key {}", privateKeyBase64);
    log.info("public key {}", publicKeyBase64);
    String priKey = ResourceUtil.readStr("config/pri_key.txt", Charset.forName("UTF-8"));
    String pubKey = ResourceUtil.readStr("config/pub_key.txt", Charset.forName("UTF-8"));
    RSA rsa1 = new RSA(priKey, pubKey);
    // 公钥加密,私钥解密
    byte[] encrypt = rsa1.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
    String encrypt3 = rsa1.encryptBase64(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
    System.out.println("加密秘文 " + StrUtil.str(encrypt, CharsetUtil.CHARSET_UTF_8));
    System.out.println("加密秘文3 " + encrypt3);
    byte[] decrypt = rsa1.decrypt(encrypt, KeyType.PrivateKey);
    String decrypt3 = rsa1.decryptStr(encrypt3, KeyType.PrivateKey);
    String str = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
    System.out.println("解密明文 " + str);
    System.out.println("解密明文3 " + decrypt3);
    // 私钥加密,公钥解密
    byte[] encrypt2 = rsa1.encrypt(StrUtil.bytes("我是一段测试bbb", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
    System.out.println("加密秘文1" + StrUtil.str(encrypt2, CharsetUtil.CHARSET_UTF_8));
    byte[] decrypt2 = rsa1.decrypt(encrypt2, KeyType.PublicKey);
    String str1 = StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8);
    System.out.println("解密明文1" + str1);
}
Also used : RSA(cn.hutool.crypto.asymmetric.RSA)

Example 4 with RSA

use of cn.hutool.crypto.asymmetric.RSA in project springboot-templet-start by thedestiny.

the class RASUtils method main.

public static void main(String[] args) {
    byte[] cfs = HexUtil.decodeHex("ff1790033dd85ed3f78df634732c09cf");
    System.out.println(cfs);
    RSA rsa = new RSA();
    // 获得私钥
    PrivateKey privateKey = rsa.getPrivateKey();
    String privateKeyBase64 = rsa.getPrivateKeyBase64();
    // 获得公钥
    PublicKey publicKey = rsa.getPublicKey();
    String publicKeyBase64 = rsa.getPublicKeyBase64();
    // 公钥加密,私钥解密
    byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
    byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
    Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
    // 私钥加密,公钥解密
    byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
    byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
    Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
}
Also used : RSA(cn.hutool.crypto.asymmetric.RSA) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey)

Example 5 with RSA

use of cn.hutool.crypto.asymmetric.RSA in project kms by mahonelau.

the class SecurityTools method valid.

public static SecurityResp valid(SecurityReq req) {
    SecurityResp resp = new SecurityResp();
    String pubKey = req.getPubKey();
    String aesKey = req.getAesKey();
    String data = req.getData();
    String signData = req.getSignData();
    RSA rsa = new RSA(null, Base64Decoder.decode(pubKey));
    Sign sign = new Sign(SignAlgorithm.SHA1withRSA, null, pubKey);
    byte[] decryptAes = rsa.decrypt(aesKey, KeyType.PublicKey);
    // log.info("rsa解密后的秘钥"+ Base64Encoder.encode(decryptAes));
    AES aes = SecureUtil.aes(decryptAes);
    String dencrptValue = aes.decryptStr(data);
    // log.info("解密后报文"+dencrptValue);
    resp.setData(new JSONObject(dencrptValue));
    boolean verify = sign.verify(dencrptValue.getBytes(), Base64Decoder.decode(signData));
    resp.setSuccess(verify);
    return resp;
}
Also used : RSA(cn.hutool.crypto.asymmetric.RSA) JSONObject(cn.hutool.json.JSONObject) Sign(cn.hutool.crypto.asymmetric.Sign) AES(cn.hutool.crypto.symmetric.AES)

Aggregations

RSA (cn.hutool.crypto.asymmetric.RSA)54 Test (org.junit.Test)27 PublicKey (java.security.PublicKey)9 PrivateKey (java.security.PrivateKey)7 Sign (cn.hutool.crypto.asymmetric.Sign)6 AES (cn.hutool.crypto.symmetric.AES)6 BadRequestException (co.yixiang.exception.BadRequestException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 KeyPair (java.security.KeyPair)3 SecretKey (javax.crypto.SecretKey)3 JSONObject (cn.hutool.json.JSONObject)2 ForbidSubmit (co.yixiang.modules.aop.ForbidSubmit)2 Log (co.yixiang.modules.logging.aop.log.Log)2 UserDto (co.yixiang.modules.system.service.dto.UserDto)2 Operation (io.swagger.v3.oas.annotations.Operation)2 BigInteger (java.math.BigInteger)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 Scanner (java.util.Scanner)2 Cipher (javax.crypto.Cipher)2 ResponseEntity (org.springframework.http.ResponseEntity)2