Can I rescue this corrupted DER format file? - rsa

I have two files that represent the same DER format public key. They both look like this when I look at them in a text editor (sublime)
3081 9f30 0d06 092a 8648 86f7 0d01 0101
0500 0381 8d00 3081 8902 8181 00d2 d475
9de6 d795 7d0f e5e8 375f 3605 7e06 e391
83e7 58b2 3e46 d4e8 da3e 0ac5 136c fff2
2d0e 8386 af7c 6347 a3fd 4c39 1989 7764
69b1 85e1 f5cc fe3f 4957 c110 4264 f112
ad8f e1b9 c108 cf5e 9a77 8d6c 60b2 150a
954f 725f 25c7 b720 e536 ca81 9686 fe71
4e86 1822 48a9 df17 6c95 f9ed 8e9b a674
1a9f 868d 92a9 d2a5 5721 6dd9 1f02 0301
0001
but one works with RSA.importKey() and one gives me the error "ValueError: RSA key format is not supported”.
When I read them in to python using "key = open('pub.der', "r").read()”
and then print out key, the file that works prints out as:
'0\x81\x9f0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x81\x8d\x000\x81\x89\x02\x81\x81\x00\xd2\xd4u\x9d\xe6\xd7\x95}\x0f\xe5\xe87_6\x05~\x06\xe3\x91\x83\xe7X\xb2>F\xd4\xe8\xda>\n\xc5\x13l\xff\xf2-\x0e\x83\x86\xaf|cG\xa3\xfdL9\x19\x89wdi\xb1\x85\xe1\xf5\xcc\xfe?IW\xc1\x10Bd\xf1\x12\xad\x8f\xe1\xb9\xc1\x08\xcf^\x9aw\x8dl`\xb2\x15\n\x95Or_%\xc7\xb7 \xe56\xca\x81\x96\x86\xfeqN\x86\x18"H\xa9\xdf\x17l\x95\xf9\xed\x8e\x9b\xa6t\x1a\x9f\x86\x8d\x92\xa9\xd2\xa5W!m\xd9\x1f\x02\x03\x01\x00\x01’
and the file that gives the error prints as:
'3081 9f30 0d06 092a 8648 86f7 0d01 0101\n0500 0381 8d00 3081 8902 8181 00d2 d475\n9de6 d795 7d0f e5e8 375f 3605 7e06 e391\n83e7 58b2 3e46 d4e8 da3e 0ac5 136c fff2\n2d0e 8386 af7c 6347 a3fd 4c39 1989 7764\n69b1 85e1 f5cc fe3f 4957 c110 4264 f112\nad8f e1b9 c108 cf5e 9a77 8d6c 60b2 150a\n954f 725f 25c7 b720 e536 ca81 9686 fe71\n4e86 1822 48a9 df17 6c95 f9ed 8e9b a674\n1a9f 868d 92a9 d2a5 5721 6dd9 1f02 0301\n0001’
I have another public key file for which I only have the corrupted/nonworking version. What is the difference between the two files, and how can I convert the version that does not work with RSA.importKey() into a version that does work with RSA.importKey()?

Related

chatterbot twitter_trainer ASCII encoding error

I am trying to run the chatterbot's TwitterTrainer on a separate program like so:
from chatterbot import ChatBot
from chatterbot.trainers import TwitterTrainer
from settings import TWITTER
import logging
# Comment out the following line to disable verbose logging
logging.basicConfig(level=logging.INFO)
chatbot = ChatBot("TwitterBot",
logic_adapters=[
"chatterbot.logic.BestMatch"
],
input_adapter="chatterbot.input.TerminalAdapter",
output_adapter="chatterbot.output.TerminalAdapter",
database="./twitter-database.db",
twitter_consumer_key=TWITTER["CONSUMER_KEY"],
twitter_consumer_secret=TWITTER["CONSUMER_SECRET"],
twitter_access_token_key=TWITTER["ACCESS_TOKEN"],
twitter_access_token_secret=TWITTER["ACCESS_TOKEN_SECRET"],
trainer="chatterbot.trainers.TwitterTrainer",
random_seed_word="random"
)
chatbot.train()
chatbot.logger.info('Trained database generated successfully!')
And i get errors that look like that:
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx) UnicodeDecodeError: 'utf8' codec can't decode byte 0x85 in position 94: invalid start byte
This program doesn't run more than 3 seconds straight, but some tweets are written to the twitter-database.db until exception occurs.
Also when looking at the trainer.py i saw this:
# TODO: Handle non-ascii characters properly
Any ideas about why this happens and how can i fix this?
Could you try to add Python Source Code Encoding top of your file # -*- coding: utf-8 -*-. These type error will occurs due to this. More information available here http://chatterbot.readthedocs.io/en/stable/encoding.html#fixing-encoding-errors

