Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
15ac6938
Commit
15ac6938
authored
Jun 17, 2026
by
liuwenhui
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/20260615-add-field-encryption' into 'master'
fix: AES UDF 密钥派生方式对齐 StarRocks 内置函数(null 填充到 16 字节) See merge request
!13
parents
66152b8a
f102982c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
12 deletions
+20
-12
AesDecrypt.java
...tion/src/main/java/org/dinky/function/udf/AesDecrypt.java
+10
-6
AesEncrypt.java
...tion/src/main/java/org/dinky/function/udf/AesEncrypt.java
+10
-6
No files found.
dinky-function/src/main/java/org/dinky/function/udf/AesDecrypt.java
View file @
15ac6938
...
@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
...
@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
javax.crypto.spec.SecretKeySpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.security.MessageDigest
;
import
java.util.Base64
;
import
java.util.Base64
;
/**
/**
...
@@ -17,17 +17,21 @@ public class AesDecrypt extends ScalarFunction {
...
@@ -17,17 +17,21 @@ public class AesDecrypt extends ScalarFunction {
private
static
final
String
ALGORITHM
=
"AES/ECB/PKCS5Padding"
;
private
static
final
String
ALGORITHM
=
"AES/ECB/PKCS5Padding"
;
// ★★★ 必须和 AesEncrypt 里的密钥一致 ★★★
// ★★★ 必须和 AesEncrypt 里的密钥一致 ★★★
private
static
final
String
SECRET_KEY
=
"
your_secret_key_here
"
;
private
static
final
String
SECRET_KEY
=
"
my_secret_key
"
;
public
String
eval
(
String
encryptedValue
)
{
public
String
eval
(
String
encryptedValue
)
{
if
(
encryptedValue
==
null
)
{
return
eval
(
encryptedValue
,
SECRET_KEY
);
}
public
String
eval
(
String
encryptedValue
,
String
secretKey
)
{
if
(
encryptedValue
==
null
||
secretKey
==
null
)
{
return
null
;
return
null
;
}
}
try
{
try
{
MessageDigest
sha256
=
MessageDigest
.
getInstance
(
"SHA-256"
);
// null-pad key to 16 bytes (AES-128), matching StarRocks built-in behavior
byte
[]
hash
=
sha256
.
digest
(
SECRET_KEY
.
getBytes
(
StandardCharsets
.
UTF_8
)
);
byte
[]
rawKey
=
secretKey
.
getBytes
(
StandardCharsets
.
UTF_8
);
byte
[]
keyBytes
=
new
byte
[
16
];
byte
[]
keyBytes
=
new
byte
[
16
];
System
.
arraycopy
(
hash
,
0
,
keyBytes
,
0
,
16
);
System
.
arraycopy
(
rawKey
,
0
,
keyBytes
,
0
,
Math
.
min
(
rawKey
.
length
,
16
)
);
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
keyBytes
,
"AES"
);
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
keyBytes
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHM
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHM
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
);
...
...
dinky-function/src/main/java/org/dinky/function/udf/AesEncrypt.java
View file @
15ac6938
...
@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
...
@@ -5,7 +5,7 @@ import org.apache.flink.table.functions.ScalarFunction;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
javax.crypto.spec.SecretKeySpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.security.MessageDigest
;
import
java.util.Base64
;
import
java.util.Base64
;
/**
/**
...
@@ -17,17 +17,21 @@ public class AesEncrypt extends ScalarFunction {
...
@@ -17,17 +17,21 @@ public class AesEncrypt extends ScalarFunction {
private
static
final
String
ALGORITHM
=
"AES/ECB/PKCS5Padding"
;
private
static
final
String
ALGORITHM
=
"AES/ECB/PKCS5Padding"
;
// ★★★ 在这里改你的密钥 ★★★
// ★★★ 在这里改你的密钥 ★★★
private
static
final
String
SECRET_KEY
=
"
your_secret_key_here
"
;
private
static
final
String
SECRET_KEY
=
"
my_secret_key
"
;
public
String
eval
(
String
value
)
{
public
String
eval
(
String
value
)
{
if
(
value
==
null
)
{
return
eval
(
value
,
SECRET_KEY
);
}
public
String
eval
(
String
value
,
String
secretKey
)
{
if
(
value
==
null
||
secretKey
==
null
)
{
return
null
;
return
null
;
}
}
try
{
try
{
MessageDigest
sha256
=
MessageDigest
.
getInstance
(
"SHA-256"
);
// null-pad key to 16 bytes (AES-128), matching StarRocks built-in behavior
byte
[]
hash
=
sha256
.
digest
(
SECRET_KEY
.
getBytes
(
StandardCharsets
.
UTF_8
)
);
byte
[]
rawKey
=
secretKey
.
getBytes
(
StandardCharsets
.
UTF_8
);
byte
[]
keyBytes
=
new
byte
[
16
];
byte
[]
keyBytes
=
new
byte
[
16
];
System
.
arraycopy
(
hash
,
0
,
keyBytes
,
0
,
16
);
System
.
arraycopy
(
rawKey
,
0
,
keyBytes
,
0
,
Math
.
min
(
rawKey
.
length
,
16
)
);
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
keyBytes
,
"AES"
);
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
keyBytes
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHM
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHM
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment