Skip to content

Commit

Permalink
added option to choose Aes strength #25
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Jan 24, 2024
1 parent c1a8b4b commit 8b7bd22
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
43 changes: 41 additions & 2 deletions app/src/main/java/com/wirelessalien/zipxtract/CreateZipFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.provider.OpenableColumns
import android.system.ErrnoException
import android.system.OsConstants
import android.text.InputType
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -58,6 +59,8 @@ import kotlinx.coroutines.withContext
import net.lingala.zip4j.ZipFile
import net.lingala.zip4j.exception.ZipException
import net.lingala.zip4j.model.ZipParameters
import net.lingala.zip4j.model.enums.AesKeyStrength
import net.lingala.zip4j.model.enums.AesVersion
import net.lingala.zip4j.model.enums.CompressionLevel
import net.lingala.zip4j.model.enums.CompressionMethod
import net.lingala.zip4j.model.enums.EncryptionMethod
Expand Down Expand Up @@ -607,6 +610,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
outArchive.setLevel(level)
outArchive.setSolid(solid)
outArchive.setThreadCount(thread)
outArchive.setHeaderEncryption(true)

val fileToArchive = cacheFile!!

Expand Down Expand Up @@ -740,6 +744,8 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
outArchive.setLevel(level)
outArchive.setSolid(solid)
outArchive.setThreadCount(thread)
outArchive.setHeaderEncryption(true)


outArchive.createArchive(RandomAccessFileOutStream(raf), filesToArchive.size,
object : IOutCreateCallback<IOutItem7z>, ICryptoGetTextPassword,
Expand Down Expand Up @@ -872,6 +878,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
val selectedCompressionMethod = getSavedCompressionMethod()
val selectedCompressionLevel = getSavedCompressionLevel()
val selectedEncryptionMethod = getSavedEncryptionMethod()
val selectedEncryptionStrength = getSavedEncryptionStrength()

val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext())
alertDialogBuilder.setTitle(getString(R.string.enter_password))
Expand Down Expand Up @@ -909,7 +916,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
// User entered a password, proceed to create the password-protected zip
CoroutineScope(Dispatchers.Main).launch {
showProgressBar(true)
createEncryptedZipMFiles(password, outputFileName, selectedCompressionMethod, selectedCompressionLevel, selectedEncryptionMethod)
createEncryptedZipMFiles(password, outputFileName, selectedCompressionMethod, selectedCompressionLevel, selectedEncryptionMethod, selectedEncryptionStrength)
showProgressBar(false)
}
} else {
Expand Down Expand Up @@ -951,6 +958,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
zipParameters.compressionMethod = compressionMethod
zipParameters.compressionLevel = compressionLevel
zipParameters.isEncryptFiles = false
zipParameters.aesKeyStrength = AesKeyStrength.KEY_STRENGTH_192

val tempZipFile = File.createTempFile("tempZip", ".zip")
val zipFile = ZipFile(tempZipFile)
Expand Down Expand Up @@ -1026,14 +1034,15 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
}
}

private suspend fun createEncryptedZipMFiles(password: String, outputFileName: String, compressionMethod: CompressionMethod, compressionLevel: CompressionLevel, encryptionMethod: EncryptionMethod) {
private suspend fun createEncryptedZipMFiles(password: String, outputFileName: String, compressionMethod: CompressionMethod, compressionLevel: CompressionLevel, encryptionMethod: EncryptionMethod, encryptionStrength: AesKeyStrength) {
withContext(Dispatchers.IO) {

val zipParameters = ZipParameters()
zipParameters.compressionMethod = compressionMethod
zipParameters.compressionLevel = compressionLevel
zipParameters.isEncryptFiles = true
zipParameters.encryptionMethod = encryptionMethod
zipParameters.aesKeyStrength = encryptionStrength

val tempZipFile = File.createTempFile("tempZip", ".zip")
val zipFile = ZipFile(tempZipFile)
Expand Down Expand Up @@ -1119,6 +1128,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
val selectedCompressionMethod = getSavedCompressionMethod()
val selectedCompressionLevel = getSavedCompressionLevel()
val selectedEncryptionMethod = getSavedEncryptionMethod()
val selectedEncryptionStrength = getSavedEncryptionStrength()
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.enter_password))
val input = EditText(requireContext())
Expand All @@ -1142,6 +1152,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
parameters.compressionMethod = selectedCompressionMethod
parameters.compressionLevel = selectedCompressionLevel
parameters.fileNameInZip = getZipFileName(selectedFileUri)
parameters.aesKeyStrength = selectedEncryptionStrength

val tempZipFile = File.createTempFile("tempZipP", ".zip")

Expand Down Expand Up @@ -1310,6 +1321,7 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
val compressionMethodInput = view.findViewById<Spinner>(R.id.compression_method_input)
val compressionLevelInput = view.findViewById<Spinner>(R.id.compression_level_input)
val encryptionMethodInput = view.findViewById<Spinner>(R.id.encryption_method_input)
val encryptionStrengthInput = view.findViewById<Spinner>(R.id.encryption_strength_input)

val compressionMethods = CompressionMethod.values().map { it.name }
val compressionMethodAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, compressionMethods)
Expand Down Expand Up @@ -1338,16 +1350,28 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
val defaultEncryptionMethodIndex = encryptionMethods.indexOf(savedEncryptionMethod.name)
encryptionMethodInput.setSelection(if (defaultEncryptionMethodIndex != -1) defaultEncryptionMethodIndex else 0)

val encryptionStrengths = AesKeyStrength.values().map { it.name }
val encryptionStrengthAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, encryptionStrengths)
encryptionStrengthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
encryptionStrengthInput.adapter = encryptionStrengthAdapter

val savedEncryptionStrength = getSavedEncryptionStrength()
val defaultEncryptionStrengthIndex = encryptionStrengths.indexOf(savedEncryptionStrength.name)
encryptionStrengthInput.setSelection(if (defaultEncryptionStrengthIndex != -1) defaultEncryptionStrengthIndex else 0)

builder.setPositiveButton(getString(R.string.save)) { _, _ ->
val selectedCompressionMethod =
CompressionMethod.valueOf(compressionMethods[compressionMethodInput.selectedItemPosition])
val selectedCompressionLevel =
CompressionLevel.valueOf(compressionLevels[compressionLevelInput.selectedItemPosition])
val selectedEncryptionMethod =
EncryptionMethod.valueOf(encryptionMethods[encryptionMethodInput.selectedItemPosition])
val selectedEncryptionStrength =
AesKeyStrength.valueOf(encryptionStrengths[encryptionStrengthInput.selectedItemPosition])
saveCompressionMethod(selectedCompressionMethod)
saveCompressionLevel(selectedCompressionLevel)
saveEncryptionMethod(selectedEncryptionMethod)
saveEncryptionStrength(selectedEncryptionStrength)
}

builder.show()
Expand All @@ -1368,6 +1392,11 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
prefs.edit().putString(KEY_ENCRYPTION_METHOD, encryptionMethod.name).apply()
}

private fun saveEncryptionStrength(encryptionStrength: AesKeyStrength) {
prefs = requireContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit().putString(KEY_ENCRYPTION_METHOD, encryptionStrength.name).apply()
}

private fun getSavedCompressionMethod(): CompressionMethod {
prefs = requireContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val savedValue = prefs.getString(KEY_COMPRESSION_METHOD, "DEFLATE") ?: "DEFLATE"
Expand Down Expand Up @@ -1398,6 +1427,16 @@ class CreateZipFragment : Fragment(), FileAdapter.OnDeleteClickListener, FileAd
}
}

private fun getSavedEncryptionStrength(): AesKeyStrength {
prefs = requireContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val savedValue = prefs.getString(KEY_ENCRYPTION_METHOD, "KEY_STRENGTH_256") ?: "KEY_STRENGTH_256"
return try {
AesKeyStrength.valueOf(savedValue)
} catch (e: IllegalArgumentException) {
AesKeyStrength.KEY_STRENGTH_256 // Default value if the saved string is not a valid enum constant
}
}

private fun showPasswordInputDialog7z(onPasswordEntered: (String?, String, Int, Boolean, Int) -> Unit) {
val layoutInflater = LayoutInflater.from(requireContext())
val customView = layoutInflater.inflate(R.layout.seven_z_option_dialog, null)
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/seven_z_option_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="10dp">
android:padding="5dp">

<LinearLayout
android:layout_width="match_parent"
Expand Down
33 changes: 28 additions & 5 deletions app/src/main/res/layout/zip_settings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">

<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
Expand All @@ -44,7 +49,7 @@
<Spinner
android:id="@+id/compression_method_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:padding="5dp"/>
</com.google.android.material.card.MaterialCardView>
<TextView
Expand All @@ -60,7 +65,7 @@
<Spinner
android:id="@+id/compression_level_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:padding="5dp"/>
</com.google.android.material.card.MaterialCardView>
<TextView
Expand All @@ -76,8 +81,26 @@
<Spinner
android:id="@+id/encryption_method_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="48dp"
android:padding="5dp"/>
</com.google.android.material.card.MaterialCardView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginStart="10dp"
android:text="Encryption Strength"/>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<Spinner
android:id="@+id/encryption_strength_input"
android:layout_width="match_parent"
android:layout_height="48dp"
android:padding="5dp"/>
</com.google.android.material.card.MaterialCardView>

</LinearLayout>
</ScrollView>

0 comments on commit 8b7bd22

Please sign in to comment.