Parsing out values from source file and inserting into a template messages

If I have a text file with the following messages, how can I strip out the value after "55=" and "48=" for each message, and populate those values into a new template message where the "55=" and "48=" are from each message in the file?
Source file:
Recv: 8=FIX.4.2|9=00258|35=d|49=XXXX|56=XXXX|34=4|52=20151026-16:04:21.972|320=1|322=1:2|323=4|393=43389|15=USD|55=H8X|167=FUT|200=201603|541=20160301|205=1|48=17888384001387914931|207=CME|100=XCME|16552=1|16554=20|454=3|455=HW|456=99|455=H8XH6|456=98|455=H8X Mar16|456=97|10=223|
Recv: 8=FIX.4.2|9=00249|35=d|49=XXXX|56=XXXX|34=5|52=20151026-16:04:21.972|320=1|322=1:3|323=4|393=43389|15=USD|55=BQS|167=FUT|200=201601|541=20160101|205=1|48=17875701615154475972|207=CME|100=XNYM|16552=1|454=3|455=09|456=99|455=BQSF6|456=98|455=BQS Jan16|456=97|10=065|
Recv: 8=FIX.4.2|9=00264|35=d|49=xxx|56=xxx|34=6|52=20151026-16:04:21.972|320=1|322=1:4|323=4|393=43389|15=USD|55=T1A|167=FUT|200=201512|541=20151201|205=1|48=17665452254677820169|207=CME|100=XCBT|16552=0.001|16554=2000|454=3|455=06|456=99|455=T1AZ5|456=98|455=T1A Dec15|456=97|10=141|
Recv: 8=FIX.4.2|9=00268|35=d|49=xxxx|56=xxxx|34=7|52=20151026-16:04:21.972|320=1|322=1:5|323=4|393=43389|15=USD|55=T03|167=FUT|200=201511|541=20151101|205=1|48=17593596635603008360|207=CME|100=XNYM|16460=50|16552=5|16554=0.4|454=3|455=08|456=99|455=T03X5|456=98|455=T03 Nov15|456=97|10=088|
Systemically create the same template below with new 55= and 48 values from each message in the source file?
8=FIX.4.2|9=195|35=V|49=XXXX|56=XXXX|34=2|52=20151027-21:05:58|262=1|263=1|264=0|265=1|266=Y|267=5|269=0|269=1|269=2|269=4|269=5|146=1|55=HW|48=17888384001387914931|167=FUT|207=CME|10=247|
8=FIX.4.2|9=195|35=V|49=XXXX|56=XXXX|34=2|52=20151027-21:05:58|262=1|263=1|264=0|265=1|266=Y|267=5|269=0|269=1|269=2|269=4|269=5|146=1|55=BQS|48=17875701615154475972|167=FUT|207=CME|10=247|
see if this grep line helps you:
grep -oE '(48|55)=[^|]*' file
With your text input, the line above gives:
55=H8X
48=17888384001387914931
55=HW
55=H8XH6
55=H8X Mar16
55=BQS
48=17875701615154475972
55=09
55=BQSF6
55=BQS Jan16
55=T1A
48=17665452254677820169
55=06
55=T1AZ5
55=T1A Dec15
55=T03
48=17593596635603008360
55=08
55=T03X5
55=T03 Nov15
55= and 48 values from each message in the source file?
55=HW
48=17888384001387914931
55=BQS
48=17875701615154475972

phpseclib cant parse CSR from Microsoft CA?

