Jmeter编写的加密/解密代码的问题

发布于 2025-02-11 14:41:08 字数 2375 浏览 1 评论 0原文

我已经在Beanshell中编写了一个代码,用于加密和解密Jmeter,但某种程度上不起作用。我会遇到错误:在文件中:``Import java.security.invalidalgorithmparameterexception; inline评估;导入java.security.in。 。 。 ''遇到的“}”在第19行,第82列。 我在测试计划中添加了加密罐子,但问题仍然存在。

附件是代码。

工作:

  1. 我正在生成一个随机字符串(R1),并使用RSA Algo使用公共密钥对其进行加密。
  2. 使用密钥R1,我需要使用AES ALGO,CBC模式,PKCS7PADDING加密请求主体。
        package com.sample.feedbackrating;
        import java.security.InvalidAlgorithmParameterException;
        import java.security.InvalidKeyException;
        import java.security.NoSuchAlgorithmException;
        import java.security.SecureRandom;
        import java.util.Base64;
        import java.util.UUID;
        import javax.crypto.BadPaddingException;
        import javax.crypto.Cipher;
        import javax.crypto.IllegalBlockSizeException;
        import javax.crypto.NoSuchPaddingException;
        import javax.crypto.spec.IvParameterSpec;
        import javax.crypto.spec.SecretKeySpec;

    public class Crypto {
    

        public static IvParameterSpec generateIv() {
        byte[] iv = new byte[16];
        return new IvParameterSpec(iv);
    }

        public static String encrypt(String algorithm, String input, String secretKey, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException

    {
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, new 
    SecretKeySpec(secretKey.getBytes(), "AES"), iv);
    byte[] cipherText = cipher.doFinal(input.getBytes());
    return Base64.getEncoder().encodeToString(cipherText);
    }
    
    public static String decrypt(String algorithm, String cipherText, String secretKey, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,InvalidAlgorithmParameterException, InvalidKeyException,BadPaddingException, IllegalBlockSizeException

    {
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey.getBytes(), "AES"), iv);
    byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
    return new String(plainText);
}

        public static String generateSecretKey() {
            return UUID.randomUUID().toString().replace("-", "");
        }

    }

I have written a code in beanshell for encryption and decryption in Jmeter but somehow its not working. I am getting error : In file: inline evaluation of: ``import java.security.InvalidAlgorithmParameterException; import java.security.In . . . '' Encountered "}" at line 19, column 82.
I have added the crypto jar in test plan but issue persists.

Attached is the code.

Working :

  1. I am generating a random string(R1) and encryption it with a public key using RSA algo.
  2. Using the key R1, I need to encrypt the request body using AES algo, CBC mode, PKCS7Padding.
        package com.sample.feedbackrating;
        import java.security.InvalidAlgorithmParameterException;
        import java.security.InvalidKeyException;
        import java.security.NoSuchAlgorithmException;
        import java.security.SecureRandom;
        import java.util.Base64;
        import java.util.UUID;
        import javax.crypto.BadPaddingException;
        import javax.crypto.Cipher;
        import javax.crypto.IllegalBlockSizeException;
        import javax.crypto.NoSuchPaddingException;
        import javax.crypto.spec.IvParameterSpec;
        import javax.crypto.spec.SecretKeySpec;

    public class Crypto {
    

        public static IvParameterSpec generateIv() {
        byte[] iv = new byte[16];
        return new IvParameterSpec(iv);
    }

        public static String encrypt(String algorithm, String input, String secretKey, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException

    {
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, new 
    SecretKeySpec(secretKey.getBytes(), "AES"), iv);
    byte[] cipherText = cipher.doFinal(input.getBytes());
    return Base64.getEncoder().encodeToString(cipherText);
    }
    
    public static String decrypt(String algorithm, String cipherText, String secretKey, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,InvalidAlgorithmParameterException, InvalidKeyException,BadPaddingException, IllegalBlockSizeException

    {
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey.getBytes(), "AES"), iv);
    byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
    return new String(plainText);
}

        public static String generateSecretKey() {
            return UUID.randomUUID().toString().replace("-", "");
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2025-02-18 14:41:08

遇到的“}”是一个语法错误,因此请仔细检查您的beanshell脚本,以了解最终缺少的半柱或闭合括号或其他内容。您可以使用在线棉绒工具,例如这个以查看问题的确切问题

,我一般无法重现您的问题:

因此,它是复制问题或与BeanShell相关的问题,请注意,从jmeter 3.1开始,您应该使用JSR223测试元素和Groovy语言进行脚本,尤其是对于加密操作等“重型”任务。

Encountered "}" is a syntax error so double check your Beanshell script for eventual missing semicolons or closing brackets or whatever. You can use online lint tool like this one to see where exactly the problem is

In general I cannot reproduce your issue:

enter image description here

So it's either a copy-paste issue or a Beanshell-related problem, be aware that starting from JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting, especially for "heavy" tasks like cryptographic operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文