Sure, here’s the translation: Java解密来自CryptoJS的内容

huangapple go评论124阅读模式
英文:

Java decryption from CryptoJS

问题

以下是您提供的代码部分的翻译:

在Angularjs中的加密

  1. CryptoJS.AES.encrypt(message, "my secret key").toString();

在Java中的解密

  1. package com.mypackage;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. import java.util.Base64;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. public class AES {
  10. private static SecretKeySpec secretKey;
  11. private static byte[] key;
  12. public static void setKey(String myKey) {
  13. MessageDigest sha = null;
  14. try {
  15. key = myKey.getBytes("UTF-8");
  16. sha = MessageDigest.getInstance("SHA-1");
  17. key = sha.digest(key);
  18. key = Arrays.copyOf(key, 16);
  19. secretKey = new SecretKeySpec(key, "AES");
  20. } catch (NoSuchAlgorithmException e) {
  21. e.printStackTrace();
  22. } catch (UnsupportedEncodingException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. public static String decrypt(String strToDecrypt, String secret) {
  27. try {
  28. setKey(secret);
  29. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  30. cipher.init(Cipher.DECRYPT_MODE, AES.secretKey);
  31. return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
  32. } catch (Exception e) {
  33. System.out.println("解密时出错:" + e.toString());
  34. }
  35. return null;
  36. }
  37. }

调用Java解密方法

  1. @PostMapping("/decryption")
  2. public ResponseEntity<DecryptResponseBean> decryption(@RequestBody DecryptRequestBean data) {
  3. String dataToDecrypt = data.getData();
  4. String decrypted = AES.decrypt(dataToDecrypt, "my secret key");
  5. DecryptResponseBean responseBean = new DecryptResponseBean(decrypted);
  6. return ResponseEntity.ok(responseBean);
  7. }

我已经完成了您提供的代码部分的翻译。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

I'm trying yo decrypt Java-size some informations previously crypted in CryptoJS (angualrjs).
Here are some code snippet:

Encryption in Angularjs

  1. CryptoJS.AES.encrypt(message, &quot;my secret key&quot;).toString();

Decryptionin Java

  1. package com.mypackage;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. import java.util.Base64;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. public class AES {
  10. private static SecretKeySpec secretKey;
  11. private static byte[] key;
  12. public static void setKey(String myKey) {
  13. MessageDigest sha = null;
  14. try {
  15. key = myKey.getBytes(&quot;UTF-8&quot;);
  16. sha = MessageDigest.getInstance(&quot;SHA-1&quot;);
  17. key = sha.digest(key);
  18. key = Arrays.copyOf(key, 16);
  19. secretKey = new SecretKeySpec(key, &quot;AES&quot;);
  20. }
  21. catch (NoSuchAlgorithmException e) {
  22. e.printStackTrace();
  23. }
  24. catch (UnsupportedEncodingException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. public static String decrypt(String strToDecrypt, String secret) {
  29. try{
  30. setKey(secret);
  31. Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
  32. cipher.init(Cipher.DECRYPT_MODE, AES.secretKey);
  33. return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
  34. } catch (Exception e) {
  35. System.out.println(&quot;Error while decrypting: &quot; + e.toString());
  36. }
  37. return null;
  38. }
  39. }

Calling java decrypt method

  1. @PostMapping(&quot;/decryption&quot;)
  2. public ResponseEntity&lt;DecryptResponseBean&gt; decryption(@RequestBody DecryptRequestBean data){
  3. String dataToDecrypt = data.getData();
  4. String decrypted = AES.decrypt(dataToDecrypt, &quot;my secret key&quot;);
  5. DecryptResponseBean responseBean = new DecryptResponseBean(decrypted);
  6. return ResponseEntity.ok(responseBean);
  7. }

I'm obtaining the following exception:

java.security.InvalidKeyException: Parameters missing

What am I doing wrong?

EDIT1 full stacktrace of Exception

  1. java.security.InvalidKeyException: Parameters missing
  2. at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469)
  3. at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
  4. at javax.crypto.Cipher.implInit(Cipher.java:805)
  5. at javax.crypto.Cipher.chooseProvider(Cipher.java:867)
  6. at javax.crypto.Cipher.init(Cipher.java:1252)
  7. at javax.crypto.Cipher.init(Cipher.java:1189)
  8. at com.ses.aes.AES.decrypt(AES.java:53)
  9. at com.ses.services.Encryption.decryption(Encryption.java:34)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  12. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  13. at java.lang.reflect.Method.invoke(Unknown Source)
  14. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
  15. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
  16. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
  17. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
  18. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
  19. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
  20. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
  21. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
  22. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
  23. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
  24. at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
  25. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
  26. at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  27. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
  28. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  29. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  30. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  31. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  32. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  33. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
  34. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  35. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  36. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
  37. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
  38. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  39. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  40. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
  41. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
  42. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  43. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  44. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
  45. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:109)
  46. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  47. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  48. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
  49. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  50. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
  51. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
  52. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
  53. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
  54. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  55. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
  56. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  57. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853)
  58. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587)
  59. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  60. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  61. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  62. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  63. at java.lang.Thread.run(Unknown Source)
  64. Error while decrypting: java.security.InvalidKeyException: Parameters missing

