Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
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
ai-tech
dify
Commits
a0ec7de0
Unverified
Commit
a0ec7de0
authored
Feb 08, 2024
by
takatost
Committed by
GitHub
Feb 08, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean: remove no-use ecc_aes.py (#2426)
parent
14a19a3d
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
0 additions
and
82 deletions
+0
-82
ecc_aes.py
api/libs/ecc_aes.py
+0
-82
No files found.
api/libs/ecc_aes.py
deleted
100644 → 0
View file @
14a19a3d
from
Crypto.Cipher
import
AES
from
Crypto.Hash
import
SHA256
from
Crypto.PublicKey
import
ECC
from
Crypto.Util.Padding
import
pad
,
unpad
class
ECC_AES
:
def
__init__
(
self
,
curve
=
'P-256'
):
self
.
curve
=
curve
self
.
_aes_key
=
None
self
.
_private_key
=
None
def
_derive_aes_key
(
self
,
ecc_key
,
nonce
):
if
not
self
.
_aes_key
:
hasher
=
SHA256
.
new
()
hasher
.
update
(
ecc_key
.
export_key
(
format
=
'DER'
)
+
nonce
.
encode
())
self
.
_aes_key
=
hasher
.
digest
()[:
32
]
return
self
.
_aes_key
def
generate_key_pair
(
self
):
private_key
=
ECC
.
generate
(
curve
=
self
.
curve
)
public_key
=
private_key
.
public_key
()
pem_private
=
private_key
.
export_key
(
format
=
'PEM'
)
pem_public
=
public_key
.
export_key
(
format
=
'PEM'
)
return
pem_private
,
pem_public
def
load_private_key
(
self
,
private_key_pem
):
self
.
_private_key
=
ECC
.
import_key
(
private_key_pem
)
self
.
_aes_key
=
None
def
encrypt
(
self
,
text
,
nonce
):
if
not
self
.
_private_key
:
raise
ValueError
(
"Private key not loaded"
)
# Generate AES key using ECC private key and nonce
aes_key
=
self
.
_derive_aes_key
(
self
.
_private_key
,
nonce
)
# Encrypt data using AES key
cipher
=
AES
.
new
(
aes_key
,
AES
.
MODE_ECB
)
padded_text
=
pad
(
text
.
encode
(),
AES
.
block_size
)
ciphertext
=
cipher
.
encrypt
(
padded_text
)
return
ciphertext
def
decrypt
(
self
,
ciphertext
,
nonce
):
if
not
self
.
_private_key
:
raise
ValueError
(
"Private key not loaded"
)
# Generate AES key using ECC private key and nonce
aes_key
=
self
.
_derive_aes_key
(
self
.
_private_key
,
nonce
)
# Decrypt data using AES key
cipher
=
AES
.
new
(
aes_key
,
AES
.
MODE_ECB
)
padded_plaintext
=
cipher
.
decrypt
(
ciphertext
)
plaintext
=
unpad
(
padded_plaintext
,
AES
.
block_size
)
return
plaintext
.
decode
()
if
__name__
==
'__main__'
:
ecc_aes
=
ECC_AES
()
# Generate key pairs for the user
private_key
,
public_key
=
ecc_aes
.
generate_key_pair
()
ecc_aes
.
load_private_key
(
private_key
)
nonce
=
"THIS-IS-USER-ID"
print
(
private_key
)
# Encrypt a message
message
=
"Hello, this is a secret message!"
encrypted_message
=
ecc_aes
.
encrypt
(
message
,
nonce
)
print
(
f
"Encrypted message: {encrypted_message.hex()}"
)
# Decrypt the message
decrypted_message
=
ecc_aes
.
decrypt
(
encrypted_message
,
nonce
)
print
(
f
"Decrypted message: {decrypted_message}"
)
# Check if the original message and decrypted message are the same
assert
message
==
decrypted_message
,
"Original message and decrypted message do not match"
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