本文介绍了RSASSA_PKCS1v15_SHA_Signer和PK_SignatureScheme :: KeyTooShort异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我第一次使用Crypto ++,我有一些麻烦。为什么这在C#(使用相同的键),但不是与Crypto ++工作?我将在下面显示我的C#和C ++代码。 C#代码(this works!): byte [] Modulus = new byte [] {0xA3,0x1D,0x6C,0xE5,0xFA,0x95,0xFD,0xE8,0x90,0x21,0xFA,0xD1,0x0C, 0x64,0x19,0x2B,0x86,0x58,0x9B,0x17,0x2B,0x10,0x05,0xB8,0xD1,0xF8,0x4C,0xEF,0x53,0x4C,0xD5,0x4E,0x5C,0xAE,0x86,0xEF,0x92,0x7B, 0x54,0xE0,0x54,0x55,0x9E,0xE0,0xE7,0xBE,0xFA,0x3F,0x9E,0x15,0x6F,0x6C,0x38,0x4E,0xAF,0x07,0x0C,0x61,0xAB, 0x51,0x5E,0x23,0x53,0x14,0x18,0x88,0xCB,0x6F,0xCB,0xC5,0xD6,0x30,0xF4,0x06,0xED,0x24,0x23,0xEF,0x25,0x6D,0x00,0x91,0x77,0x24, 0x9B,0xE5,0xA3,0xC0,0x27,0x90,0xC2,0x97,0xF7,0x74,0x9D,0x6F,0x17,0x83,0x7E,0xB5,0x37,0xDE,0x51,0xE8,0xD7,0x1C,0xE1,0x56,0xD9, 0x8,0xC8,0xC3,0xC3,0x20,0x9D,0x64,0xC3,0x2F,0x8C,0x91,0x92,0x30,0x6F,0xDB}。 byte [] Exponent = new byte [] {0x00,0x01,0x00,0x01}; byte [] P = new byte [] {0xCC,0xE7,0x5D,0xFE,0x72,0xB6,0xFD,0xE7,0x1D,0xE3,0x1A,0x0E,0xAC,0x33,0x7A,0xB9,0x21,0xE8, 0x8A,0x84,0x9B,0xDA,0x9F,0x1E,0x58,0x34,0x68,0x7A,0xB1,0x1D,0x7E,0x1C,0x18,0x52,0x65,0x7B,0x97,0x8E,0xA7,0x6A,0x9D,0xEE,0x5A, 0x77,0x52,0x3B,0x71,0x8F,0x33,0xD0,0x49,0x5E,0xC3,0x30,0x39,0x72,0x36,0xBF,0x1D,0xD9,0xF2,0x24,0xE8,0x71}。 byte [] Q = new byte [] {0xCB,0xCA,0x58,0x74,0xD4,0x03,0x62,0x93,0x06,0x50,0x1F,0x42,0xF6,0xAA,0x59,0x36,0xA7,0xA1, 0xF3,0x97,0x5C,0x9A,0xC8,0x6A,0x27,0xCF,0x85,0x05,0x2A,0x66,0x41,0x6A,0x7F,0x2F,0x84,0xC8,0x18,0x13,0xC6,0x1D,0x8D,0xC7,0x32, 0x2F,0x72,0x19,0x3F,0xA4,0xED,0x71,0xE7,0x61,0xC0,0xCF,0x61,0xAE,0x8B,0xA0,0x68,0xA7,0x7D,0x83,0x23,0x0B}; byte [] DP = new byte [] {0x4C,0xCA,0x74,0xE6,0x74,0x35,0x72,0x48,0x58,0x62,0x11,0x14,0xE8,0xA2,0x4E,0x5E,0xED,0x7F, 0x49,0xD2,0x52,0xDA,0x87,0x01,0x87,0x4A,0xF4,0xD0,0xEE,0x69,0xC0,0x26,0x65,0x53,0x13,0xE7,0x52,0xB0,0x4A,0xBB,0xE1,0x3E,0x3F, 0xB7,0x32,0x21,0x46,0xF8,0xC5,0x11,0x4D,0x3D,0xEF,0x66,0xB6,0x50,0xC0,0x85,0xB5,0x79,0x45,0x8F,0x61,0x71}; byte [] InverseQ = new byte [] {0x28,0x6A,0xBB,0xD1,0x93,0x95,0x94,0x1A,0x6E,0xED,0xD7,0x0E,0xC0,0x61,0x2B,0xC2,0xEF,0xE1, 0x86,0x3D,0x34,0x12,0x88,0x6F,0x94,0xA4,0x48,0x6E,0xC9,0x87,0x1E,0x46,0x00,0x46,0x00,0x52,0x8E,0x9F,0x47,0xC0,0x8C,0xAB,0xBC, 0x49,0xAC,0x5B,0x13,0xF2,0xEC,0x27,0x8D,0x1B,0x6E,0x51,0x06,0xA6,0xF1,0x62,0x1A,0xEB,0x78,0x2E,0x88,0x48}。 byte [] D = new byte [] {0x9B,0xF9,0xDE,0xC2,0x45,0x93,0x4C,0x4C,0xAC,0x48,0x2B,0xA8,0x4D,0xFC,0xD7,0xED,0xB2,0xFB, 0x72,0xE9,0xEA,0xC1,0x88,0x39,0x07,0x2A,0x6F,0x34,0x07,0x81,0x97,0x7E,0xCD,0xFA,0x21,0x02,0xF5,0xDD,0x30,0xDD,0x22,0x4A,0xB3, 0x41,0xE5,0x89,0x80,0x73,0xC4,0xAF,0x90,0x9E,0x2B,0x50,0x8A,0x0A,0xD4,0x6E,0xBD,0x0F,0x15,0x79,0x37,0x95,0xE8,0x3D,0xCF,0x4C, 0x6D,0xFF,0x51,0x65,0xE7,0x90,0xC1,0xAC,0x2D,0xC6,0xEB,0x47,0x19,0x2D,0xD0,0x58,0x74,0x79,0xAC,0x08,0x1C,0xA3,0x1D,0xD0,0xCE, 0x39,0x2E,0xC3,0xFA,0x66,0xEF,0xC7,0x8E,0x10,0x2F,0xE4,0xA1,0xE7,0x4E,0xA8,0x42,0xF0,0xF4,0xFD,0x10,0xA6,0x67,0x64,0xCB,0x3A, 0x6D,0x4D,0x51,0xEC,0x1F,0x9D,0x56,0x26,0xC2,0xFC}; byte [] DQ = new byte [] {0xAF,0xDC,0x46,0xE7,0x52,0x8A,0x35,0x47,0xA1,0x1C,0x05,0x4E,0x39,0x24,0x99,0xE6,0x43,0x54, 0xCB,0xAB,0xE3,0xDB,0x22,0x76,0x11,0x32,0xD0,0x9C,0xBB,0x91,0x10,0x84,0x81,0x8B,0x15,0x2F,0xC3,0x2F,0x55,0x38,0xED,0xBF,0x67, 0x3C,0x70,0x5E,0xFF,0x80,0x28,0xF3,0xB1,0x73,0xB6,0xFA,0x7F,0x56,0x2B,0xE1,0xDA,0x4E,0x27,0x4E,0xC2,0x2F}。 RSAParameters rsaParams = new RSAParameters(); rsaParams.Modulus = Modulus; rsaParams.Exponent =指数; rsaParams.P = P; rsaParams.Q = Q; rsaParams.DP = DP; rsaParams.InverseQ = InverseQ; rsaParams.D = D; rsaParams.DQ = DQ; RSACryptoServiceProvider crypt = new RSACryptoServiceProvider(); crypt.ImportParameters(rsaParams); RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(); formatter.SetHashAlgorithm(SHA1); formatter.SetKey(crypt); byte [] dataFile = new byte [] {0x6F,0x9F,0x07,0x04,0xE2,0x1A,0xF7,0xB8,0xB2,0x4F,0x8D,0x66,0x49,0xA1,0x09,0xA7, 0xB2,0x22,0x3C,0xF9}; byte [] signature = formatter.CreateSignature(dataFile); 现在,我的C ++代码不起作用: const char ModulusCON [0x80] = {0xA3,0x1D,0x6C,0xE5,0xFA,0x95,0xFD,0xE8,0x90,0x21,0xFA,0xD1,0x0C,0x64 ,0x19,0x2B,0x86,0x58,0x9B,0x17,0x2B,0x10,0x05,0xB8,0xD1,0xF8,0x4C,0xEF,0x53,0x4C,0xD5,0x4E,0x5C,0xAE,0x86,0xEF,0x92,0x7B,0x90 ,0xD1,0xE0,0x62,0xFD,0x7C,0x54,0x55,0x9E,0xE0,0xE7,0xBE,0xFA,0x3F,0x9E,0x15,0x6F,0x6C,0x38,0x4E,0xAF,0x07,0x0C,0x61,0xAB,0x51 ,0x5E,0x23,0x53,0x14,0x18,0x88,0xCB,0x6F,0xCB,0xC5,0xD6,0x30,0xF4,0x06,0xED,0x24,0x23,0xEF,0x25,0x6D,0x00,0x91,0x77,0x24,0x9B ,0xE5,0xA3,0xC0,0x27,0x90,0xC2,0x97,0xF7,0x74,0x9D,0x6F,0x17,0x83,0x7E,0xB5,0x37,0xDE,0x51,0xE8,0xD7,0x1C,0xE1,0x56,0xD9,0x56 ,0xC8,0xC3,0xC3,0x20,0x9D,0x64,0xC3,0x2F,0x8C,0x91,0x92,0x30,0x6F,0xDB}; const char ExponentCON [0x4] = {0x00,0x01,0x00,0x01}; const char PCON [0x40] = {0xCC,0xE7,0x5D,0xFE,0x72,0xB6,0xFD,0xE7,0x1D,0xE3,0x1A,0x0E,0xAC,0x33,0x7A,0xB9,0x21,0xE8,0x8A, 0x84,0x9B,0xDA,0x9F,0x1E,0x58,0x34,0x68,0x7A,0xB1,0x1D,0x7E,0x1C,0x18,0x52,0x65,0x7B,0x97,0x8E,0xA7,0x6A,0x9D,0xEE,0x5A,0x77, 0x52,0x3B,0x71,0x8F,0x33,0xD0,0x49,0x5E,0xC3,0x30,0x39,0x72,0x36,0xBF,0x1D,0xD9,0xF2,0x24,0xE8,0x71}; const char QCON [0x40] = {0xCB,0xCA,0x58,0x74,0xD4,0x03,0x62,0x93,0x06,0x50,0x1F,0x42,0xF6,0xAA,0x59,0x36,0xA7,0xA1,0xF3, 0x7A,0xC8,0x6A,0x27,0xCF,0x85,0x05,0x2A,0x66,0x41,0x6A,0x7F,0x2F,0x84,0xC8,0x18,0x13,0xC6,0x1D,0x8D,0xC7,0x32,0x2F, 0x72,0x19,0x3F,0xA4,0xED,0x71,0xE7,0x61,0xC0,0xCF,0x61,0xAE,0x8B,0xA0,0x68,0xA7,0x7D,0x83,0x23,0x0B}。 const char DPCON [0x40] = {0x4C,0xCA,0x74,0xE6,0x74,0x35,0x72,0x48,0x58,0x62,0x11,0x14,0xE8,0xA2,0x4E,0x5E,0xED,0x7F,0x49, 0xD2,0x52,0xDA,0x87,0x01,0x87,0x4A,0xF4,0xD0,0xEE,0x69,0xC0,0x26,0x65,0x53,0x13,0xE7,0x52,0xB0,0x4A,0xBB,0xE1,0x3E,0x3F,0xB7, 0x32,0x21,0x46,0xF8,0xC5,0x11,0x4D,0x3D,0xEF,0x66,0xB6,0x50,0xC0,0x85,0xB5,0x79,0x45,0x8F,0x61,0x71}。 const char InverseQCON [0x40] = {0x28,0x6A,0xBB,0xD1,0x93,0x95,0x94,0x1A,0x6E,0xED,0xD7,0x0E,0xC0,0x61,0x2B,0xC2,0xEF,0xE1,0x86, 0x3D,0x34,0x12,0x88,0x6F,0x94,0xA4,0x48,0x6E,0xC9,0x87,0x1E,0x46,0x00,0x46,0x00,0x52,0x8E,0x9F,0x47,0xC0,0x8C,0xAB,0xBC,0x49, 0xAC,0x5B,0x13,0xF2,0xEC,0x27,0x8D,0x1B,0x6E,0x51,0x06,0xA6,0xF1,0x62,0x1A,0xEB,0x78,0x2E,0x88,0x48} const char DCON [0x80] = {0x9B,0xF9,0xDE,0xC2,0x45,0x93,0x4C,0x4C,0xAC,0x48,0x2B,0xA8,0x4D,0xFC,0xD7,0xED,0xB2,0xFB,0x72, 0xE9,0xEA,0xC1,0x88,0x39,0x07,0x2A,0x6F,0x34,0x07,0x81,0x97,0x7E,0xCD,0xFA,0x21,0x02,0xF5,0xDD,0x30,0xDD,0x22,0x4A,0xB3,0x41, 0x7A,0x0A,0xD4,0x6E,0xBD,0x0F,0x15,0x79,0x37,0x95,0xE8,0x3D,0xCF,0x4C,0x6D,0x0B,0xE5,0x89,0x80,0x73,0xC4,0xAF,0x90,0x9E, 0xFF,0x51,0x65,0xE7,0x90,0xC1,0xAC,0x2D,0xC6,0xEB,0x47,0x19,0x2D,0xD0,0x58,0x74,0x79,0xAC,0x08,0x1C,0xA3,0x1D,0xD0,0xCE,0x39, 0x4E,0xA8,0x42,0xF0,0xF4,0xFD,0x10,0xA6,0x67,0x64,0xCB,0x3A,0x6D,0x8E,0x8E,0x8E,0x8E, 0x4D,0x51,0xEC,0x1F,0x9D,0x56,0x26,0xC2,0xFC}; const char DQCON [0x40] = {0xAF,0xDC,0x46,0xE7,0x52,0x8A,0x35,0x47,0xA1,0x1C,0x05,0x4E,0x39,0x24,0x99,0xE6,0x43,0x54,0xCB, 0xAB,0xE3,0xDB,0x22,0x76,0x11,0x32,0xD0,0x9C,0xBB,0x91,0x10,0x84,0x81,0x8B,0x15,0x2F,0xC3,0x2F,0x55,0x38,0xED,0xBF,0x67,0x3C, 0x70,0x5E,0xFF,0x80,0x28,0xF3,0xB1,0x73,0xB6,0xFA,0x7F,0x56,0x2B,0xE1,0xDA,0x4E,0x27,0x4E,0xC2,0x2F}。 //设置参数 CryptoPP :: AutoSeededRandomPool rng; InvertibleRSAFunction params; 整数整数(ModulusCON); params.SetModulus(integ); Integer integ1(ExponentCON); params.SetPublicExponent(integ1); 整数integ2(PCON); params.SetPrime1(integ2); 整数integ3(QCON); params.SetPrime2(integ3); 整数integ4(DPCON); params.SetModPrime1PrivateExponent(integ4); Integer integ5(InverseQCON); params.SetMultiplicativeInverseOfPrime2ModPrime1(integ5); 整数integ6(DCON); params.SetPrivateExponent(integ6); 整数integ7(DQCON); params.SetModPrime2PrivateExponent(integ7); //创建键 RSA :: PrivateKey privateKey(params); RSA :: PublicKey publicKey(params); CryptoPP :: RSASSA_PKCS1v15_SHA_Signer签名者(privateKey); unsigned char data [20] = {0x6F,0x9F,0x07,0x04,0xE2,0x1A,0xF7,0xB8,0xB2,0x4F,0x8D,0x66,0x49,0xA1,0x09,0xA7,0xB2, 0x22,0x3C,0xF9}; BYTE * signature = new BYTE [0x80]; signer.SignMessage(rng,data,20,signature);基于我所知道的,Crypto ++的'RSASSA_PKCS1v15_SHA_Signer'是我想要的等效于C#的'RSAPKCS1SignatureFormatter'和'将散列算法设置为SHA1。 它引发的错误是:感谢任何帮助,Hetelek。解决方案我不能回答为什么这在C#,但对于Crypto ++,有几个问题。 首先,您可能调用了 Integer 。我想你希望这个从大端字节数组转换的数据: 整数(const byte * encodedInteger,size_t byteCount,Signedness s = UNSIGNED) ,但使用 this one 从基础2,8,10或16(在您的情况下是10)中的字符串转换。 因此,使用 ExponentCON 为例,您应该这样做: const int ExponentSize(0x4); const byte ExponentCON [ExponentSize] = {0x00,0x01,0x00,0x01}; Integer integ1(ExponentCON,ExponentSize); 或可能更好: std :: string ExponentCON(101h); // 整数integ1(ExponentCON.c_str()); InvertibleRSAFunction 的成员必须满足特定条件才能成为有效的键。您可以通过 InvertibleRSAFunction :: Validate 函数,我假设检查描述的条件 params.Validate(rng,3); //为您的输入数据返回false 有一些帮助函数被设计为避免必须明确地设置一切。我不知道这些是否适合您,但我指的是 InvertibleRSAFunction :: GenerateRandomWithKeySize 和重载的 InvertibleRSAFunction :: Initialize 。 Crypto ++ Wiki 介绍了使用这些函数与示例代码。 I am using Crypto++ for the first time, and I am having some trouble. Why does this work in C# (with the same keys), but not with Crypto++? I will show my C# and C++ code below.C# code (this works!):byte[] Modulus = new byte[] { 0xA3, 0x1D, 0x6C, 0xE5, 0xFA, 0x95, 0xFD, 0xE8, 0x90, 0x21, 0xFA, 0xD1, 0x0C, 0x64, 0x19, 0x2B, 0x86, 0x58, 0x9B, 0x17, 0x2B, 0x10, 0x05, 0xB8, 0xD1, 0xF8, 0x4C, 0xEF, 0x53, 0x4C, 0xD5, 0x4E, 0x5C, 0xAE, 0x86, 0xEF, 0x92, 0x7B, 0x90, 0xD1, 0xE0, 0x62, 0xFD, 0x7C, 0x54, 0x55, 0x9E, 0xE0, 0xE7, 0xBE, 0xFA, 0x3F, 0x9E, 0x15, 0x6F, 0x6C, 0x38, 0x4E, 0xAF, 0x07, 0x0C, 0x61, 0xAB, 0x51, 0x5E, 0x23, 0x53, 0x14, 0x18, 0x88, 0xCB, 0x6F, 0xCB, 0xC5, 0xD6, 0x30, 0xF4, 0x06, 0xED, 0x24, 0x23, 0xEF, 0x25, 0x6D, 0x00, 0x91, 0x77, 0x24, 0x9B, 0xE5, 0xA3, 0xC0, 0x27, 0x90, 0xC2, 0x97, 0xF7, 0x74, 0x9D, 0x6F, 0x17, 0x83, 0x7E, 0xB5, 0x37, 0xDE, 0x51, 0xE8, 0xD7, 0x1C, 0xE1, 0x56, 0xD9, 0x56, 0xC8, 0xC3, 0xC3, 0x20, 0x9D, 0x64, 0xC3, 0x2F, 0x8C, 0x91, 0x92, 0x30, 0x6F, 0xDB };byte[] Exponent = new byte[] { 0x00, 0x01, 0x00, 0x01 };byte[] P = new byte[] { 0xCC, 0xE7, 0x5D, 0xFE, 0x72, 0xB6, 0xFD, 0xE7, 0x1D, 0xE3, 0x1A, 0x0E, 0xAC, 0x33, 0x7A, 0xB9, 0x21, 0xE8, 0x8A, 0x84, 0x9B, 0xDA, 0x9F, 0x1E, 0x58, 0x34, 0x68, 0x7A, 0xB1, 0x1D, 0x7E, 0x1C, 0x18, 0x52, 0x65, 0x7B, 0x97, 0x8E, 0xA7, 0x6A, 0x9D, 0xEE, 0x5A, 0x77, 0x52, 0x3B, 0x71, 0x8F, 0x33, 0xD0, 0x49, 0x5E, 0xC3, 0x30, 0x39, 0x72, 0x36, 0xBF, 0x1D, 0xD9, 0xF2, 0x24, 0xE8, 0x71 };byte[] Q = new byte[] { 0xCB, 0xCA, 0x58, 0x74, 0xD4, 0x03, 0x62, 0x93, 0x06, 0x50, 0x1F, 0x42, 0xF6, 0xAA, 0x59, 0x36, 0xA7, 0xA1, 0xF3, 0x97, 0x5C, 0x9A, 0xC8, 0x6A, 0x27, 0xCF, 0x85, 0x05, 0x2A, 0x66, 0x41, 0x6A, 0x7F, 0x2F, 0x84, 0xC8, 0x18, 0x13, 0xC6, 0x1D, 0x8D, 0xC7, 0x32, 0x2F, 0x72, 0x19, 0x3F, 0xA4, 0xED, 0x71, 0xE7, 0x61, 0xC0, 0xCF, 0x61, 0xAE, 0x8B, 0xA0, 0x68, 0xA7, 0x7D, 0x83, 0x23, 0x0B };byte[] DP = new byte[] { 0x4C, 0xCA, 0x74, 0xE6, 0x74, 0x35, 0x72, 0x48, 0x58, 0x62, 0x11, 0x14, 0xE8, 0xA2, 0x4E, 0x5E, 0xED, 0x7F, 0x49, 0xD2, 0x52, 0xDA, 0x87, 0x01, 0x87, 0x4A, 0xF4, 0xD0, 0xEE, 0x69, 0xC0, 0x26, 0x65, 0x53, 0x13, 0xE7, 0x52, 0xB0, 0x4A, 0xBB, 0xE1, 0x3E, 0x3F, 0xB7, 0x32, 0x21, 0x46, 0xF8, 0xC5, 0x11, 0x4D, 0x3D, 0xEF, 0x66, 0xB6, 0x50, 0xC0, 0x85, 0xB5, 0x79, 0x45, 0x8F, 0x61, 0x71 };byte[] InverseQ = new byte[] { 0x28, 0x6A, 0xBB, 0xD1, 0x93, 0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xEC, 0x1F, 0x9D, 0x56, 0x26, 0xC2, 0xFC };byte[] DQ = new byte[] { 0xAF, 0xDC, 0x46, 0xE7, 0x52, 0x8A, 0x35, 0x47, 0xA1, 0x1C, 0x05, 0x4E, 0x39, 0x24, 0x99, 0xE6, 0x43, 0x54, 0xCB, 0xAB, 0xE3, 0xDB, 0x22, 0x76, 0x11, 0x32, 0xD0, 0x9C, 0xBB, 0x91, 0x10, 0x84, 0x81, 0x8B, 0x15, 0x2F, 0xC3, 0x2F, 0x55, 0x38, 0xED, 0xBF, 0x67, 0x3C, 0x70, 0x5E, 0xFF, 0x80, 0x28, 0xF3, 0xB1, 0x73, 0xB6, 0xFA, 0x7F, 0x56, 0x2B, 0xE1, 0xDA, 0x4E, 0x27, 0x4E, 0xC2, 0x2F };RSAParameters rsaParams = new RSAParameters();rsaParams.Modulus = Modulus;rsaParams.Exponent = Exponent;rsaParams.P = P;rsaParams.Q = Q;rsaParams.DP = DP;rsaParams.InverseQ = InverseQ;rsaParams.D = D;rsaParams.DQ = DQ;RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();crypt.ImportParameters(rsaParams);RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter();formatter.SetHashAlgorithm("SHA1");formatter.SetKey(crypt);byte[] dataFile = new byte[] { 0x6F, 0x9F, 0x07, 0x04, 0xE2, 0x1A, 0xF7, 0xB8, 0xB2, 0x4F, 0x8D, 0x66, 0x49, 0xA1, 0x09, 0xA7, 0xB2, 0x22, 0x3C, 0xF9};byte[] signature = formatter.CreateSignature(dataFile);Now, my C++ code, which doesn't work:const char ModulusCON[0x80] = { 0xA3, 0x1D, 0x6C, 0xE5, 0xFA, 0x95, 0xFD, 0xE8, 0x90, 0x21, 0xFA, 0xD1, 0x0C, 0x64, 0x19, 0x2B, 0x86, 0x58, 0x9B, 0x17, 0x2B, 0x10, 0x05, 0xB8, 0xD1, 0xF8, 0x4C, 0xEF, 0x53, 0x4C, 0xD5, 0x4E, 0x5C, 0xAE, 0x86, 0xEF, 0x92, 0x7B, 0x90, 0xD1, 0xE0, 0x62, 0xFD, 0x7C, 0x54, 0x55, 0x9E, 0xE0, 0xE7, 0xBE, 0xFA, 0x3F, 0x9E, 0x15, 0x6F, 0x6C, 0x38, 0x4E, 0xAF, 0x07, 0x0C, 0x61, 0xAB, 0x51, 0x5E, 0x23, 0x53, 0x14, 0x18, 0x88, 0xCB, 0x6F, 0xCB, 0xC5, 0xD6, 0x30, 0xF4, 0x06, 0xED, 0x24, 0x23, 0xEF, 0x25, 0x6D, 0x00, 0x91, 0x77, 0x24, 0x9B, 0xE5, 0xA3, 0xC0, 0x27, 0x90, 0xC2, 0x97, 0xF7, 0x74, 0x9D, 0x6F, 0x17, 0x83, 0x7E, 0xB5, 0x37, 0xDE, 0x51, 0xE8, 0xD7, 0x1C, 0xE1, 0x56, 0xD9, 0x56, 0xC8, 0xC3, 0xC3, 0x20, 0x9D, 0x64, 0xC3, 0x2F, 0x8C, 0x91, 0x92, 0x30, 0x6F, 0xDB };const char ExponentCON[0x4] = { 0x00, 0x01, 0x00, 0x01 };const char PCON[0x40] = { 0xCC, 0xE7, 0x5D, 0xFE, 0x72, 0xB6, 0xFD, 0xE7, 0x1D, 0xE3, 0x1A, 0x0E, 0xAC, 0x33, 0x7A, 0xB9, 0x21, 0xE8, 0x8A, 0x84, 0x9B, 0xDA, 0x9F, 0x1E, 0x58, 0x34, 0x68, 0x7A, 0xB1, 0x1D, 0x7E, 0x1C, 0x18, 0x52, 0x65, 0x7B, 0x97, 0x8E, 0xA7, 0x6A, 0x9D, 0xEE, 0x5A, 0x77, 0x52, 0x3B, 0x71, 0x8F, 0x33, 0xD0, 0x49, 0x5E, 0xC3, 0x30, 0x39, 0x72, 0x36, 0xBF, 0x1D, 0xD9, 0xF2, 0x24, 0xE8, 0x71 };const char QCON[0x40] = { 0xCB, 0xCA, 0x58, 0x74, 0xD4, 0x03, 0x62, 0x93, 0x06, 0x50, 0x1F, 0x42, 0xF6, 0xAA, 0x59, 0x36, 0xA7, 0xA1, 0xF3, 0x97, 0x5C, 0x9A, 0xC8, 0x6A, 0x27, 0xCF, 0x85, 0x05, 0x2A, 0x66, 0x41, 0x6A, 0x7F, 0x2F, 0x84, 0xC8, 0x18, 0x13, 0xC6, 0x1D, 0x8D, 0xC7, 0x32, 0x2F, 0x72, 0x19, 0x3F, 0xA4, 0xED, 0x71, 0xE7, 0x61, 0xC0, 0xCF, 0x61, 0xAE, 0x8B, 0xA0, 0x68, 0xA7, 0x7D, 0x83, 0x23, 0x0B };const char DPCON[0x40] = { 0x4C, 0xCA, 0x74, 0xE6, 0x74, 0x35, 0x72, 0x48, 0x58, 0x62, 0x11, 0x14, 0xE8, 0xA2, 0x4E, 0x5E, 0xED, 0x7F, 0x49, 0xD2, 0x52, 0xDA, 0x87, 0x01, 0x87, 0x4A, 0xF4, 0xD0, 0xEE, 0x69, 0xC0, 0x26, 0x65, 0x53, 0x13, 0xE7, 0x52, 0xB0, 0x4A, 0xBB, 0xE1, 0x3E, 0x3F, 0xB7, 0x32, 0x21, 0x46, 0xF8, 0xC5, 0x11, 0x4D, 0x3D, 0xEF, 0x66, 0xB6, 0x50, 0xC0, 0x85, 0xB5, 0x79, 0x45, 0x8F, 0x61, 0x71 };const char InverseQCON[0x40] = { 0x28, 0x6A, 0xBB, 0xD1, 0x93, 0x95, 0x94, 0x1A, 0x6E, 0xED, 0xD7, 0x0E, 0xC0, 0x61, 0x2B, 0xC2, 0xEF, 0xE1, 0x86, 0x3D, 0x34, 0x12, 0x88, 0x6F, 0x94, 0xA4, 0x48, 0x6E, 0xC9, 0x87, 0x1E, 0x46, 0x00, 0x46, 0x00, 0x52, 0x8E, 0x9F, 0x47, 0xC0, 0x8C, 0xAB, 0xBC, 0x49, 0xAC, 0x5B, 0x13, 0xF2, 0xEC, 0x27, 0x8D, 0x1B, 0x6E, 0x51, 0x06, 0xA6, 0xF1, 0x62, 0x1A, 0xEB, 0x78, 0x2E, 0x88, 0x48 };const char DC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 0x73, 0xB6, 0xFA, 0x7F, 0x56, 0x2B, 0xE1, 0xDA, 0x4E, 0x27, 0x4E, 0xC2, 0x2F };// set the paramsCryptoPP::AutoSeededRandomPool rng;InvertibleRSAFunction params;Integer integ(ModulusCON);params.SetModulus(integ);Integer integ1(ExponentCON);params.SetPublicExponent(integ1);Integer integ2(PCON);params.SetPrime1(integ2);Integer integ3(QCON);params.SetPrime2(integ3);Integer integ4(DPCON);params.SetModPrime1PrivateExponent(integ4);Integer integ5(InverseQCON);params.SetMultiplicativeInverseOfPrime2ModPrime1(integ5);Integer integ6(DCON);params.SetPrivateExponent(integ6);Integer integ7(DQCON);params.SetModPrime2PrivateExponent(integ7);// create the keysRSA::PrivateKey privateKey(params);RSA::PublicKey publicKey(params);CryptoPP::RSASSA_PKCS1v15_SHA_Signer signer(privateKey);unsigned char data[20] = { 0x6F, 0x9F, 0x07, 0x04, 0xE2, 0x1A, 0xF7, 0xB8, 0xB2, 0x4F, 0x8D, 0x66, 0x49, 0xA1, 0x09, 0xA7, 0xB2, 0x22, 0x3C, 0xF9 };BYTE *signature = new BYTE[0x80];signer.SignMessage(rng, data, 20, signature);Based on what I know, Crypto++'s 'RSASSA_PKCS1v15_SHA_Signer' is what I want equivalent to C#'s 'RSAPKCS1SignatureFormatter' and setting the hash algorithm to SHA1.The error it throws is:Thanks for any help, Hetelek. 解决方案 I can't answer as to why this works in C#, but as for Crypto++, there are a couple of issues.Firstly, you're probably invoking the wrong constructor of Integer. I guess you want this one which converts from big-endian byte array:Integer (const byte *encodedInteger, size_t byteCount, Signedness s=UNSIGNED)but are using this one which convert from a string in base 2, 8, 10, or 16 (in your case base 10).So, using ExponentCON as an example, you should do:const int ExponentSize(0x4);const byte ExponentCON[ExponentSize] = { 0x00, 0x01, 0x00, 0x01 };Integer integ1(ExponentCON, ExponentSize);or possibly better still:std::string ExponentCON("101h"); // <-- Note trailing 'h' indicating hex encodingInteger integ1(ExponentCON.c_str());Next, the members of InvertibleRSAFunction must satisfy certain conditions in order to qualify as a valid key. These can be seen by stepping through the InvertibleRSAFunction::Validate function and I assume check for the conditions described here.params.Validate(rng, 3); // Returns false for your input dataThere are some helper functions which are designed to avoid having to set everything explicitly as you're doing. I don't know if these are suitable for you, but I'm referring to InvertibleRSAFunction::GenerateRandomWithKeySize and the overloaded InvertibleRSAFunction::Initialize.The Crypto++ Wiki describes the use of these functions with example code. 这篇关于RSASSA_PKCS1v15_SHA_Signer和PK_SignatureScheme :: KeyTooShort异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 20:14