EDIT2: set of sample data

  1. Key: 1234567890123456 (Both Angular and Java side)
  2. Clear data: this is my text
  3. Chipered data in CryptoJS: U2FsdGVkX18C7J5wy5R5FRjfP5Xghpry3FtuTj5xq+o=
  4. What Java backend receives: U2FsdGVkX18C7J5wy5R5FRjfP5Xghpry3FtuTj5xq+o=

答案1

得分: 1

  1. import javax.crypto.BadPaddingException;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.IllegalBlockSizeException;
  4. import javax.crypto.NoSuchPaddingException;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.nio.charset.StandardCharsets;
  8. import java.security.*;
  9. import java.util.Arrays;
  10. import java.util.Base64;
  11. public class Main {
  12. public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
  13. System.out.println("https://stackoverflow.com/questions/63484091/java-decryption-from-cryptojs");
  14. System.out.println("Java decryption from CryptoJS");
  15. String secret = "1234567890123456";
  16. String cipherText = "U2FsdGVkX18C7J5wy5R5FRjfP5Xghpry3FtuTj5xq+o=";
  17. String expectedData = "this is my text";
  18. // decode base64 encoding
  19. byte[] ciphertextData = Base64.getDecoder().decode(cipherText);
  20. byte[] saltData = Arrays.copyOfRange(ciphertextData, 8, 16);
  21. // generate key & iv
  22. MessageDigest md5 = MessageDigest.getInstance("MD5");
  23. final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);
  24. SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");
  25. IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
  26. // get encrypted data without iv
  27. byte[] encrypted = Arrays.copyOfRange(ciphertextData, 16, ciphertextData.length);
  28. Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");
  29. aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
  30. // decryption
  31. byte[] decryptedData = aesCBC.doFinal(encrypted);
  32. String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);
  33. System.out.println("decrypted text: " + decryptedText);
  34. System.out.println("expected data: " + expectedData);
  35. }
  36. public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {
  37. int digestLength = md.getDigestLength();
  38. int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;
  39. byte[] generatedData = new byte[requiredLength];
  40. int generatedLength = 0;
  41. try {
  42. md.reset();
  43. // Repeat process until sufficient data has been generated
  44. while (generatedLength < keyLength + ivLength) {
  45. // Digest data (last digest if available, password data, salt if available)
  46. if (generatedLength > 0)
  47. md.update(generatedData, generatedLength - digestLength, digestLength);
  48. md.update(password);
  49. if (salt != null)
  50. md.update(salt, 0, 8);
  51. md.digest(generatedData, generatedLength, digestLength);
  52. // additional rounds
  53. for (int i = 1; i < iterations; i++) {
  54. md.update(generatedData, generatedLength, digestLength);
  55. md.digest(generatedData, generatedLength, digestLength);
  56. }
  57. generatedLength += digestLength;
  58. }
  59. // Copy key and IV into separate byte arrays
  60. byte[][] result = new byte[2][];
  61. result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);
  62. if (ivLength > 0)
  63. result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);
  64. return result;
  65. } catch (DigestException e) {
  66. throw new RuntimeException(e);
  67. } finally {
  68. // Clean out temporary data
  69. Arrays.fill(generatedData, (byte)0);
  70. }
  71. }
  72. }
英文:

With a little search here on Stackoverflow you could have found the same solution as this here. The important code is not from myself but taken from the answer https://stackoverflow.com/a/41434590/8166854 by author @Codo, so all credit go to him.

Crypto.JS is deriving the key and initialization vector (iv) from a passphrase ("key") so the method GenerateKeyAndIV is responsible to get key and IV.

