diff --git a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt index 6b1d385b3..d3af75f75 100644 --- a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt @@ -25,13 +25,13 @@ import android.content.IntentFilter import android.content.ServiceConnection import android.net.Uri import android.os.IBinder -import android.util.Base64 import android.util.Log import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.SingletonHolderParameter +import org.apache.commons.codec.binary.Base64 import java.util.LinkedList class CipherDatabaseAction(context: Context) { @@ -136,13 +136,11 @@ class CipherDatabaseAction(context: Context) { mBinder?.getCipherDatabase(databaseUri)?.let { cipherDatabaseEntity -> cipherDatabase = CipherEncryptDatabase().apply { this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) - this.encryptedValue = Base64.decode( - cipherDatabaseEntity.encryptedValue, - Base64.NO_WRAP + this.encryptedValue = Base64.decodeBase64( + cipherDatabaseEntity.encryptedValue ) - this.specParameters = Base64.decode( - cipherDatabaseEntity.specParameters, - Base64.NO_WRAP + this.specParameters = Base64.decodeBase64( + cipherDatabaseEntity.specParameters ) } } @@ -155,13 +153,11 @@ class CipherDatabaseAction(context: Context) { ?.let { cipherDatabaseEntity -> CipherEncryptDatabase().apply { this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) - this.encryptedValue = Base64.decode( - cipherDatabaseEntity.encryptedValue, - Base64.NO_WRAP + this.encryptedValue = Base64.decodeBase64( + cipherDatabaseEntity.encryptedValue ) - this.specParameters = Base64.decode( - cipherDatabaseEntity.specParameters, - Base64.NO_WRAP + this.specParameters = Base64.decodeBase64( + cipherDatabaseEntity.specParameters ) } } @@ -186,8 +182,8 @@ class CipherDatabaseAction(context: Context) { val cipherDatabaseEntity = CipherDatabaseEntity( databaseUri.toString(), - Base64.encodeToString(cipherEncryptDatabase.encryptedValue, Base64.NO_WRAP), - Base64.encodeToString(cipherEncryptDatabase.specParameters, Base64.NO_WRAP), + Base64.encodeBase64String(cipherEncryptDatabase.encryptedValue), + Base64.encodeBase64String(cipherEncryptDatabase.specParameters), ) if (useTempDao) { diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt index 2d0eb960f..3df7fe02d 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt @@ -20,10 +20,8 @@ package com.kunzisoft.keepass.database.element import android.os.Parcel import android.os.Parcelable -import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.HashManager -import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars import com.kunzisoft.keepass.utils.StringUtil.toHexString @@ -31,6 +29,7 @@ import com.kunzisoft.keepass.utils.readByteArrayCompat import com.kunzisoft.keepass.utils.readEnum import com.kunzisoft.keepass.utils.writeByteArrayCompat import com.kunzisoft.keepass.utils.writeEnum +import org.apache.commons.codec.binary.Base64 import org.apache.commons.codec.binary.Hex import org.w3c.dom.Node import java.io.ByteArrayInputStream @@ -212,9 +211,7 @@ data class MasterCredential(var password: String? = null, when (xmlKeyFileVersion) { 1F -> { // No hash in KeyFile XML version 1 - return Base64.decode(dataString, - DatabaseKDBX.BASE_64_FLAG - ) + return Base64.decodeBase64(dataString) } 2F -> { return if (hashString != null diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index 6a372b661..88d3e84e9 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -19,7 +19,6 @@ */ package com.kunzisoft.keepass.database.element.database -import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.HashManager import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm @@ -886,12 +885,9 @@ class DatabaseKDBX : DatabaseVersioned { } companion object { - val TYPE = DatabaseKDBX::class.java private val TAG = DatabaseKDBX::class.java.name private const val DEFAULT_HISTORY_MAX_ITEMS = 10 // -1 unlimited private const val DEFAULT_HISTORY_MAX_SIZE = (6 * 1024 * 1024).toLong() // -1 unlimited - - const val BASE_64_FLAG = Base64.NO_WRAP } } diff --git a/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt index 7d9e1867d..c85f56141 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt @@ -19,7 +19,6 @@ */ package com.kunzisoft.keepass.database.file.input -import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.StreamCipher import com.kunzisoft.keepass.database.crypto.CipherEngine @@ -29,7 +28,6 @@ import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.DatabaseKDBX -import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG import com.kunzisoft.keepass.database.element.database.DatabaseVersioned import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX @@ -47,6 +45,7 @@ import com.kunzisoft.keepass.stream.HashedBlockInputStream import com.kunzisoft.keepass.stream.HmacBlockInputStream import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.* +import org.apache.commons.codec.binary.Base64 import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParserException import org.xmlpull.v1.XmlPullParserFactory @@ -55,7 +54,10 @@ import java.io.InputStream import java.io.UnsupportedEncodingException import java.nio.charset.Charset import java.text.ParseException -import java.util.* +import java.util.Arrays +import java.util.Date +import java.util.Stack +import java.util.UUID import java.util.zip.GZIPInputStream import javax.crypto.Cipher import javax.crypto.CipherInputStream @@ -346,7 +348,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } else if (name.equals(DatabaseKDBXXML.ElemHeaderHash, ignoreCase = true)) { val encodedHash = readString(xpp) if (encodedHash.isNotEmpty() && hashOfHeader != null) { - val hash = Base64.decode(encodedHash, BASE_64_FLAG) + val hash = Base64.decodeBase64(encodedHash) if (!Arrays.equals(hash, hashOfHeader)) { throw DatabaseInputException() } @@ -432,7 +434,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) { val strData = readString(xpp) if (strData.isNotEmpty()) { - customIconData = Base64.decode(strData, BASE_64_FLAG) + customIconData = Base64.decodeBase64(strData) } } else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) { customIconName = readString(xpp) @@ -836,7 +838,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) // Catch with null test below } } else { - var buf = Base64.decode(sDate, BASE_64_FLAG) + var buf = Base64.decodeBase64(sDate) if (buf.size != 8) { val buf8 = ByteArray(8) System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8)) @@ -902,7 +904,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } return try { - val buf = Base64.decode(encoded, BASE_64_FLAG) + val buf = Base64.decodeBase64(encoded) bytes16ToUuid(buf) } catch (e: Exception) { Log.e(TAG, "Unable to read base 64 UUID, create a random one", e) @@ -1022,7 +1024,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) isRAMSufficient.invoke(base64.length.toLong()), compressed, protected, binaryId) try { binaryAttachment.getOutputDataStream(mDatabase.binaryCache).use { outputStream -> - outputStream.write(Base64.decode(base64, BASE_64_FLAG)) + outputStream.write(Base64.decodeBase64(base64)) } } catch (e: Exception) { Log.e(TAG, "Unable to read base 64 attachment", e) @@ -1055,7 +1057,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) if (xpp.attributeCount > 0) { val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected) if (protect != null && protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)) { - Base64.decode(xpp.safeNextText(), BASE_64_FLAG)?.let { data -> + Base64.decodeBase64(xpp.safeNextText())?.let { data -> return randomStream?.processBytes(data) } return ByteArray(0) diff --git a/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt index 2866ffaca..fb7289e47 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt @@ -19,7 +19,6 @@ */ package com.kunzisoft.keepass.database.file.output -import android.util.Base64 import android.util.Log import android.util.Xml import com.kunzisoft.encrypt.StreamCipher @@ -28,7 +27,6 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.DatabaseKDBX -import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG import com.kunzisoft.keepass.database.element.database.DatabaseVersioned import com.kunzisoft.keepass.database.element.entry.AutoType import com.kunzisoft.keepass.database.element.entry.EntryKDBX @@ -45,11 +43,13 @@ import com.kunzisoft.keepass.database.file.DateKDBXUtil import com.kunzisoft.keepass.stream.HashedBlockOutputStream import com.kunzisoft.keepass.stream.HmacBlockOutputStream import com.kunzisoft.keepass.utils.* +import org.apache.commons.codec.binary.Base64 import org.xmlpull.v1.XmlSerializer import java.io.IOException import java.io.OutputStream import java.security.SecureRandom -import java.util.* +import java.util.Stack +import java.util.UUID import java.util.zip.GZIPOutputStream import javax.crypto.Cipher import javax.crypto.CipherOutputStream @@ -216,7 +216,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName) if (hashOfHeader != null) { - writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encode(hashOfHeader!!, BASE_64_FLAG))) + writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encodeBase64(hashOfHeader!!))) } if (!header!!.version.isBefore(FILE_VERSION_40)) { @@ -417,7 +417,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) writeString(name, DatabaseKDBXXML.DateFormatter.format(date)) } else { val buf = longTo8Bytes(DateKDBXUtil.convertDateToKDBX4Time(date)) - val b64 = String(Base64.encode(buf, BASE_64_FLAG)) + val b64 = String(Base64.encodeBase64(buf)) writeString(name, b64) } } @@ -441,7 +441,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) @Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class) private fun writeUuid(name: String, uuid: UUID) { val data = uuidTo16Bytes(uuid) - writeString(name, String(Base64.encode(data, BASE_64_FLAG))) + writeString(name, String(Base64.encodeBase64(data))) } /* @@ -490,7 +490,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) // Write the XML binary.getInputDataStream(binaryCache).use { inputStream -> inputStream.readAllBytes { buffer -> - xml.text(String(Base64.encode(buffer, BASE_64_FLAG))) + xml.text(String(Base64.encodeBase64(buffer))) } } } catch (e: Exception) { @@ -562,7 +562,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) xml.attribute(null, DatabaseKDBXXML.AttrProtected, DatabaseKDBXXML.ValTrue) val data = value.toString().toByteArray() val encoded = randomStream?.processBytes(data) ?: ByteArray(0) - xml.text(String(Base64.encode(encoded, BASE_64_FLAG))) + xml.text(String(Base64.encodeBase64(encoded))) } else { xml.text(value.toString()) } @@ -723,7 +723,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) Log.e(TAG, "Unable to write custom icon", e) } finally { writeString(DatabaseKDBXXML.ElemCustomIconItemData, - String(Base64.encode(customImageData, BASE_64_FLAG))) + String(Base64.encodeBase64(customImageData))) } if (iconCustom.name.isNotEmpty()) { writeString(DatabaseKDBXXML.ElemName, iconCustom.name)