Converting an encrypted message to string - encryption

Here is my encrypted string:
b'pN\xe89\xd8\x83x\x9d\ts\xdb\x91\x87\xb4L\x1bU\xf2\x1fQqL\rz\x8d\xbb\xd0J\x99\xb7\xc0\x8a\x17n#\xdc\x16s\xfaf\x17\x00:\x0f\x86'
How do I convert this into a String that can be written to ConfigParser
later, read this string and convert it back to the correct data type for used for encryption?
encrypted username are of type:<class 'nacl.utils.EncryptedMessage'>

Related

What is the data form of public key cryptography algorithm?

The following take C programming language and Python as an example.
RSA's key is different from AES (or the block encryption algorithm).
Specifically, AES's key is byte by byte (unsigned characters), while RSA's key is operated in the long type.
When viewing the key in Python, you can see that the data form of the key is as follows.
# RSA - Python data
PublicKey(83263512511647620840626771691498251004604648594110197508580385953996828999497, 65537)
# AES - Python data
b'12345678901234567890123456789012'
# RSA - File data
-----BEGIN RSA PUBLIC KEY-----
MCgCIQC4FXpfXETC0us2BHofL2K7BE7t4de4j/xj0KprmbJ/SQIDAQAB
-----END RSA PUBLIC KEY-----
# AES - File data
12345678901234567890123456789012
I want to know how RSA converts this string of content that looks nothing like the PublicKey in Python into a key for use.
Because the key of the block encryption algorithm can obviously know that one byte of key corresponds to one byte of data. After the RSA key is decoded by Base64, it doesn't look like the following.
b'0(\x02!\x00\xb8\x15z_\\D\xc2\xd2\xeb6\x04z\x1f/b\xbb\x04N\xed\xe1\xd7\xb8\x8f\xfcc\xd0\xaak\x99\xb2\x7fI\x02\x03\x01\x00\x01'
PublicKey(83263512511647620840626771691498251004604648594110197508580385953996828999497, 65537)
Hmm. If I still haven't expressed clearly, I'll try to be more specific in my mind.
Because RSA key stores key data in long type (64 bit), while AES key stores key data in unsigned char (8 bit).
When reading the AES key, you can directly read bytes one by one to obtain the key.
RSA needs to convert the byte code in the file to long type data. What is the process like?

Is there a way to generate same encrypted value while encrypting same text using aes 256?

I have a requirement of of encrypt and decrypt the text but each time the encryption value of same text should be generated same.
Suppose if I am encrypting a text "My Name is John". And while encrypting first time its value was generated kPddYx+eLwgK6CkJK1Vt2iGP8x8dv66YKz8YoHmfhrQ= then requirement is, if I am again encrypting the same text "My Name is John", It should generate the same encrypted string as the first one.

Do a SHA256 of a query field in Teradata

As part of a project I need to retrieve personal user information from a Teradata table.
To preserve user privacy we need to have access only on the sha256 hash of the values.
Would it be possible in Teradata to hash the value directly in the query?
I tried:
SELECT sha256(login_email) as sha_login_email
FROM tablex;
but the function sha256(binary) doesn't work with Strings.
I get an error: The string contains an untranslatable character.
whichever string is processed by the query.

Padding Encrypted result string

There is a requirement in my new job regarding encryption. We need encrypt specific data from DB and then decrypt it whenever needed. We're planning to go for DBMS_CRYPTO with AES algorithm. Now the requirement is that whatever the input string may be and whatever be its length, my encrypted string's length should be same everytime i do the encryption. For example, a 15 character string should have the length of its encrypted string same as a 24 character string. Can someone please guide me on this?

When deriving keys from passwords, should I store the key or use the key to encrypt?

The following quote has made me question what I thought I knew.
"Now consider bcrypt. It uses Blowfish to encrypt a magic string, using
a key "derived" from the password. Later, when a user enters a
password, the key is derived again, and if the ciphertext produced by
encrypting with that key matches the stored ciphertext, the user is
authenticated. The ciphertext is stored in the "password" table, but
the derived key is never stored."
I was under the assumption that you store the hash of the salted password.
Where:
- KDF = a key derivation function such as pbkdf2 or bcrypt
- salt = a unique 16 byte salt
- password = the password the user entered
- saltedPassword = the password appended to the salt
- hashedSaltedPassword = KDF(saltedPassword)
- USER = a database table where usernames and passwords are stored<br
USER.Password = hashedSaltedPassword
USER.PasswordSalt = salt
Is this correct or am I way off?
Your quote basically describes how to use bcrypt (which is a key-derivation function) as the basis of a salted hash function. That function's inputs are a password and a salt, and its output is a hash value. That hash value happens to be the ciphertext produced by encryping a constant string with a bcrypt-derived key, but you don't care about that: you're just using it as a hash value.
So yes, you do store the hash of the salted password.

Resources