3

我有以下代码:

BigInteger d = new BigInteger(1,NFCUtil.hexStringToByteArray(IssPrivKey));
BigInteger n = new BigInteger(1,NFCUtil.hexStringToByteArray(IssPubKeyMod));
BigInteger X = new BigInteger(1,NFCUtil.hexStringToByteArray(unsignedPhoneCert));

BigInteger iccCertBI = X.modPow(d, n);

final String iccCert = iccCertBI.toString(16);

System.out.println("\nSigned Certificate: (int)" + iccCertBI +
            "\nSigned Certificate (hex): "+iccCert + 
            "\nsigned Cert length: "+iccCert.length());

我正在做的是使用私有指数和公共模数对字符串进行签名。所有长度均为 288 个字符或 144 个字节。踢球者是当我将它用于以下字符串时:

//Length 144
    final String unsignedTerCert="6A04846983677983FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF121" +
            "3AFAFAF01018001BDB011D89730D7958DCAF0D34A4668A65073F2766BAC3C3E7D551211A" +
            "1D7896085F1F4FDAA9A256579B0D4D6B1C7CF6C88D318886C55FB3CA91DB721376797058" +
            "F482A51A363B92A89DA81BB6A7FFBB6F8C8E109E5909EFFFFFFFFFFFFFFFFFFFFFFFFFFF" +
            "FFFFFFFFFFFFFBC";

正如我所期望的那样,它返回了一个长度为 288 个字符或 144 个字节的字符串。

但是将其用于:

//Length 144
    final String unsignedPhoneCert="6A0400112233445566778899011401020301018001AE0A3CCBF6B1A997E4A4AEC7EDA6697F6B73C2EC18F8E3403F83237BD9863B4CD0BE0EBC2A8E3FB140251935F1D4EAEF2D7801FFC2CF36328E234676F96883BAB6547F82FE02455B59B6952231B130EE70980B74456168AD2A511A00448BFEA62593DCF9F3D9FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC";

给我一个长度为 287 个字符的字符串。

所以我的问题是,BigInteger toString(16) 方法中是否存在导致这种情况的原因?

注意。我仍然可以使用公钥 expo 从 BigInteger 很好地解密它们,这就是让我认为这与 .toString() 方法有关的原因。

任何帮助/提示将不胜感激,谢谢。

编辑:这是我从不正确的输出中得到的输出:

Expo:36652731434641292350412815683229009979770975413220344544898857375896901092978000426765621805499026545042319009684553729382562239973283276192079521889464379604341193094271888918244826201023503324034315850394178807890994288745747556760470026279678747937975742329807945656583328292494373708777822442236249197245972251059241540578219194613740104635899
X:25332900759103924912689760614898214526937827890939506310067994686010108648336498000719255096072122266248508522922053911850606613923138698430079380374971383359222723175609119386369637533489684435824557964231714784677160255389973258884831385632708921103814163273457386881057406862373463121304854072959526597286211022281774584184894225094204257730492
X (hex):6a0400112233445566778899011401020301018001ae0a3ccbf6b1a997e4a4aec7eda6697f6b73c2ec18f8e3403f83237bd9863b4cd0be0ebc2a8e3fb140251935f1d4eaef2d7801ffc2cf36328e234676f96883bab6547f82fe02455b59b6952231b130ee70980b74456168ad2a511a00448bfea62593dcf9f3d9ffffffffffffffffffffffffffffffffffffffffbc

    Phone Signed Certificate: (int)2621041784071301050954845366555843081386290833242762939438653553587304361236513516037765146640763105648748143029523335470875071401132351534501879861341081099809142476414060273707263492268408647346182500147665413648376803840594353667155297685676942776395991299461798048817170064783101287983256626056086159016336001344190438106860395808579588320414
    Phone Signed Certificate (hex): af802ec9b412c2e223e28cd0e540255b18351d570e24690b1b441f6e084afb3a03a6a1a52ec4268d9dacf47385222011a977994c9dc521dc4ca82f20f4345ee8697a1cc269931211640efe7b6d29c728283aa823525887f09dbc6df37f3e1a6ec4718ccbb31778d813e4375d89631503dc785c9c9eb4770e918a3cc37c90b6d75135d8c32c299d8096d4bd88199ec9e
     Phone signed Cert length: 287

从正确的一个:

Expo:36652731434641292350412815683229009979770975413220344544898857375896901092978000426765621805499026545042319009684553729382562239973283276192079521889464379604341193094271888918244826201023503324034315850394178807890994288745747556760470026279678747937975742329807945656583328292494373708777822442236249197245972251059241540578219194613740104635899
X:25333383310317922886770001828148668452338535025796085964448660925847832365078972818630528414968514941209307982589233259227108935074767908506952878286644207041412726931112453402165128539381501316868062159358826581046954850027043009445846574678562969939333097216245548333577227051225788418683968135213089604118672442657593365304099582656739524214716
X (hex):6a04846983677983ffffffffffffffffffffffffffffffffffffff1213afafaf01018001bdb011d89730d7958dcaf0d34a4668a65073f2766bac3c3e7d551211a1d7896085f1f4fdaa9a256579b0d4d6b1c7cf6c88d318886c55fb3ca91db721376797058f482a51a363b92a89da81bb6a7ffbb6f8c8e109e5909effffffffffffffffffffffffffffffffffffffffbc

Phone Signed Certificate: (int)21432015836175396598983285831512248305709761145636099501338782160749145447638782142594889169355603470185237110648036474961995187557563452989823081731645192439732733979910407327875747461117018365485531108584396064685039171356192052384601156809092826847482291755881176360680132279291900385137689881779233791387498805975645418591098518000313085841654
Phone Signed Certificate (hex): 59b0d96873fa0895d52d3f63475dd224c0a73bc34439e369a7b5eea88fd65b52464317b88642016421e9054d7e19dfc9cac7ffda07faea496268ea133cd83dc45959de61b1aa4b26a46ab47c6b8f4f2c092e914f0d940e9082b800119eba061bf7a654c022bc9c601a2d02a9775fb2cd23e532146443d7236c2ab301478fc9c59bbd984792d95d1db1a8d2ce80add0f6
 Phone signed Cert length: 288
4

1 回答 1

5

BigInteger.toString(int)不会用前导零填充其输出。如果您想要固定宽度的输出,请尝试String.format

String.format("%0288x", iccCertBI)

添加

格式字符串分解如下:

  • %标记格式说明符的开头,它告诉格式化程序将相应的参数放在输出中的这个位置。
  • 0告诉格式化程序使用零填充;没有这个,它使用空格进行填充。
  • 288告诉格式化程序在输出中至少生成 288 个字符(不确定是否可以指定最大值)
  • x告诉格式化程序将相应的参数转换为十六进制整数(任何数字基元或类型,例如int, long. 或BigInteger,都可以在这里使用)。

String.format是用于将常见数据类型格式化为字符串的强大工具,您绝对应该阅读Javadoc

于 2012-04-11T19:25:05.863 回答