Decrypted image starts good but becomes gray - python-3.x

I am working on a personal project of mine and was wondering how I can fix my issue.
Here is a piece of code I am working on:
f = open('sample.jpeg','rb')
choice = int(input("-> "))
mutableBytes = bytearray(f.read())
f.close()
print(str(mutableBytes) + "SAMPLE")
if choice == 1:
for i in range(len(mutableBytes)):
if mutableBytes[i] < 255:
mutableBytes[i] = mutableBytes[i] + 1
f.close()
print(str(mutableBytes) + "ENCRYPTED")
f = open('samplecopy.jpeg','wb+')
f.write(mutableBytes)
else:
f = open('samplecopy.jpeg','rb')
mutableBytes = bytearray(f.read())
f.close()
for i in range(len(mutableBytes)):
if mutableBytes[i] > 0 and mutableBytes[i]<255:
mutableBytes[i] = mutableBytes[i] - 1
f = open('samplecopy.jpeg','wb+')
f.write(mutableBytes)
print(str(mutableBytes) + "Decrypted")
This should in theory get a picture and encrypt it, after decrypt it. I printed all the bytes and I looked for changes but it looks the same.
Here is the comparison: https://www.diffchecker.com/vTtzGe4O
And here is the image I get vs the original:
(the bottom one is the one I get after decrypting).

Related

Package functions on strings using GPU in Python - Generating addresses from private keys

Problem: I am trying to convert a very big list (many millions) of private keys (hexadecimal format, stored in a list of strings) to addresses. Can this be run on the GPU?
I have tried looking for resources on how to adapt my code to a GPU/CUDA-friendly version. However, I've found that most examples online are for pure math operations on a list of ints or floats. Also, the function where the 'processing' is defined is also entirely re-written, and does not use functions from packages (other than those already supported by numpy etc.).
Is there a way to make the [private key -> public key -> address] process GPU-friendly, and can string operations be carried out on a GPU in the first place?
The following is what I have for my serial CPU version for Python3.x:
import codecs
import hashlib
import ecdsa
def get_pub_keys(priv_key):
private_hex = codecs.decode(priv_key, 'hex')
key = ecdsa.SigningKey.from_string(private_hex, curve=ecdsa.SECP256k1).verifying_key
key_bytes = key.to_string()
key_hex = codecs.encode(key_bytes, 'hex')
public_key_uncompressed = b'04' + key_hex
key_string = key_hex.decode('utf-8')
last_byte = int(key_string[-1], 16)
half_len = len(key_hex) // 2
key_half = key_hex[:half_len]
bitcoin_byte = b'02' if last_byte % 2 == 0 else b'03'
public_key_compressed = bitcoin_byte + key_half
return public_key_uncompressed, public_key_compressed
def public_to_address(public_key):
public_key_bytes = codecs.decode(public_key, 'hex')
# Run SHA256 for the public key
sha256_bpk = hashlib.sha256(public_key_bytes)
sha256_bpk_digest = sha256_bpk.digest()
# Run ripemd160 for the SHA256
ripemd160_bpk = hashlib.new('ripemd160')
ripemd160_bpk.update(sha256_bpk_digest)
ripemd160_bpk_digest = ripemd160_bpk.digest()
ripemd160_bpk_hex = codecs.encode(ripemd160_bpk_digest, 'hex')
# Add network byte
network_byte = b'00'
network_bitcoin_public_key = network_byte + ripemd160_bpk_hex
network_bitcoin_public_key_bytes = codecs.decode(network_bitcoin_public_key, 'hex')
# Double SHA256 to get checksum
sha256_nbpk = hashlib.sha256(network_bitcoin_public_key_bytes)
sha256_nbpk_digest = sha256_nbpk.digest()
sha256_2_nbpk = hashlib.sha256(sha256_nbpk_digest)
sha256_2_nbpk_digest = sha256_2_nbpk.digest()
sha256_2_hex = codecs.encode(sha256_2_nbpk_digest, 'hex')
checksum = sha256_2_hex[:8]
# Concatenate public key and checksum to get the address
address_hex = (network_bitcoin_public_key + checksum).decode('utf-8')
wallet = base58(address_hex)
return wallet
def base58(address_hex):
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
b58_string = ''
# Get the number of leading zeros and convert hex to decimal
leading_zeros = len(address_hex) - len(address_hex.lstrip('0'))
# Convert hex to decimal
address_int = int(address_hex, 16)
# Append digits to the start of string
while address_int > 0:
digit = address_int % 58
digit_char = alphabet[digit]
b58_string = digit_char + b58_string
address_int //= 58
# Add '1' for each 2 leading zeros
ones = leading_zeros // 2
for one in range(ones):
b58_string = '1' + b58_string
return b58_string
def get_addresses(i):
key1,key2 = get_pub_keys(i)
add1 = public_to_address(key1)
add2 = public_to_address(key2)
return add1, add2
filename = 'bunchOfHexKeys.txt'
with open(filename, 'r') as f:
hexKeys = f.read().splitlines()
addresses = []
for i in hexKeys:
addresses.append(get_addresses(i))
As can be seen, I'm using many functions from the 3 imported packages. So far the only way I can see is to rewrite those. Is there another way?
The size of hexKeys isn't an issue for the GPU cache size, as I can just adjust the input list as needed.

