Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSA Decryption infrequently returns empty array when C# would not #8

Open
wartab opened this issue Apr 9, 2019 · 1 comment
Open

Comments

@wartab
Copy link

wartab commented Apr 9, 2019

I am encountering an issue that is infrequently showing up when decrypting some bytes using the RSACryptoServiceProvider. In the vast majority of cases, the output is correct for other input data.

Example for an encrypted byte array that will cause the decryption to succeed in C# and to fail in JavaScript (C# code followed by JS code):

using (var rsa = new RSACryptoServiceProvider(1024))
{
    var rsaParameters = new RSAParameters
    {
        D = new byte[] {83, 2, 72, 41, 251, 127, 74, 7, 216, 204, 6, 47, 215, 147, 107, 94, 120, 146, 170, 101, 124, 157, 78, 54, 129, 219, 93, 31, 169, 110, 177, 206, 114, 43, 109, 64, 124, 190, 42, 23, 77, 36, 220, 207, 107, 232, 217, 111, 2, 46, 93, 60, 51, 100, 219, 55, 235, 160, 102, 11, 240, 115, 110, 12, 209, 32, 199, 123, 59, 223, 117, 80, 108, 187, 249, 222, 104, 96, 25, 196, 33, 46, 7, 2, 68, 193, 125, 167, 43, 200, 253, 150, 20, 151, 153, 224, 95, 54, 96, 222, 62, 154, 161, 205, 4, 35, 50, 106, 168, 104, 130, 136, 196, 206, 241, 213, 116, 197, 120, 61, 156, 166, 253, 253, 211, 32, 192, 37 },
        DP = new byte[] {140, 55, 185, 180, 167, 87, 181, 75, 29, 239, 103, 114, 171, 239, 159, 109, 97, 149, 194, 92, 155, 239, 145, 59, 17, 21, 125, 203, 18, 46, 30, 243, 203, 254, 132, 113, 241, 239, 69, 138, 234, 183, 223, 68, 103, 224, 132, 26, 195, 38, 10, 0, 254, 17, 26, 168, 134, 130, 126, 186, 233, 161, 75, 13},
        DQ = new byte[] {192, 93, 194, 73, 3, 66, 188, 80, 200, 178, 116, 150, 211, 157, 108, 91, 31, 35, 192, 20, 101, 144, 205, 222, 32, 58, 144, 121, 192, 107, 94, 52, 44, 156, 155, 12, 253, 85, 118, 111, 50, 221, 197, 131, 245, 173, 123, 119, 85, 133, 151, 182, 119, 91, 165, 234, 244, 146, 70, 160, 211, 134, 150, 183},
        Exponent = new byte[] {1, 0, 1},
        InverseQ = new byte[] {68, 124, 205, 56, 249, 77, 65, 126, 45, 165, 101, 65, 253, 121, 110, 174, 50, 108, 34, 215, 181, 146, 107, 192, 233, 189, 251, 120, 45, 112, 231, 182, 182, 135, 56, 112, 154, 83, 136, 31, 211, 63, 86, 254, 110, 120, 94, 174, 233, 157, 217, 202, 191, 16, 231, 255, 48, 195, 3, 161, 1, 64, 97, 69},
        P = new byte[] {208, 234, 226, 108, 191, 99, 90, 7, 113, 245, 148, 27, 208, 240, 224, 55, 129, 119, 171, 213, 51, 130, 33, 143, 223, 129, 17, 246, 193, 244, 40, 49, 83, 16, 186, 181, 153, 81, 131, 41, 79, 230, 234, 98, 209, 182, 123, 49, 187, 209, 48, 187, 56, 33, 3, 244, 153, 122, 240, 30, 151, 153, 247, 151},
        Q = new byte[] {231, 199, 50, 29, 21, 126, 75, 238, 165, 177, 106, 200, 57, 112, 108, 122, 74, 127, 150, 214, 195, 201, 64, 96, 0, 222, 134, 172, 69, 101, 209, 129, 213, 18, 50, 202, 95, 18, 188, 69, 164, 176, 117, 27, 6, 139, 74, 8, 61, 178, 110, 248, 72, 29, 4, 76, 195, 222, 249, 187, 63, 147, 194, 231},
        Modulus = new byte[] {189, 38, 129, 203, 191, 154, 166, 80, 184, 154, 185, 149, 93, 86, 109, 123, 9, 134, 117, 21, 152, 2, 41, 81, 6, 161, 75, 54, 147, 157, 204, 75, 177, 130, 166, 219, 54, 93, 37, 154, 10, 179, 76, 216, 40, 41, 216, 172, 163, 165, 171, 4, 210, 45, 175, 97, 214, 120, 150, 73, 89, 35, 39, 128, 157, 105, 31, 231, 175, 174, 129, 80, 168, 239, 228, 14, 207, 206, 184, 90, 124, 166, 165, 114, 184, 209, 13, 57, 71, 12, 23, 69, 107, 73, 213, 80, 2, 141, 239, 130, 21, 129, 102, 231, 237, 118, 170, 191, 108, 226, 231, 86, 92, 225, 184, 174, 72, 15, 20, 115, 74, 1, 134, 33, 206, 67, 215, 65},
    };

    rsa.ImportParameters(rsaParameters);

    var decrypted = rsa.Decrypt(new byte[]{43, 243, 196, 226, 169, 144, 254, 151, 45, 0, 216, 92, 167, 98, 18, 137, 20, 39, 234, 99, 147, 12, 255, 74, 157, 94, 46, 190, 169, 161, 150, 249, 138, 132, 212, 38, 252, 132, 7, 109, 80, 194, 251, 162, 60, 171, 156, 67, 157, 113, 142, 196, 225, 135, 27, 3, 139, 35, 72, 137, 246, 43, 145, 182, 197, 242, 253, 221, 66, 20, 96, 190, 161, 94, 228, 157, 146, 211, 108, 77, 172, 105, 110, 37, 87, 182, 126, 240, 16, 91, 178, 223, 116, 56, 135, 77, 111, 247, 187, 20, 223, 96, 187, 145, 18, 146, 37, 193, 71, 161, 71, 203, 16, 15, 17, 190, 121, 223, 233, 61, 126, 50, 217, 119, 135, 147, 56, 91}, true);
}

