Spring Boot 在我例子中,我需要将整个JSON对象转换为加密格式,如何转换?

cngwdvgl  于 2023-08-04  发布在  Spring
关注(0)|答案(1)|浏览(136)

String getString = new String(); String getString = new String();

inputParams.put("referenceid", referenceid);
        inputParams.put("sourcetype",sourcetype );
        inputParams.put("source", source);
        
        byte[] encryptedJson=PasswordUtils.encryptJson(inputParams);
        String encryptedInput=new String(encryptedJson);

public static byte[] encryptJson(JSONObject inputParams) throws Exception {
    
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream objectStream= new ObjectOutputStream(byteStream);
    objectStream.writeObject(inputParams);
    byte[] serializedObject=byteStream.toByteArray();
    String key="qPNNbUwcSOhXgWGwdBOXopjgSNPynDkC";
    byte[] keyByte=key.getBytes();
    byte[] hashedKey=generateHash(keyByte);
    
    SecretKeySpec secretKeySpec=new SecretKeySpec(hashedKey, "AES");
    Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] encryptedObject=cipher.doFinal(serializedObject);
    return encryptedObject;
    
}

字符串
我试图将整个json对象转换为加密数据,然后我应该为转换后的json数据提供输入,这里错误通过加密方法。这里我应该遵循AES ECB PCKS 5 Padding算法。

dw1jzc5e

dw1jzc5e1#

你想要这样东西吗??

JSONObject inputParams = new JSONObject();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        byteStream.writeBytes(inputParams.toString().getBytes());
        String key = "AKeyForYourValue?";
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        byte[] hashedKey = sha256.digest(key.getBytes("UTF-8"));

        SecretKey secretKey = new SecretKeySpec(hashedKey, "AES");

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedObject = cipher.doFinal(byteStream.toByteArray());

        System.out.println(new String(encryptedObject));

字符串
我不确定我是否正确理解了你的问题

相关问题