CryptoJs Encryption在android中不起作用

我必须在Android应用程序中实现encryption。 网页开发人员正在使用CryptoJs库。 意味着encryptionalgorithm是AES256encryption。

iOS和Android平台都给出了不同的string,iOS上的一个接受了web.Itstring应该是一样的。

我正在使用下面的代码片段(有2个不同的function):

private void newEnc() { String secret = "LSC@SD2017@ps"; String cipherText = "{\"device_type\":\"iOS\",\"email\" : \"jhon@gmail.com\",\"device_id\" : \"14105DA4-CEE5-431E-96A2-2331CDA7F062\",\"password\" : \"123456\",\"device_token\" : \"B44777563552882EC3139A0317E401B55D6FC699D0AC3D279F392927CAF9B566\"}"; KeyGenerator kgen = null; try { kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(secret.getBytes("UTF8")); kgen.init(256, sr); SecretKey skey = kgen.generateKey(); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES"); c.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] decrypted = c.doFinal(cipherText.getBytes()); System.out.println(Base64.encodeToString(decrypted, Base64.NO_WRAP)); // decrypted = Base64.encodeBase64(decrypted); // byte[] iv = Base64.encodeBase64(c.getIV()); // Log.e("encryptString", new String(decrypted)); // Log.d("encryptString iv", new String(iv)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } 

我也用过:

 private void enctest(String cipherText) { String secret = "LSC@SD2017@ps"; // String cipherText = "{\"device_type\":\"iOS\",\"email\" : \"jhon@gmail.com\",\"device_id\" : \"14105DA4-CEE5-431E-96A2-2331CDA7F062\",\"password\" : \"123456\",\"device_token\" : \"B44777563552882EC3139A0317E401B55D6FC699D0AC3D279F392927CAF9B566\"}"; MessageDigest md5 = null; try { // String cipherText = "U2FsdGVkX1+tsmZvCEFa/iGeSA0K7gvgs9KXeZKwbCDNCs2zPo+BXjvKYLrJutMK+hxTwl/hyaQLOaD7LLIRo2I5fyeRMPnroo6k8N9uwKk="; byte[] cipherData = Base64.decode(cipherText.getBytes(), Base64.NO_WRAP); byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16); md5 = MessageDigest.getInstance("MD5"); final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes("UTF-8"), md5); SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES"); IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]); byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length); Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding"); aesCBC.init(Cipher.ENCRYPT_MODE, key, iv); byte[] decryptedData = aesCBC.doFinal(cipherText.getBytes("UTF-8")); // String plainText = "Hello, World! This is a Java/Javascript AES test."; // SecretKey key = new SecretKeySpec( // Base64.decodeBase64("u/Gu5posvwDsXUnV5Zaq4g=="), "AES"); // AlgorithmParameterSpec iv = new IvParameterSpec( // Base64.decodeBase64("5D9r9ZVzEYYgha93/aUK2w==")); // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // cipher.init(Cipher.ENCRYPT_MODE, key, iv); // System.out.println(Base64.encodeBase64String(cipher.doFinal( // plainText.getBytes("UTF-8")))); // String decryptedText = new String(decryptedData, "UTF-8"); System.out.println(Base64.encodeToString(decryptedData, Base64.NO_WRAP)); // enctest(decryptedText); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } 

但没有一个给出相同的结果。

在iOS中,他们使用https://github.com/etienne-martin/CryptoJS.swift

我应该怎么做,我们的两个encryption的string匹配。

实际的cipherText (不要混淆具有相同variables名称的string)被格式化并以“Salted__”开始,并且可能是encryption参数。 两种不同的function以不同的格式创build不同的输出。 他们不能产生相同的输出。

注1,混淆cipherText

 // String cipherText = "{\"device_type\":\"iOS\",\"email\" : \"jhon@gmail.com\",\"device_id\" : \"14105DA4-CEE5-431E-96A2-2331CDA7F062\",\"password\" : \"123456\",\"device_token\" : \"B44777563552882EC3139A0317E401B55D6FC699D0AC3D279F392927CAF9B566\"}"; // String cipherText = "U2FsdGVkX1+tsmZvCEFa/iGeSA0K7gvgs9KXeZKwbCDNCs2zPo+BXjvKYLrJutMK+hxTwl/hyaQLOaD7LLIRo2I5fyeRMPnroo6k8N9uwKk="; 

笔记2:
Base64对于人类来说是非常有用的,它是为电脑devise的,hex是为人类和计算机提供的一个字节对应的直接位。