Main Site Documentation

RSA Decrypt error


#1

Using the RSA sample from the ebook (on a mini). It keeps throwing Decrypt exception. Any thoughts? I ran out of ideas.

    #### Exception System.ArgumentException - 0xfd000000 (1) ####
    #### Message: 
    #### Microsoft.SPOT.Cryptography.Key_RSA::Decrypt [IP: 0000] ####
    #### FEZ_Mini_Application1.Program::TestRSA [IP: 0091] ####
    #### FEZ_Mini_Application1.Program::Main [IP: 0012] ####

public static void TestRSA()
{
    //this is shared between public and private keys
    byte[] module = new byte[] {0x31,   0xa1,   0xef,   0xdf,   0x59,   0x8a,   0x69,   0x4a,   0x14,   0x05,   0x6a,   0x00,   0x0e,   0x62,   0x89,   0xf7,   0xb4,   0x14,   0x98,   0x3c,   0x9b,   0xe8,   0xf9,   0xd2,   0xa7,   0xe1,   0x7d,   0x30,   0x32,   0x03,   0x57,   0x0c,   0x50,   0xd9,   0xa3,   0x03,   0x21,   0x2a,   0xfd,   0x92,   0x59,   0x4b,   0xdc,   0x04,   0x8d,   0xc1,   0xaf,   0x0a,   0x6f,   0x47,   0x1f,   0xb7,   0xa6,   0xea,   0x20,   0xcc,   0x65,   0x06,   0xd6,   0xe8,   0x26,   0x54,   0x24,   0xea,   0x6b,   0x89,   0x49,   0xf0,   0x39,   0x07,   0xce,   0x3c,   0x27,   0xfb,   0xe2,   0x95,   0x35,   0x8d,   0x41,   0x00,   0x3f,   0x12,   0xb0,   0xda,   0xd9,   0x80,   0x13,   0x7d,   0xd2,   0xc2,   0xed,   0x2e,   0xba,   0xeb,   0x5d,   0xe2,   0x42,   0x58,   0x8e,   0xc6,   0xf8,   0x83,   0x4d,   0x93,   0xfa,   0x79,   0x46,   0x35,   0x71,   0x9a,   0x41,   0xd5,   0x60,   0x22,   0x04,   0xfa,   0x6d,   0x7a,   0x39,   0x0a,   0x42,   0x71,   0x6d,   0xb3,   0x7d,   0x1a,   0xcc,   0x94,
    };

    //the private key...for dycrypting
    byte[] private_key = new byte[] {0x01,   0x30,   0x7e,   0x9b,   0x8d,   0x6a,   0x9d,   0xe9,   0xc4,   0xe5,   0xc7,   0x71,   0x6f,   0x6f,   0xe7,   0xc2,   0xbf,   0xf8,   0x58,   0x72,   0x7f,   0x21,   0xf2,   0x6d,   0x19,   0xc2,   0x83,   0x17,   0xf8,   0xa9,   0xb9,   0x51,   0x10,   0x16,   0xfc,   0x06,   0x7d,   0xb1,   0x36,   0x6a,   0xaf,   0x43,   0x7d,   0xa3,   0x86,   0x7c,   0xf4,   0x45,   0xa7,   0x3e,   0x8b,   0x4b,   0x7c,   0x9b,   0x89,   0x48,   0x46,   0x6c,   0xf5,   0x85,   0x79,   0x15,   0x6d,   0x55,   0xcf,   0x8f,   0x43,   0x39,   0xde,   0xe8,   0xd4,   0x3a,   0xcb,   0xca,   0x86,   0x49,   0x6a,   0x44,   0x62,   0xdc,   0x77,   0x00,   0x88,   0xfa,   0x5a,   0x71,   0x12,   0xde,   0x1b,   0x48,   0xfa,   0x70,   0xc4,   0x95,   0x1d,   0xe0,   0x4c,   0xb2,   0x96,   0x19,   0x89,   0x43,   0xd4,   0x6f,   0x2c,   0x0d,   0x05,   0x8c,   0x7f,   0xb4,   0x30,   0x5b,   0xd6,   0x9e,   0x2f,   0xd8,   0xa2,   0xb9,   0x22,   0x03,   0xc0,   0x26,   0xdf,   0x2b,   0xb3,   0x95,   0x77,   0x4d,
    };
    Debug.Print("Module: " +module.Length);
    Debug.Print("Private: " + private_key.Length);

    // the public key, always starts with 0x01, 0x00, 0x01,...
    // and the reast are all zeros
    byte[] public_key = new byte[128];
    public_key[0] = 1;
    public_key[2] = 1;
    Key_RSA rsa_encrypt = new Key_RSA(module, public_key);
    Key_RSA rsa_decrypt = new Key_RSA(module, private_key);
            
    // The data we want to encrypt
    string original_string = "FEZ is so easy";
    //convert to byte array
    byte[] original_data = UTF8Encoding.UTF8.GetBytes(original_string);

    //Encrypt the data
    byte[] encrypted_bytes = rsa_encrypt.Encrypt(original_data, 0, original_data.Length, null);

    //Decrypt the data
    byte[] decrypted_bytes = rsa_decrypt.Decrypt( encrypted_bytes, 0, encrypted_bytes.Length, null);

    //print the decrypted data
    string decrypted_string = new string(Encoding.UTF8.GetChars(decrypted_bytes));
    Debug.Print("Data Size= " + original_string.Length + " Data= " + original_string);
    Debug.Print("Encrypted Data size= " + encrypted_bytes.Length + " Decrypted Data= " + decrypted_string);
}