The following code has no proper exception handling and is for educational purposes only.

result:

  1. Java decryption from CryptoJS
  2. decrypted text: this is my text
  3. expected data: this is my text

code:

  1. import javax.crypto.BadPaddingException;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.IllegalBlockSizeException;
  4. import javax.crypto.NoSuchPaddingException;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.nio.charset.StandardCharsets;
  8. import java.security.*;
  9. import java.util.Arrays;
  10. import java.util.Base64;
  11. public class Main {
  12. public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
  13. System.out.println(&quot;https://stackoverflow.com/questions/63484091/java-decryption-from-cryptojs&quot;);
  14. System.out.println(&quot;Java decryption from CryptoJS&quot;);
  15. String secret = &quot;1234567890123456&quot;;
  16. String cipherText = &quot;U2FsdGVkX18C7J5wy5R5FRjfP5Xghpry3FtuTj5xq+o=&quot;;
  17. String expectedData = &quot;this is my text&quot;;
  18. // decode base64 encoding
  19. byte[] ciphertextData = Base64.getDecoder().decode(cipherText);
  20. byte[] saltData = Arrays.copyOfRange(ciphertextData, 8, 16);
  21. // generate key &amp; iv
  22. MessageDigest md5 = MessageDigest.getInstance(&quot;MD5&quot;);
  23. final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);
  24. SecretKeySpec key = new SecretKeySpec(keyAndIV[0], &quot;AES&quot;);
  25. IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
  26. // get encrypted data without iv
  27. byte[] encrypted = Arrays.copyOfRange(ciphertextData, 16, ciphertextData.length);
  28. Cipher aesCBC = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
  29. aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
  30. // decryption
  31. byte[] decryptedData = aesCBC.doFinal(encrypted);
  32. String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);
  33. System.out.println(&quot;decrypted text: &quot; + decryptedText);
  34. System.out.println(&quot;expected data: &quot; + expectedData);
  35. }
  36. /**
  37. * Generates a key and an initialization vector (IV) with the given salt and password.
  38. * &lt;p&gt;
  39. * This method is equivalent to OpenSSL&#39;s EVP_BytesToKey function
  40. * (see https://github.com/openssl/openssl/blob/master/crypto/evp/evp_key.c).
  41. * By default, OpenSSL uses a single iteration, MD5 as the algorithm and UTF-8 encoded password data.
  42. * &lt;/p&gt;
  43. * @param keyLength the length of the generated key (in bytes)
  44. * @param ivLength the length of the generated IV (in bytes)
  45. * @param iterations the number of digestion rounds
  46. * @param salt the salt data (8 bytes of data or &lt;code&gt;null&lt;/code&gt;)
  47. * @param password the password data (optional)
  48. * @param md the message digest algorithm to use
  49. * @return an two-element array with the generated key and IV
  50. * answered Jan 2 &#39;17 at 23:38 author Codo
  51. * https://stackoverflow.com/a/41434590/8166854
  52. */
  53. public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {
  54. int digestLength = md.getDigestLength();
  55. int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;
  56. byte[] generatedData = new byte[requiredLength];
  57. int generatedLength = 0;
  58. try {
  59. md.reset();
  60. // Repeat process until sufficient data has been generated
  61. while (generatedLength &lt; keyLength + ivLength) {
  62. // Digest data (last digest if available, password data, salt if available)
  63. if (generatedLength &gt; 0)
  64. md.update(generatedData, generatedLength - digestLength, digestLength);
  65. md.update(password);
  66. if (salt != null)
  67. md.update(salt, 0, 8);
  68. md.digest(generatedData, generatedLength, digestLength);
  69. // additional rounds
  70. for (int i = 1; i &lt; iterations; i++) {
  71. md.update(generatedData, generatedLength, digestLength);
  72. md.digest(generatedData, generatedLength, digestLength);
  73. }
  74. generatedLength += digestLength;
  75. }
  76. // Copy key and IV into separate byte arrays
  77. byte[][] result = new byte[2][];
  78. result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);
  79. if (ivLength &gt; 0)
  80. result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);
  81. return result;
  82. } catch (DigestException e) {
  83. throw new RuntimeException(e);
  84. } finally {
  85. // Clean out temporary data
  86. Arrays.fill(generatedData, (byte)0);
  87. }
  88. }
  89. }

huangapple
  • 本文由 发表于 2020年8月19日 17:36:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63484091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定