I wrote a code that turns text into ASCII. but now according to the algorithm, I need to split the number into blocks m[1],m[2],m[3]...m[n] : m[i] <N. N = p * q. Here is my code below.
But when I try to split a number on a block using "while", it gives the error "TypeError: '<' not supported between instances of 'str' and 'int'" although I checked the type "str_values" and it shows "int".
It should turn out something like this: If N = 2106053, then we take a list of blocks (numbers) ['104101', '1081081', '1132119', '111114', '108100'] the same condition must be met that m[i] should not start with 0, so we have m[1] not '1041011' because then m[2] = [081081] and this is not a number. I hope you understand me :)
I don't fully understand python syntax, tell me what is my mistake and how can I write this algorithm. `
p = 1039
q = 2027
if isprime(p) and isprime(q):
N = p * q
fiN = (p - 1) * (q - 1)
else:
print('not prime')
print(N, fiN)
e = 17
d = pow(e, -1, fiN)
d1 = [N, e]
print('open - ', d1)
d2 = [N, d]
print('secret - ', d2)
text = input("enter a string to convert into ascii values: ")
ascii_values = [ord(character) for character in text]
print(ascii_values)
str_values = int(''.join(map(str, ascii_values)))
a = list(str(str_values))
print(type(str_values))
print(a)
k = 0
i=0
while k < N:
k = a[i] a[i 1]
print(k)
-Example
2106053 2102988
open - [2106053, 17]
secret - [2106053, 1360757]
enter a string to convert into ascii values: hello world
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
<class 'int'>
['1', '0', '4', '1', '0', '1', '1', '0', '8', '1', '0', '8', '1', '1', '1', '3', '2', '1', '1', '9', '1', '1', '1', '1', '1', '4', '1', '0', '8', '1', '0', '0']
`
CodePudding user response:
In this snippet, you expect somehow the a's to be integers, aren't you? while k < m: k = a[i] a[i 1]
They are not. k
and m
are initialized with integer values but after the first loop, k
contains a str
value because the list a
contains instances of str
hence the TypeError
exception.
If you must keep that list of strings, the best you can do is:
while k < m:
k = int(a[i]) int(a[i 1])