android中的加密字符串和node.js中的解密不起作用

时间:2017-08-30 06:19:21

标签: java android encryption aes cryptojs

我必须在Android应用中加密数据并在Node.js中解密。但是在网络端解密不起作用。

我需要实施AES256加密,并在网络端使用cryptoJS加密库。

这是要加密的字符串:{"email":"jhon@gmail.com","device_type":"iOS","password":"123456","device_id":"14105DA4-CEE5-431E-96A2-2331CDA7F062","device_token":"B44777563552882EC3139A0317E401B55D6FC699D0AC3D279F392927CAF9B566"}

结果应该是

"U2FsdGVkX19hTYqle0GhmGoOK2oKGPfPXDkKsH8MdANABQROPiQ7hxnSA289rRT4fds5yZG4FX7BoTKqzV40NLIpeBoRJw6Uazax8kiKyxRYDUBPNpevD7vveDLoWekRL2cLZTV3S0gkynhL\/fKMg7BUFfT3Lt9BZeiFxYKZKD++ul5DG1isRycwG06l5ZgQJDwfyNAlRz7WbJXrevvCQPMagoCqrB4i49Mgj2ANMLFzUDyImhn9gX3PGrlzlY8xgzhg2oQfHF\/5USd5pqXswTRzVhGxUoUhNahf3pFZtbscr0AyvvE8ffF6ftRo4ZLYRt72aB9Br\/vqshXxr9VYuQ=="

Currenly我使用下面的代码:

//加密输入jo的位置,输出查询并输入ENCRPYTION_KEy

String secret = "LSC@SD2017@ps";
        byte[] input = new byte[0];
        String query = null;
        try {
            input = cipherText1.getBytes();

            byte[] cipherData = Base64.decode(cipherText1.getBytes(), Base64.NO_WRAP);
            byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);

            MessageDigest md5 = MessageDigest.getInstance("MD5");

            final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes("UTF-8"), md5);


            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(secret.getBytes("UTF-8"));
            SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skc);

            byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
            int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
            ctLength += cipher.doFinal(cipherText, ctLength);

             query = Base64.encodeToString(cipherText, Base64.NO_WRAP);

            System.out.println("NEw code\n" + query);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (ShortBufferException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

在iOS中,代码完全正常,他们正在使用:https://github.com/etienne-martin/CryptoJS.swift

请帮我获得相同的输出。

0 个答案:

没有答案