In my ongoing struggle with public key crypto i ran into a snag that's possibly beyond my expertise with parsing CSR's:
I have not been able to get phpseclib to X509->loadCSR() this chunk from a MS server 2012r2 CA:
# more file
-----BEGIN NEW CERTIFICATE REQUEST-----
MIIFGDCCAwACAQAwOjEWMBQGCgmSJomT8ixkARkWBnNlY3VyZTEgMB4GA1UEAxMX
LlNlY3VyZSBFbnRlcnByaXNlIENBIDEwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAw
ggIKAoICAQCzgEpL+Za7a3y7YpURDrxlGIBlks25fD0tHaZIYkBTaXA5h+9MWoXn
FA7AlIUt8pbBvXdJbOCmGaeQmBfBH0Qy9vTbx/DR2IOwzqy2ZHuurI5bPL12ceE2
Mxa9xgY/i7U6MAUtoA3amEd7cKj2fz9EWZruRladOX0DXv9KexSan+45QjCWH+u2
Cxem2zH9ZDNPGBuAF9YsAvkdHdAoX8aSm05ZAjUiO2e/+L57whh7zZiDY3WIhin7
N/2JNTKVO6lx50S8a34XUKBt3SKgSR941hcLrBYUNftUYsTPo40bzKKcWqemiH+w
jQiDrln4V2b5EbVeoGWe4UDPXCVmC6UPklG7iYfF0eeK4ujV8uc9PtV2LvGLOFdm
AYE3+FAba5byQATw/DY8EJKQ7ptPigJhVe47NNeJlsKwk1haJ9k8ZazjS+vT45B5
pqe0yBFAEon8TFn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/BgKPIyPzZ4w
secxBTTCNgI48YezK3GDkn65cmlnkt6F6Mf0MwoDaXTuB88Jycbwb5ihKnHEJIsO
draiRBZruwMPwPIP
-----END NEW CERTIFICATE REQUEST-----
# openssl req -in file -noout -text
Certificate Request:
Data:
Version: 0 (0x0)
Subject: DC=secure, CN=.Secure Enterprise CA 1
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (4096 bit)
Modulus:
00:b3:80:4a:4b:f9:96:bb:6b:7c:bb:62:95:11:0e:
bc:65:18:80:65:92:cd:b9:7c:3d:2d:1d:a6:48:62:
40:53:69:70:39:87:ef:4c:5a:85:e7:14:0e:c0:94:
85:2d:f2:96:c1:bd:77:49:6c:e0:a6:19:a7:90:98:
17:c1:1f:44:32:f6:f4:db:c7:f0:d1:d8:83:b0:ce:
ac:b6:64:7b:ae:ac:8e:5b:3c:bd:76:71:e1:36:33:
16:bd:c6:06:3f:8b:b5:3a:30:05:2d:a0:0d:da:98:
47:7b:70:a8:f6:7f:3f:44:59:9a:ee:46:56:9d:39:
7d:03:5e:ff:4a:7b:14:9a:9f:ee:39:42:30:96:1f:
eb:b6:0b:17:a6:db:31:fd:64:33:4f:18:1b:80:17:
d6:2c:02:f9:1d:1d:d0:28:5f:c6:92:9b:4e:59:02:
35:22:3b:67:bf:f8:be:7b:c2:18:7b:cd:98:83:63:
75:88:86:29:fb:37:fd:89:35:32:95:3b:a9:71:e7:
44:bc:6b:7e:17:50:a0:6d:dd:22:a0:49:1f:78:d6:
17:0b:ac:16:14:35:fb:54:62:c4:cf:a3:8d:1b:cc:
a2:9c:5a:a7:a6:88:7f:b0:8d:08:83:ae:59:f8:57:
66:f9:11:b5:5e:a0:65:9e:e1:40:cf:5c:25:66:0b:
a5:0f:92:51:bb:89:87:c5:d1:e7:8a:e2:e8:d5:f2:
e7:3d:3e:d5:76:2e:f1:8b:38:57:66:01:81:37:f8:
50:1b:6b:96:f2:40:04:f0:fc:36:3c:10:92:90:ee:
9b:4f:8a:02:61:55:ee:3b:34:d7:89:96:c2:b0:93:
58:5a:27:d9:3c:65:ac:e3:4b:eb:d3:e3:90:79:a6:
a7:b4:c8:11:40:12:89:fc:4c:59:ce:2e:70:0e:6e:
59:8a:3b:5d:a2:d3:3a:8c:50:d0:00:96:6a:f5:73:
c8:cd:8c:ba:fe:76:1a:ee:4b:e4:2b:a2:96:66:cd:
9d:b8:98:09:c5:1b:e6:34:aa:97:e1:33:30:31:4c:
64:32:56:3c:86:c0:37:d1:2b:f0:11:9a:da:c6:a6:
64:4e:8e:3e:7e:e9:5b:8c:99:9e:8b:8d:fd:f1:37:
4f:5e:a6:6b:47:80:97:be:95:83:41:96:97:d4:62:
0d:7a:b6:6f:8e:ba:c9:f3:89:89:d7:2f:a9:c8:a3:
60:a1:39:8f:fb:18:d2:49:89:ff:5f:8e:fb:da:3c:
30:82:f7:c2:ba:e1:ab:f9:e6:ba:48:c7:b3:7f:5c:
7e:21:c0:77:90:42:41:10:6a:78:04:c4:4f:bc:50:
cc:81:34:53:66:bc:f9:c8:1d:51:d4:65:d6:28:e1:
22:ec:63
Exponent: 65537 (0x10001)
Attributes:
1.3.6.1.4.1.311.13.2.3 :6.3.9600.2.
Requested Extensions:
1.3.6.1.4.1.311.21.1:
...
X509v3 Subject Key Identifier:
E6:71:08:30:45:13:E6:63:1D:D5:67:A9:9A:F8:B0:80:AE:DD:23:3C
1.3.6.1.4.1.311.20.2: ..S.u.b.C.A
X509v3 Key Usage:
Digital Signature, Certificate Sign, CRL Sign
X509v3 Basic Constraints: critical
CA:TRUE
Signature Algorithm: sha256WithRSAEncryption
a6:65:e9:b0:1e:68:80:f7:6f:f8:9c:e1:ac:ed:2f:c6:59:23:
bb:e2:0f:70:3d:a2:d5:0a:c7:ab:f5:5d:ee:b7:cc:33:d1:f8:
38:96:4a:7c:5b:f6:6f:a5:f0:e3:ed:9d:69:1c:2e:4c:c4:20:
cf:c8:31:6a:2b:1c:a5:00:27:f2:7c:ef:50:f4:43:1f:b4:fc:
76:e4:72:b8:cc:b5:5b:bf:74:da:ed:6f:d6:7d:09:7b:a9:eb:
a6:6f:c5:33:dc:a4:4c:e2:ab:4f:e7:db:2c:3c:41:d0:5a:6b:
e9:65:8b:ae:50:73:07:32:b7:33:10:2b:a5:90:4d:16:1f:bd:
50:88:5f:d0:0a:70:f2:b1:c3:1d:43:2c:7f:bb:84:bf:db:11:
10:75:95:40:ee:e7:d2:71:e9:f4:1f:a8:9a:c5:54:e6:36:37:
c7:34:30:6b:ee:ad:fa:07:1a:61:28:c8:23:08:ca:f9:88:32:
ee:3e:ff:04:8d:10:4f:ec:48:3a:78:95:9e:b3:b8:5e:ab:7f:
59:b4:1a:22:dd:4d:7d:97:24:4f:a0:7e:f7:a9:25:31:66:a7:
84:b7:58:5a:a4:b3:32:af:6b:10:48:4f:e8:d5:d1:72:93:34:
3c:27:3b:e7:69:13:1c:14:e2:36:54:87:5f:02:74:41:64:21:
4b:90:6f:48:05:17:c0:7b:58:91:55:a4:d7:6c:46:a6:51:3c:
1c:fc:dc:15:b5:2a:3a:34:c8:09:9a:a1:3a:c6:36:61:b4:a7:
a4:1f:dc:ad:b3:6e:4d:7e:26:29:37:55:9a:b8:ae:f2:a8:4e:
2d:b4:e9:1b:cb:03:6f:ff:70:c3:5f:91:83:ec:ba:ab:e8:1c:
67:b6:7b:41:8e:2e:d2:01:eb:7c:09:ce:5a:16:44:ea:5b:ef:
6a:21:10:7b:2f:08:ab:a5:94:82:55:4a:e8:92:f6:c7:d3:04:
07:a7:1c:2c:a7:76:00:04:77:84:f7:9e:94:d4:75:a8:8d:ba:
65:44:9b:98:a5:64:87:7b:2c:06:1e:90:98:64:80:5b:b7:be:
57:25:f1:b9:41:24:a8:c4:b2:45:53:a7:b2:69:06:67:57:08:
bf:c6:ff:a1:f6:48:58:dc:6b:82:4c:38:0c:b7:0f:1c:25:19:
d8:27:78:86:97:6b:31:ec:e6:70:4a:fc:18:0a:3c:8c:8f:cd:
9e:30:b1:e7:31:05:34:c2:36:02:38:f1:87:b3:2b:71:83:92:
7e:b9:72:69:67:92:de:85:e8:c7:f4:33:0a:03:69:74:ee:07:
cf:09:c9:c6:f0:6f:98:a1:2a:71:c4:24:8b:0e:76:b6:a2:44:
16:6b:bb:03:0f:c0:f2:0f
I added the mystery vendor specific OID's to the OID list in hopes that would get me somewhere:
// the following are X.509 extensions not supported by phpseclib
'1.3.6.1.5.5.7.1.12' => 'id-pe-logotype',
'1.3.6.1.4.1.311.13.2.3' => 'szOID_OS_VERSION',
'1.3.6.1.4.1.311.20.2' => 'szOID_ENROLL_CERTTYPE_EXTENSION',
'1.3.6.1.4.1.311.21.1' => 'szOID_CERTSRV_CA_VERSION',
But I am unable to get past this part of the loadCSR() function:
$asn1->loadOIDs($this->oids);
$decoded = $asn1->decodeBER($csr);
//... $decoded contains content, but turns into binary junk in the middle
$csr = $asn1->asn1map($decoded[0], $this->CertificationRequest);
if (!isset($csr) || $csr === false) {
Apparently the asn1map function does not like the garbage in the middle of $decoded??? Not sure if there is some kind of way to go about debugging this I am missing, but I dont know enough about BER decoding and asn1 mapping to correct the issue on my own =(
Pretty please help, I really love this library (i use it for so many great things) and would like to use it to authorize enterprise CA's from my offline Linux Root CA with a really awesome php web frontend ;D
The problem is that phpseclib does not appear to support the "Requested Extensions" block that openssl req is showing. A quick Google search suggests that CSR's are governed by PKCS10 and that the "Requested Extensions" block is governed by PKCS9. I'll contact the author and see what can be done about addressing this.
In the mean time, a quick workaround is this:
#
#-----[ OPEN ]------------------------------------------
#
File/ASN1.php
#
#-----[ FIND ]------------------------------------------
#
return $i < $n? null: $map;
#
#-----[ REPLACE WITH ]----------------------------------
#
return $map;

ASN1 sequence to use in Convert::PEM to read pkcs8 DER privateKey? PERL

I'm trying to convert to PEM a private key which is encoded and encrypted in DER using pkcs8, with module Convert::PEM and it ask for an ASN Sequence (I believe the algorithm used to encrypt is EDE_EDE3)
I've found this documentation: Public-Key Cryptography Standards (PKCS) #8: which describes the syntax used, but I haven't been able to applied successfully in my PERL code, I've tried:
my $pem = Convert::PEM->new(
Name => "RSA PRIVATE KEY",
ASN => qq(
PrivateKeyInfo ::= SEQUENCE {
version Version,
privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
privateKey PrivateKey,
attributes [0] IMPLICIT Attributes OPTIONAL }
Version ::= INTEGER
PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
PrivateKey ::= OCTET STRING
Attributes ::= SET OF Attribute
}
))or die "New failed: ", Convert::PEM->errstr;
What am I doing wrong?

Cucumber, Rspec: unicode symbols in output

I wonder if it is possible to make Cucumber output matching errors in Russian instead of this:
Сценарий: Успешное добавление кгиги # features/books/add_book.feature:12
Если я добавил книгу # features/step_definitions/books_steps.rb:3
То я должен увидеть добавленную книгу # features/step_definitions/books_steps.rb:15
expected there to be content "\320\235\320\260\320\267\320\262\320\260\320\275
\320\270\320\265 \320\272\320\275\320\270\320\263\320\270" in "\320\236\321\210\320\270\320
\261\320\272\320\260 502!\n...
Where "\320\235\320\260\320\267\320\262\320\260\320\275" is a Russian word. It may be a feature of Rspec. Any Ideas would be great.
Adding
$KCODE='u'
to my features/support/env.rb helped a little:
А должен увидеть сообщение о том, что пароль неверен
expected there to be content "Неверный прол\321\214"
This solution is only for 1.8.7 – in 1.9.3
# encoding: utf-8
works just fine