简体   繁体   中英

File is not encrypting in Aws s3 server side encryption (Customer provided Key) Android

I am trying to implement encryption in amazon with custom keys, I am providing these 3 values in the header as per the documentation mention

objectMetadata.setHeader("x-amz-server-side-encryption-customer-algorithm", ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key", key2)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key-MD5", md5)

These 3 values are required in the header but it's not encrypting files at amazon server, I am generating the customer-key and md5 key through this code

    @Throws(Exception::class)
fun encrypt(
    plaintext: ByteArray?,
    password:  CharArray,
    key: SecretKey,
    IV: ByteArray?,
    salt: ByteArray
): ByteArray? {
    val cipher = Cipher.getInstance("AES")
    val pbKeySpec = PBEKeySpec(password, salt, 1324, 256)
    val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
    val keyBytes = secretKeyFactory.generateSecret(pbKeySpec).encoded
    val keySpec = SecretKeySpec(keyBytes, "AES")
    val ivSpec = IvParameterSpec(IV)
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec)
    return cipher.doFinal(plaintext)
}
val key2 = Base64.getEncoder().encodeToString(encrypt(utf8, chr, key, iv, salt)) // This is how am calling the function and making base64 customer key

Then for md5 am using this code to create md5 key

 val md = MessageDigest.getInstance("MD5")

 Files.newInputStream(Paths.get(files[j].path)).use { `is` ->
      DigestInputStream(`is`, md).use { }
 }

 val digest: ByteArray = md.digest(files[j].path.encodeToByteArray())
 val md5 = Base64.getEncoder().encodeToString(digest)

The file is successfully uploading to the aws server but the file is not encrypted, I can't seem to figure out what's the issue

Are you adding these 3 keys in your header correctly?

objectMetadata.setHeader("x-amz-server-side-encryption-customer-algorithm", ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key", key2)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key-MD5", md5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM