Databricks aes_decrypt() not giving original text for string encrypted using aes_encrypt() - encryption

I have some data that I need to encrypt and I'd like to use the aes_encrypt function built into Databricks. However, I'm unable to successfully encrypt and then get back the original text using the aes_encrypt() and aes_decrypt() functions build into Databricks.
An example is given below.
What do I need to do to get back the original text?
Query:
select
'test' as original_text,
aes_encrypt('test','foobarfoobarfoobarfoobar') as encrypted,
aes_decrypt(aes_encrypt('test','foobarfoobarfoobarfoobar'), 'foobarfoobarfoobarfoobar') as decrypted
Output:
original_text encrypted decrypted
test shj898e14ahsu3yNcuNiI+iaVy4ajc5NBnReC08GjIA= dGVzdA==

This post was helpful:
https://docs.databricks.com/sql/language-manual/functions/aes_decrypt.html
Which leads to this solution:
-- this works
select
'test' as original_text,
base64(aes_encrypt('test','foobarfoobarfoobarfoobar')) as encrypted,
cast(aes_decrypt(unbase64(base64(aes_encrypt('test','foobarfoobarfoobarfoobar'))), 'foobarfoobarfoobarfoobar') as string) as decrypted;

Related

In R cyphr, how can I encrypt (symmetric) a string or file, save the ws, reload it and then decrypt the file or string, as simply as possible?

I'm just learning to use cyphr and doing a very simply exercise, but having a problem that involves session keys. I want to encrypt a file and then, at some later date & time, decrypt, but I ran into the problem below. I'm the only one who will be encrypting and decrypting the file, and occasionally modifying the file. Here's what happened.
library(cyphr)
# SimpFile is a simple character file of length 107.
cykey123022 <- cyphr::key_sodium(sodium::keygen()) # generate a cyphr key
encrypt_file("SimpFile.txt", cykey123022, dest = "SimpFile.enc")
# That worked.
decrypt_file("SimpFile.enc", cykey123022, dest = "SimpFile.dec")
# That worked too.
Another session: reloaded R and the workspace.
library(cyphr)
decrypt_file("SimpFile.enc", cykey123022, dest = "SimpFile.dec")
Error: Failed to decrypt key as session key has changed
The problem now is that, while I understand the purpose of the session key, I haven't been able to reset it or to do the simple task I'm trying to do. By the way, I'm happy, in this particular case, to dispense with session keys all together if I can. I would be happy to simply encrypt the file, hide the encryption key and then reload the same key at the later time in order to decrypt the file.
How can I do that most simply?
I already described what happened in the previous frame. I expected to be able to decrypt the file but couldn't. When I tried to do that, I got the following error message:
Error: Failed to decrypt key as session key has changed
Your decryption commands should probably not have the same input and output file names - you are overwriting the encrypted file each time. I don't think that's the source of the issue but worth trying decrypt_file("SimpFile.enc", cykey123022, dest = "SimpFile_2.txt")
Also, check: https://cran.r-project.org/web/packages/cyphr/vignettes/cyphr.html
"When using key_openssl, keypair_openssl, key_sodium, or keypair_sodium we generate something that can decrypt data. The objects that are returned by these functions can encrypt and decrypt data and so it is reasonable to be concerned that if these objects were themselves saved to disk your data would be compromised.
To avoid this, cyphr does not store private or symmetric keys directly in these objects but instead encrypts the sensitive keys with a cyphr-specific session key that is regenerated each time the package is loaded. This means that the objects are practically only useful within one session, and if saved with save.image (perhaps automatically at the end of a session) the keys cannot be used to decrypt data."

Invalid Header when including customer provided key to upload file to Azure Blob

I'm following this article to include customer provided key when uploading file to Azure Blob. My code is using string key instead of byte[]. I have tried different format of key such as:
Plain text string: "my key"
Base 64 AES 256 encrypted key generated from this link
When I upload file using above customer provided key, it throws "The value for one of the HTTP headers is not in the correnct format". I'm suspecting it is due to the encryption key is not included in the header.
Is my key generated in a correct approach? Or is there any configuration I have missed out?
You can use the following way to generate base 64 encrypted key when you add key as string.
string base64Encoded = "YmFzZTY0IGVuY29kZWQgc3RyaW5n";
byte[] data = System.Convert.FromBase64String(base64Encoded);
For more details, you could refer to this article.

get a value from ini file and decrypt it in powerbuilder script

Iam working in powerbuilder and i made two functions the first function called of_enc() for encrypt a string and the second function called of_dec() for decrypt, i encrypet a value in inifile by using the encryption function, the value is encrypted succssfully by using SetProfileString (ls_inifile, "Database", "DBPass", of_enc("password"))
now after that i need to decrypt the value in another window by usingof_dec() but i can't access the ecrypted value in the inifile . any idea on how to implement that?
password = ProfileString(ls_inifile, "Database", "DBPass", "")
password = of_enc(password)
*** Note that this function can only return a string which has a maximum length of 4096 characters.
i think i found what i am looking for . this statement is accessing the stored value in the inifile and working fine with the decryption function:
of_dec(ProfileString(ls_inifile, "Database", "DBPass",""))

rsa decryption data from an encrypted data

I have tried many approaches to get the decryted data. I have used the same public key and private key to encrypt the data as given in the link
Please see the attached link.
RSA encryption/decryption compatible with Javascript and PHP
Finally I got the encrpted data (I have used extjs for encrption).I need to get the decrypted data in java.
Please suggest me a solution to get the decrypted by using same keys or solution to generate random keys for encrytion and decrytion

TCL code that can encrypt and decrypt a string

I need a piece of code that defines functions which can encrypt and decrypt a piece of string. What I basically want is that the string should not be visible to third-party users, so that when the string originates in one file, it is converted to, say, an integer value using the encrypt function and then it is passed as parameter to another file. There the decrpyt function then decrypts it back and uses the string to perform actions on it.
Any suggestions or already available codes will be just fine!
Please help me out. Thanks!
Install tcllib. There are several standard encryption algorithms implemented in tcllib.
The following encryption algorithms are available:
blowfish: http://tcllib.sourceforge.net/doc/blowfish.html
aes: http://tcllib.sourceforge.net/doc/aes.html
des (including triple des): http://tcllib.sourceforge.net/doc/des.html
rc4: http://tcllib.sourceforge.net/doc/rc4.html
The des package in Tcllib should do what you want. It's pretty easy to use:
package require des
set key "12345678"; # Must be 8 bytes long
set msg "abcde"
##### ENCRYPTION
set encryptedMsg [DES::des -dir encrypt -key $key $msg]
# $encryptedMsg is a bunch of bytes; you'll want to send this around...
##### DECRYPTION
set decryptedMsg [DES::des -dir decrypt -key $key $encryptedMsg]
puts "I got '$decryptedMsg'"
Note that DES will pad the message out to a multiple of 8 bytes long.
Please visit the TCL/TK homepage e.g
here:http://wiki.tcl.tk/900
That's just one way of doing it. There will be much more, I'm sure.

Resources