#2

Did you try the example as-is without any modifications?


#3

I did that first. I will try again to see if any change.


#4

Tried again with cut/paste. Seems to be same error. Does this code work on other mini’s out there? tia
#### Exception System.ArgumentException - 0xfd000000 (1) ####
#### Message:
#### Microsoft.SPOT.Cryptography.Key_RSA::Decrypt [IP: 0000] ####
#### FEZ_Mini_Application1.Program::TestRSAFez [IP: 0071] ####
#### FEZ_Mini_Application1.Program::Main [IP: 0021] ####
A first chance exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Native.dll

public static void TestRSAFez()
{
    //this is shared between public and private keys
    byte[] module = new byte[] { 0x17, 0xe5, 0x27, 0x40, 0xa9, 0x15,
0xbd, 0xfa, 0xac, 0x45, 0xb1, 0xb8, 0xe1, 0x7d, 0xf7, 0x8b, 0x6c, 0xbd, 0xd5,
0xe3, 0xf4, 0x08, 0x83, 0xde, 0xd6, 0x4b, 0xd0, 0x6c, 0x76, 0x65, 0x17, 0x52,
0xaf, 0x1c, 0x50, 0xff, 0x10, 0xe8, 0x4f, 0x4f, 0x96, 0x99, 0x5e, 0x24, 0x4c,
0xfd, 0x60, 0xbe, 0x00, 0xdc, 0x07, 0x50, 0x6d, 0xfd, 0xcb, 0x70, 0x9c, 0xe6,
0xb1, 0xaf, 0xdb, 0xca, 0x7e, 0x91, 0x36, 0xd4, 0x2b, 0xf9, 0x51, 0x6c, 0x33,
0xcf, 0xbf, 0xdd, 0x69, 0xfc, 0x49, 0x87, 0x3e, 0x1f, 0x76, 0x20, 0x53, 0xc6,
0x2e, 0x37, 0xfa, 0x83, 0x3d, 0xf0, 0xdc, 0x16, 0x3f, 0x16, 0xe8, 0x0e, 0xa4,
0xcf, 0xcf, 0x2f, 0x77, 0x6c, 0x1b, 0xe1, 0x88, 0xbd, 0x32, 0xbf, 0x95, 0x2f,
0x86, 0xbb, 0xf9, 0xb4, 0x42, 0xcd, 0xae, 0x0b, 0x92, 0x6a, 0x74, 0xa0, 0xaf,
0x5a, 0xf9, 0xb3, 0x75, 0xa3 };
    //the private key...for dycrypting
    byte[] private_key = new byte[] { 0xb9, 0x1c, 0x24, 0xca, 0xc8, 0xe8,
0x3d, 0x35, 0x60, 0xfc, 0x76, 0xb5, 0x71, 0x49, 0xa5, 0x0e, 0xdd, 0xc8, 0x6b,
0x34, 0x23, 0x94, 0x78, 0x65, 0x48, 0x5a, 0x54, 0x71, 0xd4, 0x1a, 0x35, 0x20,
0x00, 0xc6, 0x0c, 0x04, 0x7e, 0xf0, 0x34, 0x8f, 0x66, 0x7f, 0x8a, 0x29, 0x02,
0x5e, 0xe5, 0x39, 0x60, 0x15, 0x01, 0x58, 0x2b, 0xc0, 0x92, 0xcd, 0x41, 0x75,
0x1b, 0x33, 0x49, 0x78, 0x20, 0x51, 0x19, 0x3b, 0x26, 0xaf, 0x98, 0xa5, 0x4d,
0x14, 0xe7, 0x2f, 0x95, 0x36, 0xd4, 0x0a, 0x3b, 0xcf, 0x95, 0x25, 0xbb, 0x23,
0x43, 0x8f, 0x99, 0xed, 0xb8, 0x35, 0xe4, 0x86, 0x52, 0x95, 0x3a, 0xf5, 0x36,
0xba, 0x48, 0x3c, 0x35, 0x93, 0xac, 0xa8, 0xb0, 0xba, 0xb7, 0x93, 0xf2, 0xfd,
0x7b, 0xfa, 0xa5, 0x72, 0x57, 0x45, 0xc8, 0x45, 0xe7, 0x96, 0x55, 0xf9, 0x56,
0x4f, 0x1a, 0xea, 0x8f, 0x55 };
    // the public key, always starts with 0x01, 0x00, 0x01,...
    // and the reast are all zeros
    byte[] public_key = new byte[128];
    public_key[0] = public_key[2] = 1;
    Key_RSA rsa_encrypt = new Key_RSA(module, public_key);
    Key_RSA rsa_decrypt = new Key_RSA(module, private_key);
    // The data we want to encrypt
    string original_string = "FEZ is so easy!";
    //convert to byte array
    byte[] original_data = UTF8Encoding.UTF8.GetBytes(original_string);
    //Encrypt the data
    byte[] encrypted_bytes =
    rsa_encrypt.Encrypt(original_data, 0, original_data.Length, null);
    //Decrypt the data
    byte[] decrypted_bytes =
    rsa_decrypt.Decrypt(encrypted_bytes, 0, encrypted_bytes.Length,
    null);
    //print the decrypted data
    string decrypted_string =
    new string(Encoding.UTF8.GetChars(decrypted_bytes));
    Debug.Print("Data Size= " + original_string.Length +
    " Data= " + original_string);
    Debug.Print("Encrypted Data size= " + encrypted_bytes.Length +
    " Decrypted Data= " + decrypted_string);
}

#5

I tried it, there seems to be something wrong.
Please let us check and get back to you.


#6

Looks like the bug is related to the data size!! We are still looking for a fix but if you change the size of your data then it works fine. For example, 14 and 15 bytes do not work but 16 bytes work fine!


#7

There is a workaround suggested by Microsoft:

http://netmf.codeplex.com/workitem/335


#8

Thanks for getting back Gus.


#9

That was Mike not me :slight_smile:


#10

Thanks Mike. Maybe you chimps should change your hat colors :slight_smile:


#11

Hello,

I try your example but with my modulus and private key it doesn’t work. I create a certificate and I signed it with a CA with Openssl.

How can I be sure to have the good private/public/modulus informations ?

Thanks for your help
Lionel