• java私钥加密——SHA256withRSA


    因OA系统需要对exchange的邮箱服务器做单点登录,开发商(微软中国代理商)提供了一套.Net开发的单点登录方案。平时公司为了节省成本,这种单点登录的工作一般都是我们自己开发完成,而我这边OA系统(另一家开发商提供的)是Java开发的,虽然加密这类的标准是一样的,可是没有参考代码,只能盲敲调试,且OA系统还得在jsp中写Java代码,实在是有点折磨(全程nodepad++写代码,编译错误只能在上传到服务器运行时才能发现)。

    以下是开发商提供的.Net加密代码:

    1. using System;
    2. using System.Security.Cryptography;
    3. namespace SignWithRSASHA256
    4. {
    5. public partial class TestForm : Form
    6. {
    7. public TestForm()
    8. {
    9. InitializeComponent();
    10. }
    11. private void signTest(object sender, EventArgs e)
    12. {
    13. // 123是私钥文件的密码
    14. X509Certificate2 cert = new X509Certificate2(@"cert\\ifcloud.pfx", "123");
    15. byte[] srcDataBuf = System.Text.Encoding.UTF8.GetBytes("待加密的字符串");
    16. RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)cert.PrivateKey;
    17. byte[] signedDataBuf = RSA.SignData(srcDataBuf, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    18. // base64SrcData
    19. Console.WriteLine(HttpUtility.UrlEncode(Convert.ToBase64String(srcDataBuf)));
    20. // signedData
    21. Console.WriteLine(HttpUtility.UrlEncode(Convert.ToBase64String(signedDataBuf)));
    22. }
    23. }
    24. }

    我要做的事情就是用Java给它实现一下。

    看着这段代码不多,感觉用Java写应该也不难。但主要是对加密这块了解得太少,而且也怕1.6的jdk会有不支持的情况。

    抱着学习的心态,我先去微软官方的文档查了下这段代码里面涉及到的几个类和它们的方法。

    X509Certificate2 类 (System.Security.Cryptography.X509Certificates) | Microsoft Docs

    X509Certificate2.PrivateKey 属性 (System.Security.Cryptography.X509Certificates) | Microsoft Docs

    RSACryptoServiceProvider 类 (System.Security.Cryptography) | Microsoft Docs

    RSA.SignData 方法 (System.Security.Cryptography) | Microsoft Docs

    RSASignaturePadding.Pkcs1 属性 (System.Security.Cryptography) | Microsoft Docs

    看完之后,模模糊糊,X509Certificate2的构造可以获取PrivateKey(私钥),最后一个SignData是进行加密签名,细节还是一无所知。

    第一个想法是看下java有没有相同的类名,想着同为高级语言,类名总有相似之处吧?结果还真就没有。。。

    然后就开始了艰辛的百度过程。分步走,第一步先实现获取私钥的功能,第二步再加密。

    • java读取PFX

    查到了这篇博客

    java中PFX与CER的使用(上)_楓楓楓楓楓的博客-CSDN博客_java pfx

    可以说是写得非常好的一篇博文了,代码和原理都介绍得很通透,可能是觉得这篇博客就是标准答案了,然后我就发现它里面有个rsaByPrivateKey方法,要么不做,要么做全套。于是我入坑了。贴出入坑代码:

    1. String OWA_data = "待加密字符串";
    2. PrivateKey privateKey = null;
    3. FileInputStream fileInputStream = null;
    4. try {
    5. fileInputStream = new FileInputStream("/usr/mailcert/ifcloud.pfx");
    6. byte[] pfxBytes = new byte[fileInputStream.available()];
    7. fileInputStream.read(pfxBytes);
    8. KeyStore ks = KeyStore.getInstance("PKCS12");
    9. char[] charPriKeyPass = "123".toCharArray();
    10. ks.load(new ByteArrayInputStream(pfxBytes), charPriKeyPass);
    11. Enumeration aliasEnum = ks.aliases();
    12. String keyAlias = "";
    13. if (aliasEnum.hasMoreElements()) {
    14. keyAlias = aliasEnum.nextElement();
    15. }
    16. privateKey = (PrivateKey) ks.getKey(keyAlias, charPriKeyPass);
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. } finally {
    20. try {
    21. if (fileInputStream != null) {
    22. fileInputStream.close();
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. out.println("######" + privateKey);
    29. String sign_base64 = "";
    30. try {
    31. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    32. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    33. byte[] srcData = OWA_data.getBytes("UTF-8");
    34. int blockSize = cipher.getOutputSize(srcData.length)-11;
    35. byte[] encryptData = null;
    36. for (int i = 0; i < srcData.length; i += blockSize) {
    37. byte[] subarray = new byte[0];
    38. if(srcData != null) {
    39. int endIndexExclusive = i + blockSize;
    40. if (endIndexExclusive > srcData.length) {
    41. endIndexExclusive = srcData.length;
    42. }
    43. int newSize = endIndexExclusive - i;
    44. if (newSize <= 0) {
    45. continue;
    46. }
    47. subarray = new byte[newSize];
    48. System.arraycopy(srcData, i, subarray, 0, newSize);
    49. }
    50. byte[] doFinal = cipher.doFinal(subarray);
    51. if(encryptData == null) {
    52. encryptData = new byte[doFinal.length];
    53. System.arraycopy(doFinal, 0, encryptData, 0, doFinal.length);
    54. } else {
    55. encryptData = new byte[encryptData.length + doFinal.length];
    56. System.arraycopy(doFinal, 0, encryptData, encryptData.length, doFinal.length);
    57. }
    58. }
    59. sign_base64 = new String(Base64.encode(encryptData));
    60. out.println(sign_base64);
    61. } catch (Exception e) {
    62. e.printStackTrace();
    63. out.println(e);
    64. }

    因为是jsp里面写的(供应商的系统框架太老,且不提供源码),只能改改jsp,所以调试只能靠打印(out.println)。

    一运行,加密结果果然不出意外地对不上。

    然后我发现,我没有使用SHA256,于是我就改了下最后几行代码:

    1. String result = new SHA256(new String(encryptData)).getHash();
    2. sign_base64 = new String(Base64.encode(result.getBytes("UTF-8")));

    还是不对,想想也自然,完全没摸明白,想碰运气把代码写出来,不是那么简单。

    但其实此时我已经通过读取PFX文件把私钥获取了,只是因为加密结果对不上,无法确认获取的私钥是否也是正确的(事后证明是正确的

    • SHA256withRSA

    继续找资料,然后查到了这个博客:SHA256withRSA (RSA2) 简单介绍_极客on之路的博客-CSDN博客_sha256withrsa

    里面有一句:

    SHA1WithRSA:用SHA算法进行签名,用RSA算法进行加密。同理 SHA256withRSA (RSA2)  使用SHA256签名。

    我”顿悟“了,原来我加密和签名的顺序搞反了!!!(事后来看,这句话给我造成了太多误解

    再次改完代码,一运行,哎嘿,还是错的。。。

    此时我已经怀疑是不是.Net的代码有问题?(程序员通病,如果调试一直有问题,那可能是别人的代码有问题)

    因为连续的错误,加上不明白原理,且每次改的是jsp,自然而然会产生一种程序健壮性太差的感觉,此时也极容易否定前面所有写出的代码。

    后来想想,主要是微软的这段文档,写得太简略了:

     使用指定的哈希算法和填充模式计算指定字节的哈希值,并对生产的哈希值进行签名。(这谁翻译的?绕口不说,还说了等于没说

    直到我看到这篇博客:常见签名算法之SHA256withRSA_马拉萨的春天的博客-CSDN博客_sha256withrsa签名算法

    1. public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
    2. public static final String ENCODE_ALGORITHM = "SHA-256";
    3. public static final String PLAIN_TEXT = "test string";
    4. /**
    5. * 签名
    6. * @param privateKey 私钥
    7. * @param plainText 明文
    8. * @return
    9. */
    10. public static byte[] sign(PrivateKey privateKey, String plainText) {
    11. MessageDigest messageDigest;
    12. byte[] signed = null;
    13. try {
    14. messageDigest = MessageDigest.getInstance(ENCODE_ALGORITHM);
    15. messageDigest.update(plainText.getBytes());
    16. byte[] outputDigest_sign = messageDigest.digest();
    17. System.out.println("SHA-256编码后-----》"
    18. + org.apache.commons.codec.binary.Base64.encodeBase64String(outputDigest_sign));
    19. Signature Sign = Signature.getInstance(SIGNATURE_ALGORITHM);
    20. Sign.initSign(privateKey);
    21. Sign.update(outputDigest_sign);
    22. signed = Sign.sign();
    23. System.out.println("SHA256withRSA签名后-----》"
    24. + org.apache.commons.codec.binary.Base64.encodeBase64String(signed));
    25. } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
    26. e.printStackTrace();
    27. }
    28. return signed;
    29. }

    这段代码,终于解决了我所有问题。

    jdk文档:

    MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。

    Signature 类用来为应用程序提供数字签名算法功能。数字签名用于确保数字数据的验证和完整性。

    在所有算法当中,数字签名可以是 NIST 标准的 DSA,它使用 DSA 和 SHA-1。可以将使用 SHA-1 消息摘要算法的 DSA 算法指定为 SHA1withDSA。如果使用 RSA,对消息摘要算法则会有多种选择,因此,可以将签名算法指定为 MD2withRSA、MD5withRSA 或 SHA1withRSA。因为没有默认的算法名称,所以必须为其指定名称。

    Signature 对象可用来生成和验证数字签名。

    只怪之前没看到,修改我自己的代码如下:

    1. String OWA_data = "待加密字符串";
    2. PrivateKey privateKey = null;
    3. FileInputStream fileInputStream = null;
    4. try {
    5. fileInputStream = new FileInputStream("/usr/mailcert/ifcloud.pfx");
    6. byte[] pfxBytes = new byte[fileInputStream.available()];
    7. fileInputStream.read(pfxBytes);
    8. KeyStore ks = KeyStore.getInstance("PKCS12");
    9. char[] charPriKeyPass = "123".toCharArray();
    10. ks.load(new ByteArrayInputStream(pfxBytes), charPriKeyPass);
    11. Enumeration aliasEnum = ks.aliases();
    12. String keyAlias = "";
    13. if (aliasEnum.hasMoreElements()) {
    14. keyAlias = aliasEnum.nextElement();
    15. }
    16. privateKey = (PrivateKey) ks.getKey(keyAlias, charPriKeyPass);
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. } finally {
    20. try {
    21. if (fileInputStream != null) {
    22. fileInputStream.close();
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. String sign_base64 = "";
    29. try {
    30. byte[] OWA_data_Buf = OWA_data.getBytes("UTF-8");
    31. Signature Sign = Signature.getInstance("SHA256withRSA");
    32. Sign.initSign(privateKey);
    33. Sign.update(OWA_data_Buf);
    34. sign_base64 = new String(Base64.encode(Sign.sign()));
    35. out.println(sign_base64);
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }

    perfect!!!

  • 相关阅读:
    Flutter 全能型选手GetX —— 状态管理
    C++ 内存模型
    el-table合并单元格-会超出表格内容问题
    UseGalaxy.cn生信云|生物信息必备技能-出版级别的circos圈图绘制
    在javascript中将字符串转换为数字的6种方法
    跟着腾讯T4学架构:微服务+MySQL+Nginx+Redis+容器化+虚拟机
    Spring Authorization Server OAuth 2.0获取授权码流程
    sqlmap使用图解
    国外访问学者申请政策解析
    自主品牌首次「赶超」合资,中国供应商「突围」智能电动赛道
  • 原文地址:https://blog.csdn.net/qingzhukl/article/details/126033207