The variable decrypted will contain the following byte sequence:

36, 92, 119, 11, 33, 187, 194, 98, 6, 93, 234, 80, 122, 99, 158, 21, 82, 87, 94, 95, 198, 180, 250, 39, 84, 174, 162, 202, 217, 75, 150, 160

While running the follwing JavaScript code will result in an empty array.

let rsa = new System.Security.Cryptography.RSACryptoServiceProvider(1024);

let rsaParameters = new System.Security.Cryptography.RSAParameters();
rsaParameters.D = [83, 2, 72, 41, 251, 127, 74, 7, 216, 204, 6, 47, 215, 147, 107, 94, 120, 146, 170, 101, 124, 157, 78, 54, 129, 219, 93, 31, 169, 110, 177, 206, 114, 43, 109, 64, 124, 190, 42, 23, 77, 36, 220, 207, 107, 232, 217, 111, 2, 46, 93, 60, 51, 100, 219, 55, 235, 160, 102, 11, 240, 115, 110, 12, 209, 32, 199, 123, 59, 223, 117, 80, 108, 187, 249, 222, 104, 96, 25, 196, 33, 46, 7, 2, 68, 193, 125, 167, 43, 200, 253, 150, 20, 151, 153, 224, 95, 54, 96, 222, 62, 154, 161, 205, 4, 35, 50, 106, 168, 104, 130, 136, 196, 206, 241, 213, 116, 197, 120, 61, 156, 166, 253, 253, 211, 32, 192, 37 ];
rsaParameters.DP = [140, 55, 185, 180, 167, 87, 181, 75, 29, 239, 103, 114, 171, 239, 159, 109, 97, 149, 194, 92, 155, 239, 145, 59, 17, 21, 125, 203, 18, 46, 30, 243, 203, 254, 132, 113, 241, 239, 69, 138, 234, 183, 223, 68, 103, 224, 132, 26, 195, 38, 10, 0, 254, 17, 26, 168, 134, 130, 126, 186, 233, 161, 75, 13];
rsaParameters.DQ = [192, 93, 194, 73, 3, 66, 188, 80, 200, 178, 116, 150, 211, 157, 108, 91, 31, 35, 192, 20, 101, 144, 205, 222, 32, 58, 144, 121, 192, 107, 94, 52, 44, 156, 155, 12, 253, 85, 118, 111, 50, 221, 197, 131, 245, 173, 123, 119, 85, 133, 151, 182, 119, 91, 165, 234, 244, 146, 70, 160, 211, 134, 150, 183];
rsaParameters.Exponent = [1, 0, 1];
rsaParameters.InverseQ = [68, 124, 205, 56, 249, 77, 65, 126, 45, 165, 101, 65, 253, 121, 110, 174, 50, 108, 34, 215, 181, 146, 107, 192, 233, 189, 251, 120, 45, 112, 231, 182, 182, 135, 56, 112, 154, 83, 136, 31, 211, 63, 86, 254, 110, 120, 94, 174, 233, 157, 217, 202, 191, 16, 231, 255, 48, 195, 3, 161, 1, 64, 97, 69];
rsaParameters.P = [208, 234, 226, 108, 191, 99, 90, 7, 113, 245, 148, 27, 208, 240, 224, 55, 129, 119, 171, 213, 51, 130, 33, 143, 223, 129, 17, 246, 193, 244, 40, 49, 83, 16, 186, 181, 153, 81, 131, 41, 79, 230, 234, 98, 209, 182, 123, 49, 187, 209, 48, 187, 56, 33, 3, 244, 153, 122, 240, 30, 151, 153, 247, 151];
rsaParameters.Q = [231, 199, 50, 29, 21, 126, 75, 238, 165, 177, 106, 200, 57, 112, 108, 122, 74, 127, 150, 214, 195, 201, 64, 96, 0, 222, 134, 172, 69, 101, 209, 129, 213, 18, 50, 202, 95, 18, 188, 69, 164, 176, 117, 27, 6, 139, 74, 8, 61, 178, 110, 248, 72, 29, 4, 76, 195, 222, 249, 187, 63, 147, 194, 231];
rsaParameters.Modulus = [189, 38, 129, 203, 191, 154, 166, 80, 184, 154, 185, 149, 93, 86, 109, 123, 9, 134, 117, 21, 152, 2, 41, 81, 6, 161, 75, 54, 147, 157, 204, 75, 177, 130, 166, 219, 54, 93, 37, 154, 10, 179, 76, 216, 40, 41, 216, 172, 163, 165, 171, 4, 210, 45, 175, 97, 214, 120, 150, 73, 89, 35, 39, 128, 157, 105, 31, 231, 175, 174, 129, 80, 168, 239, 228, 14, 207, 206, 184, 90, 124, 166, 165, 114, 184, 209, 13, 57, 71, 12, 23, 69, 107, 73, 213, 80, 2, 141, 239, 130, 21, 129, 102, 231, 237, 118, 170, 191, 108, 226, 231, 86, 92, 225, 184, 174, 72, 15, 20, 115, 74, 1, 134, 33, 206, 67, 215, 65];

