Databricks的`aes_decrypt()`无法将使用`aes_encrypt()`加密的字符串还原为原始文本。

huangapple go评论55阅读模式
英文:

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

问题

我有一些数据需要加密,我想使用内置在 Databricks 中的 aes_encrypt 函数。但是,我无法成功地使用 Databricks 中的 aes_encrypt() 和 aes_decrypt() 函数加密然后恢复原始文本。

一个示例如下。

我需要做什么才能恢复原始文本?

**查询:**

    select 
      'test' as original_text,
      aes_encrypt('test','foobarfoobarfoobarfoobar') as encrypted,
      aes_decrypt(aes_encrypt('test','foobarfoobarfoobarfoobar'), 'foobarfoobarfoobarfoobar') as decrypted

**输出:**

    original_text	encrypted										decrypted
    test			shj898e14ahsu3yNcuNiI+iaVy4ajc5NBnReC08GjIA=	dGVzdA==
英文:

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==

答案1

得分: 0

这篇帖子很有帮助:
https://docs.databricks.com/sql/language-manual/functions/aes_decrypt.html

这导致了这个解决方案:

-- 这个可行
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;
英文:

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;

huangapple
  • 本文由 发表于 2023年2月13日 23:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定