cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Apply Encryption Function to a Specific Column

weilin0323
New Contributor III

Hello!

I would like to apply a function to encrypt a specific column. The UDF is as follows:

 

DROP FUNCTION IF EXISTS EncryptColumn;
CREATE FUNCTION EncryptColumn (key_name STRING, encryptcolumn STRING) 
RETURN base64(aes_encrypt(encryptcolumn, key_name, 'GCM', 'DEFAULT', 'Some AAD'))

 

 

The parameter 'key_name' is the encryption key, and 'encryptcolumn' is the column to be encrypted.
I tried using the following:

 

ALTER TABLE table_name 
ALTER COLUMN column_name 
SET MASK EncryptColumn(key_name, column_name)

 

 

However, this approach seems to accept only one parameter.
weilin0323_0-1737526512566.png
Therefore, I would like to ask if there is any way to apply a function to encrypt a column in the Databricks UC architecture?

Thank you for your response!

1 ACCEPTED SOLUTION

Accepted Solutions

MadhuB
Contributor

Hi @weilin0323 

Have you tried the built-in function? Refer to below encryption code snippet.

create table if not exists catalog.schema.testencryption(
  id int,
  name string,
  tss string
);

insert into catalog.schema.testencryption
values (1,'sam','123-123-123'),
(2,'john','456-456-456'),
(3,'pat','789-789-789');

SELECT
  id,
  name,
  base64(aes_encrypt(tss, 'sample_key_16by.', 'GCM')) AS encrypted_tss
FROM
  catalog.schema.testencryption;

without encryption:

MadhuB_1-1737568383906.png

with encryption:

MadhuB_0-1737568365714.png

Please let me know if there is anything else. Otherwise, please mark it as a solution.

View solution in original post

2 REPLIES 2

MadhuB
Contributor

Hi @weilin0323 

Have you tried the built-in function? Refer to below encryption code snippet.

create table if not exists catalog.schema.testencryption(
  id int,
  name string,
  tss string
);

insert into catalog.schema.testencryption
values (1,'sam','123-123-123'),
(2,'john','456-456-456'),
(3,'pat','789-789-789');

SELECT
  id,
  name,
  base64(aes_encrypt(tss, 'sample_key_16by.', 'GCM')) AS encrypted_tss
FROM
  catalog.schema.testencryption;

without encryption:

MadhuB_1-1737568383906.png

with encryption:

MadhuB_0-1737568365714.png

Please let me know if there is anything else. Otherwise, please mark it as a solution.

weilin0323
New Contributor III

Hi @MadhuB,

The method you provided is feasible, and I later finded other ways to apply UDF:

UPDATE table_name 
SET column_name = EncryptColumn(key_name, column_name)

 Thank you!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group