当前位置:   article > 正文

Java读取文件中多个JSON对象,并且16进制字符串和byte相互转换,将byte转为16进制字符串并写入json文件_java读取文件多个json对象

java读取文件多个json对象

Java读取文件中多个JSON对象

File file = new File("/home/renjx/testcases.json");
		//将json转为maps
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, String>> maps = objectMapper.readValue(file, new TypeReference<List<Map<String, String>>>() {
        });
        for (Map<String, String> map : maps) {
            //将16进制字符串转为byte[]
            byte[] privateKey = DatatypeConverter.parseHexBinary(map.get("private_key"));
            byte[] publicKey = DatatypeConverter.parseHexBinary(map.get("public_key"));
            byte[] signature = DatatypeConverter.parseHexBinary(map.get("signature"));
            byte[] encrypted_message = DatatypeConverter.parseHexBinary(map.get("encrypted_message"));
            String message = map.get("message");
            boolean isValid = Secp256k1Crypto.verifySignature(message.getBytes(), signature, publicKey);
            if (!isValid) {
                logger.warn("sign and verify failed!");
                break;
            }

            byte[] plainText = Secp256k1Crypto.decryptMessage(encrypted_message, privateKey, (byte) 0x02);
            if (!Arrays.equals(plainText, message.getBytes())){
                logger.warn("sign and verify failed!");
                break;
            }
        }
       
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

将byte写入到json文件中

private static void testCVerifySignatureAndDecryptMessage() throws Exception {
        byte[] privateKey = Secp256k1Crypto.generatePrivateKey();
        byte[] publicKey = Secp256k1Crypto.generatePublicKeyFromPrivateKey(privateKey);
        int randomInt = new Random().nextInt(512);
        String hexString = CommonUtil.generateRandomHexString(randomInt * 2);
        byte[] signature = Secp256k1Crypto.signMessage(hexString.getBytes(), privateKey);
        byte[] cipher = Secp256k1Crypto.encryptMessage(hexString.getBytes(), publicKey, (byte) 0x02);

        String private_key = bytesToHexFun(privateKey);
        String public_key = bytesToHexFun(publicKey);
        String signatures = bytesToHexFun(signature);
        String encrypted_message = bytesToHexFun(cipher);
        Map<String, Object> map = new HashMap<>();

        map.put("private_key", private_key);
        map.put("public_key", public_key);
        map.put("signature", signatures);
        map.put("encrypted_message", encrypted_message);
        map.put("message", hexString);
        String filePath = "/home/renjx/testcasesJava.json";
        String isWrite = writeJson(filePath, map, true);
        System.out.println(isWrite);
    }
    public static String writeJson(String jsonPath, Map<String, Object> inMap, boolean flag) {
        // Map数据转化为Json,再转换为String
        String data = new JSONObject(inMap).toString();
        File jsonFile = new File(jsonPath);
        try {
            // 文件不存在就创建文件
            if (!jsonFile.exists()) {
                jsonFile.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(jsonFile.getAbsoluteFile(), flag);
            BufferedWriter bw = new BufferedWriter(fileWriter);
            bw.write(data);
            bw.close();
            return "success";
        } catch (IOException e) {
            return "error";
        }
    }
    //将byte转为16进制字符串
    public static String bytesToHexFun(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for(byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }
    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/896502
推荐阅读
相关标签
  

闽ICP备14008679号