C#RSA使用自定义密钥对进行加密和解密

时间:2018-11-08 17:26:08

标签: c# encryption rsa

我需要使用由工具生成的自定义RSA密钥对来解密数据。 但是,当我尝试导入私钥时,我得到了“错误的数据”证明。有想法吗?

private static RSAParameters publicKey;
private static RSAParameters privateKey;

static void Main(string[] args)
{
    RSACryptoServiceProvider encryptionProvider = new RSACryptoServiceProvider();

    encryptionProvider.PersistKeyInCsp = false;
    publicKey = encryptionProvider.ExportParameters(false);
    privateKey = encryptionProvider.ExportParameters(true);

    byte[] publicKeyArray =
    {
        0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
        0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x75, 0x59,
        0x9b, 0x89, 0xaa, 0xf7, 0x58, 0x70, 0xf0, 0xac, 0x84, 0x7d, 0xb3, 0xb2, 0x65, 0x86, 0x14, 0xc3,
        0x64, 0x7c, 0xda, 0x9b, 0x6b, 0x4c, 0xa8, 0x61, 0xca, 0x60, 0x7a, 0xbd, 0x06, 0x38, 0x1d, 0xe7,
        0xa1, 0x7a, 0x76, 0x0d, 0xf0, 0xde, 0x43, 0xa1, 0xc9, 0x4e, 0x13, 0x43, 0x31, 0xf0, 0x85, 0x95,
        0xc2, 0x1d, 0x83, 0xe5, 0xd2, 0x04, 0xda, 0xa5, 0x75, 0x44, 0xa4, 0xbd, 0x6b, 0x8c, 0x22, 0x4b,
        0xf7, 0xf6, 0x1c, 0x95, 0x26, 0x58, 0x59, 0x95, 0x24, 0xa9, 0xbf, 0x06, 0xa8, 0xc4, 0x6e, 0xd3,
        0x32, 0x67, 0x2d, 0x31, 0xa2, 0xbc, 0x4d, 0x66, 0xbd, 0xfa, 0xac, 0x5a, 0xd1, 0xf3, 0x0d, 0xb9,
        0x89, 0x2f, 0x20, 0x84, 0x27, 0xcf, 0xe2, 0x9d, 0x6b, 0xb2, 0xc1, 0x31, 0x06, 0x4d, 0xfd, 0x82,
        0x4a, 0x0a, 0x19, 0x76, 0x80, 0xb4, 0x49, 0x6c, 0x71, 0xdd, 0xd4, 0x98, 0x7f, 0x02, 0x03, 0x01,
        0x00, 0x01,
    };

    byte[] privateKeyArray =
    {
        0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x75, 0x59, 0x9b, 0x89,
        0xaa, 0xf7, 0x58, 0x70, 0xf0, 0xac, 0x84, 0x7d, 0xb3, 0xb2, 0x65, 0x86, 0x14, 0xc3, 0x64, 0x7c,
        0xda, 0x9b, 0x6b, 0x4c, 0xa8, 0x61, 0xca, 0x60, 0x7a, 0xbd, 0x06, 0x38, 0x1d, 0xe7, 0xa1, 0x7a,
        0x76, 0x0d, 0xf0, 0xde, 0x43, 0xa1, 0xc9, 0x4e, 0x13, 0x43, 0x31, 0xf0, 0x85, 0x95, 0xc2, 0x1d,
        0x83, 0xe5, 0xd2, 0x04, 0xda, 0xa5, 0x75, 0x44, 0xa4, 0xbd, 0x6b, 0x8c, 0x22, 0x4b, 0xf7, 0xf6,
        0x1c, 0x95, 0x26, 0x58, 0x59, 0x95, 0x24, 0xa9, 0xbf, 0x06, 0xa8, 0xc4, 0x6e, 0xd3, 0x32, 0x67,
        0x2d, 0x31, 0xa2, 0xbc, 0x4d, 0x66, 0xbd, 0xfa, 0xac, 0x5a, 0xd1, 0xf3, 0x0d, 0xb9, 0x89, 0x2f,
        0x20, 0x84, 0x27, 0xcf, 0xe2, 0x9d, 0x6b, 0xb2, 0xc1, 0x31, 0x06, 0x4d, 0xfd, 0x82, 0x4a, 0x0a,
        0x19, 0x76, 0x80, 0xb4, 0x49, 0x6c, 0x71, 0xdd, 0xd4, 0x98, 0x7f, 0x02, 0x03, 0x01, 0x00, 0x01,
        0x02, 0x81, 0x80, 0x19, 0x72, 0x11, 0x73, 0x19, 0x61, 0xfc, 0xf9, 0x0b, 0x00, 0xc8, 0x6b, 0xb1,
        0x2e, 0x26, 0xd5, 0x46, 0x7d, 0x25, 0x6f, 0x29, 0x4c, 0xbc, 0x2a, 0x93, 0x1f, 0xef, 0x4d, 0x8c,
        0x52, 0xf7, 0x87, 0x1c, 0xc5, 0x53, 0x52, 0x12, 0xa3, 0x52, 0x77, 0xab, 0x26, 0xf2, 0x9c, 0x8d,
        0xd4, 0x03, 0xc2, 0xfc, 0x86, 0x66, 0x60, 0x54, 0x28, 0xab, 0xd2, 0xcb, 0x04, 0xa1, 0x2a, 0x8a,
        0x2b, 0x9c, 0xf8, 0x35, 0xc8, 0x4a, 0x99, 0xdf, 0x86, 0xab, 0x7a, 0x2e, 0x92, 0x2c, 0xd6, 0xf8,
        0xcd, 0x99, 0xa6, 0xca, 0xc9, 0x95, 0x6c, 0x91, 0xd4, 0x7f, 0x01, 0x98, 0x62, 0x28, 0x38, 0x2b,
        0x8b, 0xc2, 0x29, 0x2c, 0x9b, 0xe0, 0xf1, 0x2c, 0x84, 0x76, 0xa0, 0x55, 0xd0, 0x29, 0x53, 0xac,
        0x8f, 0xe5, 0xb1, 0x99, 0xc2, 0x53, 0x44, 0xb0, 0x62, 0x41, 0x26, 0x85, 0x6e, 0xfc, 0xfc, 0x29,
        0x4d, 0x8b, 0xd1, 0x02, 0x41, 0x00, 0xdc, 0xe6, 0x21, 0x58, 0xff, 0x95, 0xcd, 0xc7, 0x26, 0xa3,
        0xd5, 0xcf, 0x9b, 0x90, 0x96, 0xfd, 0x7c, 0x37, 0x23, 0xcf, 0xd8, 0x1a, 0x9a, 0x7d, 0x64, 0x29,
        0x05, 0x75, 0x6b, 0x7a, 0x4b, 0xf2, 0x42, 0xf7, 0x34, 0xd5, 0x68, 0x9a, 0xc7, 0xb5, 0x6f, 0xc6,
        0xc0, 0xac, 0x18, 0xaa, 0xba, 0xcd, 0xe2, 0x1a, 0xa5, 0xa2, 0xa5, 0x44, 0x57, 0xf4, 0x50, 0x5c,
        0xad, 0x88, 0x16, 0x7d, 0x06, 0xdb, 0x02, 0x41, 0x00, 0xd5, 0xc4, 0xde, 0x1f, 0xbe, 0x29, 0x49,
        0x7d, 0x87, 0x7a, 0xdd, 0xa2, 0xdc, 0x40, 0xad, 0x93, 0xa2, 0x84, 0x82, 0xdd, 0xfb, 0xb8, 0x9d,
        0x5a, 0xa2, 0x65, 0x1d, 0x03, 0x35, 0x9f, 0x80, 0x0e, 0x85, 0x39, 0x87, 0xbf, 0xdf, 0x5b, 0x57,
        0x9e, 0x10, 0x95, 0x01, 0x78, 0x5c, 0x59, 0xdd, 0xdf, 0x7c, 0x41, 0x55, 0x9c, 0xb3, 0x39, 0x3d,
        0xa3, 0x06, 0xe8, 0xd8, 0x58, 0xbb, 0xc2, 0x6c, 0x2d, 0x02, 0x40, 0x05, 0xa0, 0xff, 0x9c, 0x8a,
        0xf6, 0xc0, 0x45, 0xa0, 0xe0, 0x6f, 0xe4, 0x58, 0x6c, 0x78, 0xcc, 0x80, 0xcb, 0xce, 0xe7, 0xce,
        0xf3, 0xee, 0x97, 0xdf, 0x63, 0x84, 0x36, 0x17, 0xc5, 0x08, 0x06, 0x66, 0x5a, 0x72, 0x48, 0x35,
        0xe0, 0x2b, 0xd2, 0x9d, 0xc4, 0x8c, 0xc8, 0x55, 0xe0, 0x93, 0xde, 0x07, 0x32, 0xf2, 0xa1, 0x90,
        0x60, 0x8b, 0x75, 0x38, 0x4f, 0x04, 0x45, 0x01, 0x7d, 0xb2, 0x1d, 0x02, 0x40, 0x27, 0x11, 0x10,
        0x6b, 0x0a, 0xac, 0xae, 0xf2, 0x92, 0xf4, 0xe0, 0x56, 0x68, 0x1c, 0x25, 0xdc, 0xaa, 0x41, 0xbf,
        0x4c, 0xe4, 0x38, 0xec, 0x00, 0xe9, 0x56, 0xc3, 0x43, 0x97, 0xe4, 0x2b, 0x59, 0xfb, 0xfc, 0xf4,
        0x93, 0x1b, 0x35, 0xad, 0x40, 0x84, 0xd5, 0x46, 0x6c, 0x81, 0xac, 0x26, 0xc1, 0x36, 0x0b, 0x16,
        0xa3, 0x49, 0x99, 0xb7, 0x6a, 0xf8, 0xd3, 0x4a, 0xde, 0x84, 0x89, 0xd6, 0x8d, 0x02, 0x41, 0x00,
        0xa0, 0x44, 0xca, 0x9e, 0xfd, 0xbe, 0x3a, 0x48, 0x6a, 0xf8, 0xc0, 0x1b, 0x1f, 0x11, 0x35, 0x25,
        0x35, 0x98, 0xca, 0x5d, 0x1e, 0x14, 0xc1, 0xf9, 0xa6, 0x25, 0x4a, 0xf4, 0x5c, 0x8f, 0xa3, 0xef,
        0xa0, 0xe5, 0x92, 0xfb, 0x11, 0x43, 0x4b, 0x00, 0xd7, 0xe5, 0x15, 0x9c, 0x20, 0xb3, 0x0a, 0x56,
        0x83, 0x8b, 0x09, 0x51, 0xb6, 0x71, 0x82, 0x87, 0x00, 0xc7, 0x4b, 0xf9, 0xe0, 0xf7, 0xe3, 0x79,
    };

    publicKey.Modulus = publicKeyArray;
    privateKey.Modulus = privateKeyArray;

    encryptionProvider.ImportParameters(publicKey);
    encryptionProvider.ImportParameters(privateKey);
}

顺便说一句,公钥似乎工作正常,如果我删除了“ encryptionProvider.ImportParameters(privateKey)”,则可以加密 一些数据成功了。

0 个答案:

没有答案