rsa.ImportParameters(rsaParameters);
let decrypted = rsa.Decrypt([43, 243, 196, 226, 169, 144, 254, 151, 45, 0, 216, 92, 167, 98, 18, 137, 20, 39, 234, 99, 147, 12, 255, 74, 157, 94, 46, 190, 169, 161, 150, 249, 138, 132, 212, 38, 252, 132, 7, 109, 80, 194, 251, 162, 60, 171, 156, 67, 157, 113, 142, 196, 225, 135, 27, 3, 139, 35, 72, 137, 246, 43, 145, 182, 197, 242, 253, 221, 66, 20, 96, 190, 161, 94, 228, 157, 146, 211, 108, 77, 172, 105, 110, 37, 87, 182, 126, 240, 16, 91, 178, 223, 116, 56, 135, 77, 111, 247, 187, 20, 223, 96, 187, 145, 18, 146, 37, 193, 71, 161, 71, 203, 16, 15, 17, 190, 121, 223, 233, 61, 126, 50, 217, 119, 135, 147, 56, 91], true);

console.log(decrypted);

But for instance, using the same RSA paramters but using the following encrypted byte array, we are getting the correct result in both programs:

var decrypted = rsa.Decrypt(new byte[]{55, 60, 151, 232, 112, 191, 217, 122, 81, 162, 169, 53, 115, 89, 192, 84, 138, 35, 69, 123, 78, 106, 190, 212, 251, 218, 88, 241, 184, 89, 4, 75, 133, 240, 121, 58, 40, 76, 190, 128, 141, 142, 30, 218, 170, 207, 3, 104, 107, 208, 0, 7, 227, 42, 186, 4, 17, 82, 139, 135, 127, 236, 185, 248, 136, 211, 185, 127, 169, 138, 165, 225, 113, 6, 31, 117, 123, 16, 91, 39, 103, 94, 113, 156, 42, 42, 167, 191, 179, 26, 7, 154, 88, 236, 113, 129, 93, 72, 97, 226, 205, 238, 207, 193, 200, 158, 192, 195, 73, 66, 6, 178, 213, 120, 238, 70, 219, 98, 66, 116, 177, 86, 84, 177, 183, 77, 239, 77});
let decrypted = rsa.Decrypt([55, 60, 151, 232, 112, 191, 217, 122, 81, 162, 169, 53, 115, 89, 192, 84, 138, 35, 69, 123, 78, 106, 190, 212, 251, 218, 88, 241, 184, 89, 4, 75, 133, 240, 121, 58, 40, 76, 190, 128, 141, 142, 30, 218, 170, 207, 3, 104, 107, 208, 0, 7, 227, 42, 186, 4, 17, 82, 139, 135, 127, 236, 185, 248, 136, 211, 185, 127, 169, 138, 165, 225, 113, 6, 31, 117, 123, 16, 91, 39, 103, 94, 113, 156, 42, 42, 167, 191, 179, 26, 7, 154, 88, 236, 113, 129, 93, 72, 97, 226, 205, 238, 207, 193, 200, 158, 192, 195, 73, 66, 6, 178, 213, 120, 238, 70, 219, 98, 66, 116, 177, 86, 84, 177, 183, 77, 239, 77], true);
console.log(decrypted);
12, 110, 218, 185, 192, 128, 248, 51, 241, 81, 54, 181, 114, 250, 222, 130, 249, 37, 163, 1, 70, 137, 181, 43, 119, 35, 50, 31, 117, 139, 74, 64

I'm assuming the issue is not on my end (correct me if I'm wrong).

@wartab
Copy link
Author

wartab commented Jan 23, 2020

In case anyone stumbles on this, I'm using this now, where it actually works correctly. No need to waste your time with this here, as it's not maintained.
https://github.com/rzcoder/node-rsa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant