|
|
|
@ -17,19 +17,61 @@
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
@file:Suppress("UnstableApiUsage")
|
|
|
|
|
|
|
|
|
|
package org.isoron.uhabits.utils
|
|
|
|
|
|
|
|
|
|
import android.util.*
|
|
|
|
|
import com.google.common.io.*
|
|
|
|
|
import java.io.*
|
|
|
|
|
import java.nio.*
|
|
|
|
|
import java.nio.charset.StandardCharsets.*
|
|
|
|
|
import java.util.zip.*
|
|
|
|
|
import javax.crypto.*
|
|
|
|
|
import javax.crypto.spec.*
|
|
|
|
|
|
|
|
|
|
fun ByteArray.encrypt(key: String): ByteArray {
|
|
|
|
|
val keySpec = SecretKeySpec(Base64.decode(key, Base64.DEFAULT), "AES")
|
|
|
|
|
/**
|
|
|
|
|
* Encryption key which can be used with [File.encryptToString], [String.decryptToFile],
|
|
|
|
|
* [ByteArray.encrypt] and [ByteArray.decrypt].
|
|
|
|
|
*
|
|
|
|
|
* To randomly generate a new key, use [EncryptionKey.generate]. To load a key from a
|
|
|
|
|
* Base64-encoded string, use [EncryptionKey.fromBase64].
|
|
|
|
|
*/
|
|
|
|
|
class EncryptionKey private constructor(
|
|
|
|
|
val base64: String,
|
|
|
|
|
val secretKey: SecretKey,
|
|
|
|
|
) {
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
fun fromBase64(base64: String): EncryptionKey {
|
|
|
|
|
val keySpec = SecretKeySpec(Base64.decode(base64, Base64.DEFAULT), "AES")
|
|
|
|
|
return EncryptionKey(base64, keySpec)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun fromSecretKey(spec: SecretKey): EncryptionKey {
|
|
|
|
|
val base64 = Base64.encodeToString(spec.encoded, Base64.DEFAULT).trim()
|
|
|
|
|
return EncryptionKey(base64, spec)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun generate(): EncryptionKey {
|
|
|
|
|
try {
|
|
|
|
|
val generator = KeyGenerator.getInstance("AES").apply { init(256) }
|
|
|
|
|
return fromSecretKey(generator.generateKey())
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
throw RuntimeException(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encrypts the byte stream using the provided symmetric encryption key.
|
|
|
|
|
*
|
|
|
|
|
* The initialization vector (16 bytes) is prepended to the cipher text. To decrypt the result, use
|
|
|
|
|
* [ByteArray.decrypt], providing the same key.
|
|
|
|
|
*/
|
|
|
|
|
fun ByteArray.encrypt(key: EncryptionKey): ByteArray {
|
|
|
|
|
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec)
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, key.secretKey)
|
|
|
|
|
val encrypted = cipher.doFinal(this)
|
|
|
|
|
return ByteBuffer
|
|
|
|
|
.allocate(16 + encrypted.size)
|
|
|
|
@ -38,48 +80,50 @@ fun ByteArray.encrypt(key: String): ByteArray {
|
|
|
|
|
.array()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun ByteArray.decrypt(key: String): ByteArray {
|
|
|
|
|
/**
|
|
|
|
|
* Decrypts a byte stream generated by [ByteArray.encrypt].
|
|
|
|
|
*/
|
|
|
|
|
fun ByteArray.decrypt(key: EncryptionKey): ByteArray {
|
|
|
|
|
val buffer = ByteBuffer.wrap(this)
|
|
|
|
|
val iv = ByteArray(16)
|
|
|
|
|
buffer.get(iv)
|
|
|
|
|
val encrypted = ByteArray(buffer.remaining())
|
|
|
|
|
buffer.get(encrypted)
|
|
|
|
|
val keySpec = SecretKeySpec(Base64.decode(key, Base64.DEFAULT), "AES")
|
|
|
|
|
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, IvParameterSpec(iv))
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, key.secretKey, IvParameterSpec(iv))
|
|
|
|
|
return cipher.doFinal(encrypted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun String.encrypt(key: String): String {
|
|
|
|
|
return Base64.encodeToString(this.toByteArray().encrypt(key), Base64.DEFAULT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun String.decrypt(key: String): String {
|
|
|
|
|
return String(Base64.decode(this, Base64.DEFAULT).decrypt(key), UTF_8)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun String.decryptToFile(key: String, output: File)
|
|
|
|
|
{
|
|
|
|
|
val outputStream = FileOutputStream(output)
|
|
|
|
|
output.writeBytes(Base64.decode(this, Base64.DEFAULT).decrypt(key))
|
|
|
|
|
outputStream.close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun File.encryptToString(key: String): String {
|
|
|
|
|
val bytes = ByteArray(this.length().toInt())
|
|
|
|
|
val inputStream = FileInputStream(this)
|
|
|
|
|
inputStream.read(bytes)
|
|
|
|
|
inputStream.close()
|
|
|
|
|
return Base64.encodeToString(bytes.encrypt(key), Base64.DEFAULT)
|
|
|
|
|
/**
|
|
|
|
|
* Takes a string produced by [File.encryptToString], decodes it with Base64, decompresses it with
|
|
|
|
|
* gzip, decrypts it with the provided key, then writes the output to the specified file.
|
|
|
|
|
*/
|
|
|
|
|
fun String.decryptToFile(key: EncryptionKey, output: File) {
|
|
|
|
|
val bytes = Base64.decode(this, Base64.DEFAULT).decrypt(key)
|
|
|
|
|
ByteArrayInputStream(bytes).use { bytesInputStream ->
|
|
|
|
|
GZIPInputStream(bytesInputStream).use { gzipInputStream ->
|
|
|
|
|
FileOutputStream(output).use { fileOutputStream ->
|
|
|
|
|
ByteStreams.copy(gzipInputStream, fileOutputStream)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun generateEncryptionKey(): String {
|
|
|
|
|
return try {
|
|
|
|
|
val keygen = KeyGenerator.getInstance("AES")
|
|
|
|
|
keygen.init(256)
|
|
|
|
|
val key = keygen.generateKey()
|
|
|
|
|
Base64.encodeToString(key.encoded, Base64.DEFAULT).trim()
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
throw RuntimeException(e)
|
|
|
|
|
/**
|
|
|
|
|
* Compresses the file with gzip, encrypts it using the the provided key, then returns a string
|
|
|
|
|
* containing the Base64-encoded cipher bytes.
|
|
|
|
|
*
|
|
|
|
|
* To decrypt and decompress the cipher text back into a file, use [String.decryptToFile].
|
|
|
|
|
*/
|
|
|
|
|
fun File.encryptToString(key: EncryptionKey): String {
|
|
|
|
|
ByteArrayOutputStream().use { bytesOutputStream ->
|
|
|
|
|
FileInputStream(this).use { inputStream ->
|
|
|
|
|
GZIPOutputStream(bytesOutputStream).use { gzipOutputStream ->
|
|
|
|
|
ByteStreams.copy(inputStream, gzipOutputStream)
|
|
|
|
|
val bytes = bytesOutputStream.toByteArray()
|
|
|
|
|
return Base64.encodeToString(bytes.encrypt(key), Base64.DEFAULT)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|