Why is this specific if condition never executes ? [if mid < k <= right:]

Im trying to solve a specific leetcode problem and but a particular if else block never executes in my code and I cant figure why. Here is the code. I'm new to python and I think i'm making a noob mistake but I just figure what it is.
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
left, right = 1, len(nums) - 1
while left < right :
mid = left + (right-left)//2
count = 0
print("l,r -->" + str(left) + ',' + str(right))
print("mid -->" + str(mid))
for k in nums :
if mid < k <= right: # this block never executes.
print(k)
count += 1
print("count -->" + str(count))
if count > right -mid:
left = mid + 1
else :
right = mid
return right
For one thing, this
if mid < k <= right: # this block never executes.
is not doing what you think it is -- instead, you want
if mid < k and k <= right:

I want to be able to restart my program after a user input error

I am making a program using the python module pypokedex, to make a program that will ask for a pokemon's name, and the generation, and pull up a https://www.serebii.net/ page about it. But, if the user spells a pokemon incorrectly, it just errors and shuts down the program. I want it to restart the program.
I also have it so that if the code succeeded, it will still loop, the problem is when it fails, (again, this is only a problem if the user spells a name wrong)
the reason this:Asking the user for input until they give a valid response doesn't work is because I need to have it be certain words, not a range of numbers.
import pypokedex
import webbrowser
while 1==1:
name = input("What pokemon are you searching for? ")
p = pypokedex.get(name = name)
zero = 3-len(str(p.dex))
if(zero == 2):
num = "00" + str(p.dex)
if(zero == 1):
num = "0" +str(p.dex)
if(zero == 0):
num = str(p.dex)
ngen = input("What generation number are you looking for? ")
if(ngen == str(1)):
gen = "pokedex"
if(ngen == str(2)):
gen ="pokedex-gs"
if(ngen == str(3)):
gen = "pokedex-rs"
if(ngen == str(4)):
gen = "pokedex-dp"
if(ngen == str(5)):
gen = "pokedex-bw"
if(ngen == str(6)):
gen = "pokedex-xy"
if(ngen == str(7)):
gen = "pokedex-sm"
url = "https://www.serebii.net/" + str(gen) + "/" + str(num) + ".shtml"\
webbrowser.open(url , 2)

Better bit operation for Python

I am generating some gradient pic with 10 bit data, and I have to mix it to 16 bit for compression purpose. I use this code but it's very slow. Is there any better way to do it?
def gradient_pic(num,f):
pic = np.zeros(2580,dtype= np.uint8)
pixvalue = np.random.randint(0,50,size=1,dtype=np.uint8)[0]
for i in range (0,1544):
for chunk in range(0,258):
pic[chunk*5] = (pixvalue<<6)+((pixvalue+1)>>4)
pic[chunk*5+1] = (((pixvalue+1)&15)<<12) + ((pixvalue+2)<<2)+ ((pixvalue+3)>>8)
pic[chunk*5+2] = (((pixvalue+3)&255)<<8) + ((pixvalue+4)>>2)
pic[chunk*5+3] = (((pixvalue+4)&3)<<14) + ((pixvalue+5)<<4)+ ((pixvalue+6)>>6)
pic[chunk*5+4] = (((pixvalue+6)&63)<<10) + pixvalue+7
pixvalue = (pixvalue+8)%1023
f.write(pic)
print(num)

How to fix a subtraction operation in Python

I am trying a simple subtraction operation: water_to_add should be dna_to_add minus final_volume which is 60.0. However the results for dna_to_add and water_to_add have been the same. Why my "minus" operation for water_to_add is not working?
BTW, dna_to_add is being calculated correctly.
concentration_ng_ul = input("Enter Concentration (ng/ul): ")
size = input("Enter Size (bp): ")
c= float(concentration_ng_ul)
s = float(size)
c_ug_ul = c/1000.0
pmol_DNA_ul = c_ug_ul*1515.15*1/s
fmol_DNA_ul = pmol_DNA_ul/1000.0
fmol_DNA_goal = 20.0
final_volume = 60.0
dna_to_add = (fmol_DNA_goal)*(final_volume/fmol_DNA_ul)
print('DNA_ul',dna_to_add)
water_to_add = (dna_to_add)-(final_volume)
print('Water_ul',water_to_add)

Resources