AWS S3 SSE Customer Key Java vs Python - Stack Overflow

admin2025-04-18  3

I have a file uploaded to an S3 using Server Side Encryption via Lambda using JAVA Code.

We have another Lambda written in Python that needs to access this file and process it but the Same Key does not work. both codes are working to write and read their files but no exchange between them.

on Python code I am using

def generate_key_from_passphrase(passphrase):
    return hashlib.sha256(passphrase.encode("utf-32")).digest()

Tried several encoding but does not work ie,(utf-8, utf-16) and even with no value

I have a file uploaded to an S3 using Server Side Encryption via Lambda using JAVA Code.

We have another Lambda written in Python that needs to access this file and process it but the Same Key does not work. both codes are working to write and read their files but no exchange between them.

on Python code I am using

def generate_key_from_passphrase(passphrase):
    return hashlib.sha256(passphrase.encode("utf-32")).digest()

Tried several encoding but does not work ie,(utf-8, utf-16) and even with no value

Share edited Jan 30 at 14:46 Peter Cordes 368k49 gold badges717 silver badges981 bronze badges asked Jan 30 at 14:32 Samy AmerSamy Amer 214 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It's not clear what your Java code looks like but I did:

$ python3
Python 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> print (hashlib.sha256('blahblah'.encode("utf-8")).digest().hex())
40b1bbb5445fc021a312315379f4633284851e14d1db83fb0730f58872d6033b
>>>

And, in Java:

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;

public class Main {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        System.out.println(HexFormat.of().formatHex(digest.digest("blahblah".getBytes(StandardCharsets.UTF_8))));
    }
}
40b1bbb5445fc021a312315379f4633284851e14d1db83fb0730f58872d6033b

I converted to hex to be able to see the value of the byte arrays in both Python and Java.

转载请注明原文地址:http://anycun.com/QandA/1744913346a89396.html