please notice that the "privateExponent" section of the output below is only 127 bytes long. My investigation of this keyset determined that the privateExponent was missing a trailing '00'. (when I added a '00' to the tail I was able to encrypt/decrypt.)
Looking at the code I was not able to determine if BN_num_bytes() and the subsequent calls were correct or effective. (Of course I have a workaround in my client code, however, that's just plain kludgy.)
OpenSSL> version OpenSSL 0.9.8g 19 Oct 2007 I used the following command line: openssl genrsa 1024 2>/dev/null |openssl rsa -text -noout generating the following results: Private-Key: (1024 bit) modulus: 00:d1:7a:62:a9:94:6c:ed:a2:1d:83:91:24:c4:78: d7:6e:7e:03:dc:f7:5b:36:56:60:26:73:78:46:98: 9e:2f:79:cc:c4:c9:f9:d3:29:0b:2b:77:c7:1d:3d: db:26:9e:30:95:8a:1d:8d:d6:44:fd:8c:7a:1d:bb: f5:d1:89:bf:1d:a4:b6:7e:2a:80:7f:6f:c8:4d:ff: dd:02:69:5b:c9:68:af:85:ba:0b:3f:54:19:46:39: ca:0f:0a:14:ca:36:04:be:65:47:51:6d:32:d3:bd: 3c:6b:5e:c7:2b:e5:74:64:66:ca:24:0c:95:d0:19: 6d:09:57:3e:6d:2c:6f:81:f1 publicExponent: 65537 (0x10001) privateExponent: 11:2e:f8:e8:f0:43:ff:3a:4d:31:7d:b6:86:67:63: 44:ad:11:a4:0e:a6:6c:ce:a9:14:7e:64:5f:66:25: 93:bf:6a:0c:ba:84:c3:cd:9a:1c:f6:b6:5e:af:b4: 4a:76:af:85:94:ce:d7:d3:bb:d5:10:b0:f3:bf:77: 6a:b8:e1:f4:66:8f:fc:5b:2a:96:46:0b:6a:9a:87: 8d:1f:cd:2a:bc:3b:16:8c:b9:1e:09:2c:9f:95:89: ce:15:e9:d8:97:c3:78:cd:a3:31:c0:d8:ac:98:a7: ee:fe:b4:b2:2b:8b:59:af:41:6a:58:da:d9:ce:08: 3a:ce:7d:04:c1:6a:01 prime1: 00:e9:e7:86:4f:ce:e2:e1:61:b0:3e:3c:1f:37:27: 50:29:d9:47:14:4a:12:6e:61:21:cb:c6:2d:d3:55: 8c:58:d2:6c:6c:25:c7:7d:1a:f7:24:45:34:b1:e9: 1f:3f:83:12:40:be:48:f8:60:54:e4:22:2c:98:34: ab:3d:b3:7e:51 prime2: 00:e5:44:29:9b:e3:11:18:fc:ae:3c:31:48:f0:f3: f8:cc:46:1c:c0:6f:e6:b1:66:f6:9c:ce:5f:b4:15: 68:74:3d:b8:c7:09:2f:d7:37:38:b9:83:0a:2b:2a: f5:c7:95:a4:b0:f2:41:72:70:05:73:ab:f7:17:ce: ef:b8:b2:c1:a1 exponent1: 00:c4:26:ab:64:dc:1b:39:64:81:b6:ff:16:32:5b: 5e:87:69:b5:88:d7:41:33:9d:73:08:fe:ed:c9:0f: 31:bd:fe:e0:7b:a5:06:f0:ec:1f:1b:24:8a:37:34: 47:47:2c:b4:ae:e4:a4:2e:de:21:f2:9a:92:09:26: 0a:00:99:38:b1 exponent2: 3a:10:77:51:49:c2:cc:c7:42:01:70:a1:fe:94:6b: 90:a0:b9:2b:6b:ab:cb:86:3e:33:1c:79:54:8c:8f: 75:d1:23:f9:14:b4:8f:17:27:40:a4:95:70:b5:62: a5:f7:9a:4c:4e:2e:91:23:19:de:5f:d1:26:b0:7c: 3a:14:db:c1 coefficient: 00:c6:d4:8e:e1:c2:5c:89:88:3d:ad:1f:7b:aa:72: 5d:83:82:7f:cc:84:8e:06:c1:31:d1:92:7d:e3:e0: 3f:80:44:b7:6d:51:de:70:2f:ae:60:b8:53:d3:25: 56:5f:73:b5:7a:01:3a:58:64:28:00:ee:9a:e0:db: c8:01:27:62:64