Decrypt Output
Decrypt a Ciphertext returned by Bubble back into a plaintext value using your AES key.
Bubble saves secure values by encapsulating them within a Ciphertext object. To get the actual value, the user should decrypt the ciphertext using his AES key.
The SDK offers a function to decrypt ciphertext to a plaintext. The function signature is provided in Python and JavaScript languages as follows:
def decrypt(key, r, ciphertext, r2=None, ciphertext2=None)The function decrypts the Ciphertext using the AES key.
The cipher text includes two values for bit size less or equal than 128 - r and AES(r)^plaintext and four values for 256 bit size - r1 and AES(r1)^plaintextHigh, r2 and AES(r2)^plaintextLow)
Input parameters for bit size less that or equal 128:
- AES key
- r: Random value used in the encryption
- ciphertext: AES(r) ^ plaintext
Input parameters for 256 bit size:
- AES key
- r: First random value used in the encryption of the high 128 bits in the plaintext
- ciphertext: AES(r1) ^ plaintextHigh
- r2: Second random value used in the encryption of the low128 bits in the plaintext
- ciphertext2: AES(r2) ^ plaintextLow
Output:
- The decrypted plaintext
Example usage - Private ERC20
When getting the current balance, the ERC20 contract return the encrypted balance. The user should decrypt it to get his actual balance value.
Below is example demonstrating how to utilize the decrypt function in Python language for plaintext with 256 bit size:
ct_value is the result of the BalanceOf function .
def decrypt_value(ct_value, user_key):
# Split ct into four 128-bit arrays rHigh, cipherHigh, rLow and cipherLow
cipherHigh = ct_value[:BLOCK_SIZE]
rHigh = ct_value[BLOCK_SIZE:2*BLOCK_SIZE]
cipherLow = ct_value[2*BLOCK_SIZE:3*BLOCK_SIZE]
rLow = ct_value[3*BLOCK_SIZE:4*BLOCK_SIZE]
# Decrypt the cipher
decrypted_message = decrypt(user_key, rHigh, cipherHigh, rLow, cipherLow)
# Print the decrypted cipher
decrypted_value = int.from_bytes(decrypted_message, 'big')
return decrypted_value