plugmate-secure-file-io.ino
Demonstrates how to use Android 14's encrypted internal storage to save sensitive data without exposing encryption keys to the host.
PlugMate uses hardware-encrypted eMMC storage. This code uses the standard Kotlin/Android getFilesDir() API. The app must be signed and installed on the PlugMate via ADB or the internal store. Data is accessible only by the app itself due to strict system isolation. No network configuration is needed for this local operation.
plugmate-secure-file-io.ino
package com.trustkernel.plugmate.demo
import android.content.Context
import java.io.File
import java.io.FileOutputStream
import java.io.FileInputStream
import java.nio.charset.StandardCharsets
object SecureStorageDemo {
/**
* Écrit des données dans le répertoire privé de l'application.
* Le stockage eMMC est chiffré au repos par PlugOS.
*/
fun writeSecret(context: Context, filename: String, data: String) {
try {
// getFilesDir() pointe vers le sandbox chiffré de l'app
val file = File(context.filesDir, filename)
FileOutputStream(file).use { fos ->
fos.write(data.toByteArray(StandardCharsets.UTF_8))
}
println("Données écrites avec succès dans le stockage chiffré.")
} catch (e: Exception) {
e.printStackTrace()
}
}
/**
* Lit les données depuis le stockage chiffré.
*/
fun readSecret(context: Context, filename: String): String? {
val file = File(context.filesDir, filename)
if (!file.exists()) return null
return try {
FileInputStream(file).bufferedReader().use { it.readText() }
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}