当前位置:   article > 正文

JDK Base64编解码1.7和1.8的坑_base64.getdecoder().decode

base64.getdecoder().decode

 附:糖豆广场舞永久会员TV版

场景

  • 对接一个第三方api接口,其中签名部分用的是JDK8的编码。我们线上采用JDK7,导致项目无法编译
  • 替换编解码部分为1.7的代码,然后签名又不对
  • 所以坑就在这里,结论,1.7的编解码有换行符导致签名失败

贴代码

  1. import sun.misc.BASE64Decoder;
  2. import java.security.KeyFactory;
  3. import java.security.PrivateKey;
  4. import java.security.Signature;
  5. import java.security.spec.PKCS8EncodedKeySpec;
  6. import java.util.Base64;
  7. public class Base64EncodeDemo {
  8. public static final String CHARSET_UTF_8 = "UTF-8";
  9. public static final String ALGORITHM_RSA = "RSA";
  10. public static final String ALGORITHM_SHA1_WITH_RSA = "SHA1withRSA";
  11. private static String signWithRSAJdk7(String source, String privateKey, String algorithm) {
  12. String result = null;
  13. try {
  14. // 修改为 1.7语法
  15. // PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
  16. PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(privateKey));
  17. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
  18. PrivateKey pk = keyFactory.generatePrivate(spec);
  19. Signature signature = Signature.getInstance(algorithm);
  20. signature.initSign(pk);
  21. signature.update(source.getBytes(CHARSET_UTF_8));
  22. // 修改为 1.7语法
  23. //result = Base64.getEncoder().encodeToString(signature.sign());
  24. result = new sun.misc.BASE64Encoder().encode(signature.sign());
  25. System.out.println("jdk7解码签名换行符去掉前:"+result);
  26. // 1.7语法需要去掉换行符 这里是重点
  27. result = result.replaceAll("\\n","");
  28. System.out.println("jdk7解码签名换行符去掉后:"+result);
  29. } catch (Exception e) {
  30. System.out.println("RSA 签名出错!");
  31. e.printStackTrace();
  32. }
  33. return result;
  34. }
  35. private static String signWithRSAJdk8(String source, String privateKey, String algorithm) {
  36. String result = null;
  37. try {
  38. PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
  39. KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA);
  40. PrivateKey pk = keyFactory.generatePrivate(spec);
  41. Signature signature = Signature.getInstance(algorithm);
  42. signature.initSign(pk);
  43. signature.update(source.getBytes(CHARSET_UTF_8));
  44. result = Base64.getEncoder().encodeToString(signature.sign());
  45. System.out.println("jdk8解码签名:"+result);
  46. } catch (Exception e) {
  47. System.out.println("RSA 签名出错!");
  48. e.printStackTrace();
  49. }
  50. return result;
  51. }
  52. public static void main(String[] args) throws Exception {
  53. String source = "app_id=89be0bb80a7a4e219b4011168c478f0c&biz_content={\"user_id\":\"17681865480\",\"idcard\":\"09612707419874225X\",\"name\":\"张三\",\"mobile\":\"17681865480\"}&format=JSON&method=moxie.api.risk.magicwand3.enhance-multi-info&sign_type=RSA&timestamp=1567593061041&version=1.0";
  54. String privatekey= "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDjAKaU7DbXOMoa63pF1ArcyMPWmMxmmfpBlewFHDnqFee5Gm3XdLR+fvGj+ZwddEx27ZJkkjVoXmk1/NqT+9nFXvXSctfHu6OsmYaWguCCHDjMLzuWKhuTtFohFUJQLIyfuVImzGKPv1WMoPsjouz1trTXHwpF5oj0mqI1B0HZDBsb55Dyl3DkfIaZBkr7s0tuJ/LBIhengMp2gBc9d61Qr1S6mSAv1Hj6IrL34gkZv0U0+bdDO227/r14KS4MTaHudYM7fb43f0+VEcBXUKnQgR+f6yCq67JJyPaPQYtvrf+J6azrr1+hlX6FFGWNYwaKRblgg+b5ICw5hrR2H97LAgMBAAECggEAPH565isA/mAC9COWxS8Z6faSwgicSCd2mq3SZRY9lNVbmGejqAxQ4XkA+lrVsL/CdNsg7S5Nj0/BqAKHWXJ/eWqyBfBW8sJdfuy3AjtEi0fsFvXrYjw8dyNT9YcN0gHdQ9+GNhK3nWk26jAoXTnY/i4/iSibez9RdiYTXi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RSAJdk7(source, privatekey, ALGORITHM_SHA1_WITH_RSA);
  56. String sign8 = signWithRSAJdk8(source, privatekey, ALGORITHM_SHA1_WITH_RSA);
  57. // System.out.println(msg);
  58. System.out.println("--------------");
  59. System.out.println(sign7.equals(sign8));
  60. }
  61. }
  • 运行结果:
  1. jdk7解码签名换行符去掉前:QXEvl/xDLanJ6GfB+eeZbawiKZwF3m1HzUr6r0GIk7DtWzI1GlOLt2au5aeqiIREwEi2sErRU/gU
  2. HCJFH1h1SfhADsw1i0xEC3C9lBU+SQCy8asEYZKnLMEsodVB2XhpBo0VkbR2dQad06kfPZoQ8qD5
  3. jTs/3xVJswATiGIVw7tGZaTO0VUwTt2v091MRMq3L80dg765QH2JMc/VzVoVCbYzcElzdwi9tNme
  4. zkzqtRnWIwCkc6gwfTsr+3mh4zIfyeB78/vTfdY84tYxh0Lxy1PXKP4SBUktaGUvuKDDsnycpoMK
  5. hCJU/WgCsmBe4Aj6+4IbnK2gUSMBOba06Cm6cA==
  6. jdk7解码签名换行符去掉后:QXEvl/xDLanJ6GfB+eeZbawiKZwF3m1HzUr6r0GIk7DtWzI1GlOLt2au5aeqiIREwEi2sErRU/gUHCJFH1h1SfhADsw1i0xEC3C9lBU+SQCy8asEYZKnLMEsodVB2XhpBo0VkbR2dQad06kfPZoQ8qD5jTs/3xVJswATiGIVw7tGZaTO0VUwTt2v091MRMq3L80dg765QH2JMc/VzVoVCbYzcElzdwi9tNmezkzqtRnWIwCkc6gwfTsr+3mh4zIfyeB78/vTfdY84tYxh0Lxy1PXKP4SBUktaGUvuKDDsnycpoMKhCJU/WgCsmBe4Aj6+4IbnK2gUSMBOba06Cm6cA==
  7. jdk8解码签名:QXEvl/xDLanJ6GfB+eeZbawiKZwF3m1HzUr6r0GIk7DtWzI1GlOLt2au5aeqiIREwEi2sErRU/gUHCJFH1h1SfhADsw1i0xEC3C9lBU+SQCy8asEYZKnLMEsodVB2XhpBo0VkbR2dQad06kfPZoQ8qD5jTs/3xVJswATiGIVw7tGZaTO0VUwTt2v091MRMq3L80dg765QH2JMc/VzVoVCbYzcElzdwi9tNmezkzqtRnWIwCkc6gwfTsr+3mh4zIfyeB78/vTfdY84tYxh0Lxy1PXKP4SBUktaGUvuKDDsnycpoMKhCJU/WgCsmBe4Aj6+4IbnK2gUSMBOba06Cm6cA==
  8. --------------
  9. true

结论

  • JDK1.7 编解码
  1. 编码
  2. new sun.misc.BASE64Encoder().encode(byte[] array)
  3. 解码
  4. new BASE64Decoder().decodeBuffer(String str)
  • JDK1.8 编解码
  1. 编码
  2. Base64.getEncoder().encodeToString(byte[] array)
  3. 解码
  4. Base64.getDecoder().decode(String str)
  • 注意换行这个坑
  • 1.8 变单例,线程安全提升了

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号