1. ? ?
  2. PowerBuilder
  3. Thursday, 20 June 2024 02:03 AM UTC

请问pb2019的json发送数据时,对方需要对发送的json数据进行带 key  的加密,他们的加密方式例子大致见下面:

///////////////////////////////

加密示例代码

String param = AesUtils.encrypt(参数json字符串, KEY);

​ 解密示例代码

String decrypSre = AesUtils.decryptToken(param, KEY));

​ 加密工具类代码

public class AesUtils {

    private static final Log log = Log.get();
    private static final String ALGORITHM = "AES";

    public static void main(String[] args) {
        String cltk = "{\n" +
                "    \"vesselName\": \"xxx\",\n" +
                "    \"voyage\": \"xxx\"\n" +
                "}";
        String key = "JJ2CQ_903DAAAE7A8387E7379684BC013085CC";
        String encrypt = encrypt(cltk, key);
        log.info("加密后:" + encrypt.length() + " " + encrypt);
        String decryptToken = decryptToken(encrypt, key);
        log.info("解密后:" + decryptToken);
    }

    /**
     * 加密
     */
    public static String encrypt(String str, String password) {
        try {
            byte[] enCodeFormat = getSecretKey(password);
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] byteContent = str.getBytes("UTF-8");
            // 初始化
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            // 加密
            return Base64.encode(result);
        } catch (Exception e) {
            log.error("encrypt password failed, ", e);
        }
        return null;
    }


    /**
     * 加密,去除特殊字符
     */
    public static String encryptExcludeSpecialChars(String str, String password) {
        try {
            byte[] enCodeFormat = getSecretKey(password);
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            // 创建密码器
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] byteContent = str.getBytes("UTF-8");
            // 初始化
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            return new String(result);
        } catch (Exception e) {
            log.error("encrypt password failed, ", e);
        }
        return null;
    }

    /**
     * 解密
     */
    public static String decryptToken(String str, String password) {
        log.info("解密密文:{},秘钥:{}", str, password);
        // 长token解密得到明文token
        String realToken = "";
        try {
            realToken = decryptEcb(str, password);
        } catch (Exception e) {
            log.error("decrypt password failed, ", e);
        }
        log.info("解密明文:{}", realToken);
        return realToken;
    }

    /**
     * 解密,没有特殊字符情况下
     */
    public static String decryptExcludeSpecialChars(String str, String password) {
        log.info("解密密文:{},秘钥:{}", str, password);
        String decryptStr = "";
        try {
            decryptStr = decryptEcbExcludeSpecialChars(str, password);
        } catch (Exception e) {
            log.error("decrypt password failed, ", e);
        }
        log.info("解密明文:{}", decryptStr);
        return decryptStr;
    }

    private static String decryptEcb(String data, String key) throws Exception {
        if (ObjectUtil.isEmpty(key) || key.length() < 16) {
            return null;
        }
        byte[] decryptdata = Base64.decode(data);
        byte[] skey = getSecretKey(key);
        // 解密
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, toKey(skey));
        byte[] result = cipher.doFinal(decryptdata);
        return new String(result);
    }

    private static String decryptEcbExcludeSpecialChars(String data, String key) throws Exception {
        if (ObjectUtil.isEmpty(key) || key.length() < 16) {
            return null;
        }
        byte[] decryptdata = data.getBytes("UTF-8");
        byte[] skey = getSecretKey(key);
        // 解密
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, toKey(skey));
        byte[] result = cipher.doFinal(decryptdata);

        // 转化为字符串
        return new String(result);
    }
    private static byte[] getSecretKey(String key) throws UnsupportedEncodingException {
        return key.substring(0, 16).getBytes("UTF-8");
    }
    private static Key toKey(byte[] key) throws IllegalArgumentException {
        return new SecretKeySpec(key, ALGORITHM);
    }
/////////////////

请问下,那我们pb 在发送json数据过去 也要进行 key 方式加密,那pb 怎样处理呢? 能给个例子吗?
谢谢
Peter Pang @Appeon Accepted Answer Pending Moderation
  1. Friday, 21 June 2024 01:05 AM UTC
  2. PowerBuilder
  3. # 1

你好,

从代码来看像C#的ASE加解密,要与PB相互信息,请确保两边加密和解密算法一致,PB中CrypterObject对象的SymmetricEncrypt函数有ASE加密方式,请使用相同Key和加密方式看看加密出来的密文是否与C#一样?参考信息:https://docs.appeon.com/pb2019r3/powerscript_reference/ch02s04s828.html

如果密文不一致,说明算法有差异,可以把C#算法函数封装为标准类库dll,PB通过.NET DLL Importer工具导入这个dll生成PB用户对象,这样可以确保使用同一个加解密算法。参考信息:https://docs.appeon.com/pb2019r3/application_techniques/ch05s01.html

 

Best Regards,
Peter

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Thursday, 20 June 2024 13:23 PM UTC
  2. PowerBuilder
  3. # 2

I've not used it, but PB 2019 provides the CrypterObject object that provides encryption and decryption functionality using popular algorithms. Look in PB Help for this object, or online here:

    https://docs.appeon.com/pb2019r3/objects_and_controls/ch02s15.html

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.