您的位置 首页 java

JMeter学习:引入jar包工具类对密码进行加密

前言

  • 第一部分:编写加密方法
  • 第二部分:生成jar包
  • 第三部分: jmeter 中引入jar包并使用

一、 RSA 密码加密传输

之前有写过一篇关于RSA加密相关的文章,当时用python实现的。思路是一样的,这里用java实现。贴上相关链接,大家可以参考下之前的一篇文章

代码示例

 package com.login.api;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class LoginTools {

    static String PUBLIC_KEY = "换成你的key";
    //公钥加密密码
    public static String encrypt(String password) throws Exception {
        byte[] bytes = PUBLIC_KEY.getBytes("UTF-8");
        Cipher cipher = Cipher.getInstance("RSA");
        byte[] decoded = Base64.getDecoder().decode(bytes);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] a = Base64.getEncoder().encode(cipher.doFinal(password.getBytes(StandardCharsets.UTF_8)));
        String outStr = java.util.Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes(StandardCharsets.UTF_8)));
        return outStr;
    }

    public static void main(String[] args) throws Exception {
        String pwd = encrypt("123456f");
        System.out.println(pwd);
    }


}
  

二、 使用 IDEA 生成jar包

1、首先在本地运行测试后,会在target下生成对应的class文件

2、IDEA->File->Project Structrue…

3、打开配置项配置信息

4、添加 源文件 生成的class文件

5、选择对应的工具类生成的class文件

6、配置下mainifest文件,点击【OK】保存

7、Build->Build Artifacts

8、生成jar包

三、jmeter中引入jar并使用

将生成的jar包拷贝到jmeter的路径lib下或者lib的ext下,都可以。然后就可以在工具的愉快地使用了。

以上只记录了如何引用jar包工具,不正确的地方,欢迎大家指正。

文章来源:智云一二三科技

文章标题:JMeter学习:引入jar包工具类对密码进行加密

文章地址:https://www.zhihuclub.com/184736.shtml

关于作者: 智云科技

热门文章

网站地图