Let's say, the original string is:

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

Now, all the instances of 'D6' were replaced with '9F' in above string by doing:

b = a.replace('D6','9F')

b = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DC9F1457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCF9FF9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

Now, let's say, we only have the value of b and not a. We need to perform some operations on b to retrieve the original value of a.

Also, we know that the value of 'a' should satisfy a certain mathematical condition.

The mathematical condition is:

number z should be divisible by a (z % a == 0)

z = 55057004365075793824891923502198296150348187500859129529014955509148421282041969078213265169463529503768779794209446773790749529176461595867792548236095966024387560672845152234957439383409540755826755640123124159246487058454615922008741879614211920551517049373314503998980825185719370304183623398662036133862488876163410866971729000216470924616148028986990798495248878127793311548452974671645100371499570058070179424193067736979204502413302335974105838586819414807952974885796840178274113497125765593996690493177955553456655538977929256055738007112424150644005452979891672942537126552535517394691741201589304958975238

We need to leverage this mathematical property of 'a' to restore it from b.

I understand that we cannot use the replace() function on b to get back 'a' because some of the original instances of '9F' might get replaced.

I think we need to perform replace operation in different positions till we satisfy that condition.

For the given string we have:

b.count('9F')
6

So, we would have to replace '9F' with 'D6' in different combinations of positions till we get back 'a' satisfying the mathematical condition, 'C'.

I did something like:

count = b.count('9F')

for i in range(1,count+1):
    print "trying: %d" %(i)
    tmp = b.replace('9F','D6',i)
    num = int(tmp, 16)

    if z % num == True:
        print num

This seems to be a problem related to permutations and combinations.

2 Answers

1
Community On

You won't retrieve your original string back after these two operations, because you probably have "9F" in your original string, too:

test = "D69F"

changed = test.replace("D6", "9F")
print(changed)
# 9F9F

undo_change = changed.replace("9F", "D6")
print(undo_change)
# D6D6

If it don't interfere with your intermediate step you can mask your replacement, e.g. with test.replace("D6", "ยง9F").

3
Devesh Kumar Singh On

You already have 4 instances of 9F in your original string. Those are causing the wierd behaviour

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

print(a.count('9F'))
#4

Otherwise the string.replace works perfectly

a = 'hello'
b = a.replace('l','a')
print(b)
#heaao
c = b.replace('a','l')
print(c)
#hello
print( a == c)
#True

Gives you True (Convert l to a, and then a to l)

Possible solution, replace existing 9F to something not present in the string,say XY, then use it in replacing back as well

a = 'DA115792C339A5E674416AB0559BF5CB8D38E88B1E71F4DFAE896EBD2D13ABB13FFB1DC687DCF0C270A8BF4861D2E6D26E74DC81C18EB53FD5C52AA691A8F16BDA4E1EB8009B00DCD61457E34C438F23EF3D1FD905CF689793CC1E02E1ECB6778A1E2720D416AC432959A8A3B20B43525A856C97E4D404065D1D30ADC74D012B27D00B0029AD3940CB2C9F2AB6C01D430F3A58584C5DB6C98839579AF2A8F90A5D80B5097D547105DF2D9F9485E9F2CCFD6F9ECDDBD562FE09EA81C19B18482BD483AEBAB8481EE208887909DDAE826629538F36E6A50CEECBF3462E9FFBDAC6363F3A9A56F31081EBF28AD0FCF288B0DB8CB44735B9D7E6D193D55C90767E83'

#Replace 9F to XY, then D6 to 9F
b = a.replace('9F','XY').replace('D6','9F')
#Replace 9F to D6, then XY to 9F
c = b.replace('9F', 'D6').replace('XY', '9F')

print(a == c)
#True