How to get the Decimal value of Private Key Contents in RSA - hex
From Private Key, I am trying to calculate the decimal value of prime1, prime2, coefficient, exponent1, exponent2, private exponent and public exponent.
With the command OpenSSL> rsa -in mykeyprivatekey.txt -text -noout, i am getting something like below. With that not sure how to convert into decimal value.
modulus:
00:84:ea:3d:7d:57:67:ad:a4:75:45:ba:67:2e:7c:
28:d6:b2:cd:28:05:cb:19:6e:90:d1:46:cc:4a:c3:
bf:ae:1d:87:fe:0e:cf:30:3b:1c:88:15:b3:89:1e:
24:c8:da:e0:56:61:fb:fb:01:c9:90:bd:23:7a:3a:
22:45:5c:ff:59:28:80:6c:59:3b:8a:d1:eb:f0:bb:
ce:07:8f:0e:a5:15:db:96:ee:c9:f8:7d:f6:cc:be:
6c:5a:3e:7f:07:24:38:9a:04:67:95:3d:c4:9b:eb:
43:74:9c:76:95:18:f4:bb:cb:30:da:a5:d6:aa:6c:
f1:4e:c6:09:ed:2d:2d:1e:2c:d9:eb:3c:55:08:0d:
13:00:45:fe:d3:1f:a8:03:39:d3:65:15:dd:8e:72:
9f:55:97:bd:3e:6a:9a:e8:82:24:0b:c0:84:8c:17:
32:37:b3:85:55:19:73:09:30:01:33:3e:77:7e:b0:
df:d6:da:a6:94:c6:3c:79:40:19:88:c9:5c:8d:f4:
9d:d0:f6:52:6f:03:4a:51:96:e7:09:8c:74:e9:09:
39:41:b9:65:9d:10:98:a6:f9:b4:ec:21:41:11:4f:
18:97:34:da:50:84:ec:5c:57:dc:07:ee:e4:4b:9b:
50:57:b5:d4:03:33:59:40:49:43:e4:1e:ab:58:7c:
6a:31
publicExponent: 65537 (0x10001)
privateExponent:
04:a6:81:72:42:3d:a1:68:b0:a5:b8:e8:e5:4a:78:
cf:52:40:35:57:46:8c:e4:3e:72:e4:0c:3f:cd:6b:
86:77:08:0c:0e:8e:97:3c:6a:3a:24:b0:3c:6a:ee:
94:8c:7b:ee:e9:cc:bc:32:8a:cf:e2:9e:3c:27:8e:
8d:68:bc:92:6d:05:59:45:21:fd:30:6d:15:e6:2c:
46:3d:6e:20:be:8c:09:52:df:84:2f:3c:14:5b:d1:
44:b3:0d:14:bd:e2:b1:18:d8:5a:f3:67:4e:a1:43:
a1:ef:db:82:49:f6:3e:1f:d4:dc:c1:92:aa:31:d7:
9b:8e:f8:47:f1:1d:b4:01:86:d1:cf:5f:57:f0:b5:
bf:fb:44:12:a8:38:bb:a6:14:31:60:51:2f:7c:44:
33:22:d3:0d:57:f1:33:1d:3e:1d:d6:95:f7:0c:2f:
36:b7:13:c6:9a:6a:ee:67:cb:82:c2:6c:f3:e9:38:
79:c4:04:23:6b:cc:54:b8:45:36:25:c2:78:e5:05:
06:5f:c5:69:7f:3b:01:b9:9b:d7:fa:56:59:20:79:
80:8c:99:20:bf:23:d4:46:00:a1:61:d8:d5:c6:cd:
cd:c1:e0:98:63:57:c7:06:8a:35:b2:b0:6c:8a:2e:
bb:04:98:93:8e:b9:5d:69:56:9c:b8:d9:0b:c5:12:
05
prime1:
00:b9:f0:c3:ee:ce:bc:6e:41:6c:96:51:86:ae:b4:
a7:eb:8c:b3:12:79:ea:e2:5d:0c:4d:f8:f6:52:3b:
ca:df:5a:ec:db:87:8a:7a:01:84:50:6d:27:47:70:
b5:f6:9f:c7:94:cb:fa:8e:5b:86:14:7e:dd:61:bb:
c1:b7:32:4f:98:f9:ee:b3:b4:1b:8e:fd:22:64:52:
97:4e:ba:b9:90:c6:db:fb:9a:ba:1d:f0:2a:e5:e9:
69:97:1e:11:1b:1d:3b:8b:69:c8:7c:d5:4d:ac:aa:
33:9c:06:c3:33:67:1d:80:47:a1:1d:66:8c:87:24:
ef:47:84:f4:ed:2d:67:09:c3
prime2:
00:b6:fe:cd:91:0f:71:47:38:df:e0:a4:d8:1e:f2:
09:b7:ad:02:d4:08:a5:3f:53:88:2d:0c:2e:72:16:
9d:e6:3b:13:51:c0:c3:d7:b4:de:35:b9:25:3e:5f:
1f:48:b3:2d:ad:da:53:94:ea:95:ab:7d:db:7c:1e:
ab:29:3d:b1:a0:e5:3e:c0:17:2e:f7:8d:7e:98:68:
8b:88:80:26:79:e8:79:fb:55:43:32:16:ed:e5:44:
e4:fa:09:2e:61:2a:b5:ed:cc:70:ae:0f:0a:20:48:
61:11:de:75:eb:b4:14:f2:df:97:45:46:a6:f0:e7:
ce:17:d4:56:89:bc:fd:48:fb
exponent1:
49:25:3a:9e:95:fb:30:50:52:5c:2b:c4:14:b4:02:
9e:b9:e1:b3:f1:fc:01:82:63:15:c5:4b:78:fc:06:
89:a8:e6:26:52:02:9a:8e:40:33:b7:4c:cd:60:bc:
64:41:64:ac:d7:35:40:8d:26:88:a7:83:85:2f:ec:
3b:99:f2:10:16:b9:dd:3d:8b:dd:cb:1c:9d:64:94:
1b:27:7f:5a:41:b0:46:21:0e:f8:3a:03:56:20:65:
26:4a:70:a6:3e:f0:ae:9e:35:7d:4e:17:d9:45:b7:
39:b8:bb:4f:5c:50:11:a7:9b:ec:c9:eb:9c:3a:5e:
5a:bf:25:ee:ab:7e:ae:2b
exponent2:
00:9f:63:0b:b5:7f:57:d1:0b:0e:a4:61:b0:04:34:
18:96:ac:d0:bb:d1:d3:ec:76:5b:33:b1:5a:7a:f0:
8c:13:4d:fd:23:c5:dc:4e:73:30:e0:66:24:b4:50:
b1:b4:e6:ab:b0:b3:6a:fd:57:dd:89:03:14:3a:e5:
93:7b:56:4a:f2:b2:f9:96:e2:1a:7c:6f:f7:85:d3:
f9:44:a9:29:da:20:cc:ae:46:b3:42:5b:8a:55:99:
4e:3f:21:e4:0d:a9:df:d0:01:b3:5b:47:aa:30:38:
1a:a4:2f:b9:51:d5:06:32:0c:e1:7a:eb:70:d8:7f:
6e:98:7e:cf:e9:24:7d:10:25
coefficient:
00:b7:37:fa:ea:7e:88:73:27:55:87:17:62:c9:14:
c9:9e:3d:cc:df:5e:5c:88:92:6f:bc:0f:b3:ad:47:
53:80:eb:1b:01:74:b0:66:34:64:69:e5:08:37:16:
2e:f9:1a:bf:8b:7a:3f:ad:c6:9a:4e:ce:33:48:6b:
8f:25:0b:80:b0:38:62:f2:db:47:03:f4:c5:20:c0:
a0:ac:85:db:aa:3a:2c:b7:00:9a:a0:e7:5f:43:47:
06:20:c9:4d:df:80:ab:96:cb:20:d2:81:c3:65:68:
d1:ea:b9:7e:62:79:3a:2c:99:15:3f:1c:17:a9:aa:
18:ba:9c:8d:9d:51:17:8d:4a
With this [http://md5decrypt.net/en/Conversion-tools/#results] tried to convert the hexadecimal to the decimal. But the decimal value of prime1*prime2 != modulus value.
I am doing something wrong in calculating the decimal value. Can you help me in converting this into decimal
From the openssl I don't find a way. still was able to get the decimal value from the equivalent hex code from https://defuse.ca/big-number-calculator.htm
For example for the prime1 value say 0x00b6fecd910f714738dfe0a4d81ef209b7ad02d408a53f53882d0c2e72169de63b1351c0c3d7b4de35b9253e5f1f48b32dadda5394ea95ab7ddb7c1eab293db1a0e53ec0172ef78d7e98688b88802679e879fb55433216ede544e4fa092e612ab5edcc70ae0f0a20486111de75ebb414f2df974546a6f0e7ce17d45689bcfd48fb
when used in the online convetor https://defuse.ca/big-number-calculator.htm gives the equivalent decimal value.
Also additional Info on different legends:
prime1: p
prime2: q
modulus: N = p * q
publicExponent: e
privateExponent: d
exponent1 = dp
exponent2 = dq
coefficient = qInv
Related
What does NNN mean in date format <YYMMDDhhmmssNNN><C|D|G|H>?
hi I has date format and I want converted to correct GMT date : <YYMMDDhhmmssNNN><C|D|G|H> Sample value on that date: 210204215026000C I get this explanation for part NNN : NNN If flag is C or D then NNN is the number of hours relativeto GMT, if flag is G or H, NNN is the number of quarter hours relative to GMT C|D|G|H C and G = Ahead of GMT, D and H = Behind GMT but I did not get how number of hours relative to GMT can present on 3 digits ? it should be in 2 digit as i knew the offset for hours related to GMT is from 0 to 23 , and also what quarter hours relative to GMT mean ? I want to use Scala or Java.
I don’t know why they set 3 digits aside for the offset. I agree with you that 2 digits suffice for all cases. Maybe they just wanted to be very sure they would never run of out space, and maybe they even overdid this a bit. 3 digits is not a problem as long as the actual values are within the range that java.time.ZoneOffset can handle, +/-18 hours. In your example NNN is 000, so 0 hours from GMT, which certainly is OK and trivial to handle. A quarter hour is a quarter of an hour. As Salman A mentioned in a comment, 22 quarter hours ahead of Greenwich means an offset of +05:30, currently used in Sri Lanka and India. If the producer of the string wants to use this option, they can give numbers up to 72 (still comfortably within 2 digits). 18 * 4 = 72, so 18 hours equals 72 quarter hours. To imagine a situation where 2 digits would be too little, think an offset of 25 hours. I wouldn’t think it realistic, on the other hand no one can guarantee that it will never happen. Java solution: how to parse and convert to GMT time I am using these constants: private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{12})(\\d{3})(\\w)"); private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("uuMMddHHmmss"); private static final int SECONDS_IN_A_QUARTER_HOUR = Math.toIntExact(Duration.ofHours(1).dividedBy(4).getSeconds()); Parse and convert like this: String sampleValue = "210204215026000C"; Matcher matcher = DATE_PATTERN.matcher(sampleValue); if (matcher.matches()) { LocalDateTime ldt = LocalDateTime.parse(matcher.group(1), FORMATTER); int offsetAmount = Integer.parseInt(matcher.group(2)); char flag = matcher.group(3).charAt(0); // offset amount denotes either hours or quarter hours boolean quarterHours = flag == 'G' || flag == 'H'; boolean negative = flag == 'D' || flag == 'H'; if (negative) { offsetAmount = -offsetAmount; } ZoneOffset offset = quarterHours ? ZoneOffset.ofTotalSeconds(offsetAmount * SECONDS_IN_A_QUARTER_HOUR) : ZoneOffset.ofHours(offsetAmount); OffsetDateTime dateTime = ldt.atOffset(offset); OffsetDateTime gmtDateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC); System.out.println("GMT time: " + gmtDateTime); } else { System.out.println("Invalid value: " + sampleValue); } Output is: GMT time: 2021-02-04T21:50:26Z I think my code covers all valid cases. You will probably want to validate that the flag is indeed C, D, G or H, and also handle the potential DateTimeException and NumberFormatException from the parsing and creating the ZoneOffset (NumberFormatException should not happen).
Having Trouble converting a string to bytes
I'm trying to convert a string to a byte, thats what I've done. I want to send out a Modbusprotocol via serial and put it together in bitstring: tiger = '01' read = '03' ac_val = '0031' word = '0002' code = tiger+read+ac_val+word print(code) 010300310002 #now i want to put thist string in a bitstring with the function: codeh = bytes.fromhex(code) codeh = b'\x01\x03\x001\x00\x02 #This is what i got But i was expecting: codeh = b'\x01\x03\x00\x31\x00\x02 I have no idea why the output is like this.
What it's showing in the output is the ASCII representation of the byte values. Hex 31 corresponds to the ascii character '1'. Try this to see a demonstration: bytes.fromhex('415343494921') Here's a chart that shows these low values: https://en.wikipedia.org/wiki/ASCII#Control_code_chart
The encryption won't decrypt
I was given an encrypted copy of the study guide here, but how do you decrypt and read it??? In a file called pa11.py write a method called decode(inputfile,outputfile). Decode should take two parameters - both of which are strings. The first should be the name of an encoded file (either helloworld.txt or superdupertopsecretstudyguide.txt or yet another file that I might use to test your code). The second should be the name of a file that you will use as an output file. Your method should read in the contents of the inputfile and, using the scheme described in the hints.txt file above, decode the hidden message, writing to the outputfile as it goes (or all at once when it is done depending on what you decide to use). The penny math lecture is here. """ Program: pennyMath.py Author: CS 1510 Description: Calculates the penny math value of a string. """ # Get the input string original = input("Enter a string to get its cost in penny math: ") cost = 0 Go through each character in the input string for char in original: value = ord(char) #ord() gives us the encoded number! if char>="a" and char<="z": cost = cost+(value-96) #offset the value of ord by 96 elif char>="A" and char<="Z": cost = cost+(value-64) #offset the value of ord by 64 print("The cost of",original,"is",cost) Another hint: Don't forget about while loops... Another hint: After letters - skip ahead by their pennymath value positions + 2 After numbers - skip ahead by their number + 7 positions After anything else - just skip ahead by 1 position The issue I'm having in that I cant seem to get the coding right to decode the file it comes out looking the same. This is the current code I have been using. But once I try to decrypt the message it stays the same. def pennycost(c): if c >="a" and c <="z": return ord(c)-96 elif c>="A" and c<="Z": return ord(c)-64 def decryption(inputfile,outputfile): with open(inputfile) as f: fo = open(outputfile,"w") count = 0 while True: c = f.read(1) if not c: break; if count > 0: count = count -1; continue elif c.isalpha(): count = pennycost(c) fo.write(c) elif c.isdigit(): count = int(c) fo.write(c) else: count = 6 fo.write(c) fo.close() inputfile = input("Please enter the input file name: ") outputfile = input("Plese enter the output file name(EXISTING FILE WILL BE OVER WRITTEN!): ") decryption(inputfile,outputfile)
Sort dictionary based on len(value) where value is a set
I know there are many solutions (How do I sort a dictionary by value?) to sorting a dictionary by values. However, most of those predate python 3.7's changes to dictionary. I am also aware of Fastest way to sort a python 3.7+ dictionary, which seems close to the answer I need. I have a large dictionary of keys that are ints and values that are sets of strings. I want to create a new dictionary that is sorted by the length of the set of each value. Dictionary: dict1={ '12':{'sym1', 'sym2'}, '13':{'sym1', 'sym4', 'sym5', 'sym6'}, '14':{'sym1', 'sym3'}, '15':{'sym2'}, '16':{'sym2'}, '17':{'sym2'}, '18':{'sym3', 'sym89', 'sym34', 'sym5', 'sym88'} } New sorted dictionary: >>sorted_dict1 { '18':{'sym3', 'sym89', 'sym34', 'sym5', 'sym88'}, '13':{'sym1', 'sym4', 'sym5', 'sym6'}, '12':{'sym1', 'sym2'}, '14':{'sym1', 'sym3'}, '15':{'sym2'}, '16':{'sym2'}, '17':{'sym2'} }
I think this is a very slow way to do this, but here goes: create a dictionary that has the same keys, and the value is the length of the set of the key from operator import itemgetter dict1={ '12':{'sym1', 'sym2'}, '13':{'sym1', 'sym4', 'sym5', 'sym6'}, '14':{'sym1', 'sym3'}, '15':{'sym2'}, '16':{'sym2'}, '17':{'sym2'}, '18':{'sym3', 'sym89', 'sym34', 'sym5', 'sym88'} } dict1_len ={} for k,v in dict1.items(): dict1_len.update({k:len(v)}) Sort dict1_len by the value numbers in reverse (descending). sorted_dict1_len = {k: v for k,v in sorted(dict1_len.items(), key=itemgetter(1), reverse=True)} using the keys in the order given by sorted_dict1_len, add the key and the values of that key as given by the original dict1 to sorted_dict1. sorted_dict1 = {} for k in sorted_dict1_len.keys(): print(k) sorted_dict1.update({k:dict1.get(k)}) Edit: improved answer from operator import itemgetter dict1={ '12':{'sym1', 'sym2'}, '13':{'sym1', 'sym4', 'sym5', 'sym6'}, '14':{'sym1', 'sym3'}, '15':{'sym2'}, '16':{'sym2'}, '17':{'sym2'}, '18':{'sym3', 'sym89', 'sym34', 'sym5', 'sym88'} } def len_val(tup): return len(tup[1]) # length of the value i.e. elements in set dict2 = sorted(dict1.items(), key=len_val, reverse=True) print(dict2) returns [('18', {'sym88', 'sym5', 'sym89', 'sym3', 'sym34'}), ('13', {'sym1', 'sym5', 'sym6', 'sym4'}), ('12', {'sym1', 'sym2'}), ('14', {'sym3', 'sym1'}), ('15', {'sym2'}), ('16', {'sym2'}), ('17', {'sym2'})]
decoding an ASCII character message
I have no idea what im doing I need to decode mmZ\dxZmx]Zpgy, I have an example but not sure what to do please help! If (EncryptedChar - Key < 32) then DecryptedChar = ((EncryptedChar - Key) + 127) - 32 Else DecryptedChar = (EncryptedChar - Key) the key us unknown 1-100
Use ord and chr to convert between the ordinals (ord) of the ASCII value and the and the characters (chr) they represent. Then you can loop through your string and apply the your algorithm to each. For example: import sys SecretMessage = "mmZ\dxZmx]Zpgy" Key = 88 for Letter in SecretMessage: EncryptedChar = ord(Letter) if (EncryptedChar - Key) < 32: DecryptedChar = ((EncryptedChar - Key) + 127) - 32 else: DecryptedChar = (EncryptedChar - Key) sys.stdout.write(chr(DecryptedChar)) Run this to see the output. I'll leave the exercise of finding the key value 88 up to you (hint: it involves iterations). You also appear to be missing the first letter from SecretMessage (probably a :).