sqlite 更改数据库密码失败

kcwpcxri  于 2022-11-24  发布在  SQLite
关注(0)|答案(4)|浏览(368)

我的应用程序有一个SQLCipher密码保护的数据库。我的活动允许更改密码:

SQLiteDatabase database = SQLiteDatabase.openDatabase(getDatabasePath("db").getPath() , oldPass, null,0) ;
database.rawExecSQL("PRAGMA rekey = '"+newPass+"';");
database.close();

执行后,再次尝试访问数据库,我得到:

10-21 16:40:28.961  26635-26635/com.example            E/Database: CREATE TABLE android_metadata failed
10-21 16:40:28.981  26635-26635/com.example            E/Database: Failed to setLocale() when constructing, closing the database
        net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
        at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
        at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
        at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
        at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
        at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
        at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)

紧接着是:

Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
    at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
    at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
    at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
    at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
    at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)

这是怎么回事?执行日志:

10-21 23:22:46.915  17043-17043/com.example            D/testapp: Finalizing DB Facade Object
10-21 23:22:46.945  17043-17043/com.example            D/testapp: saveDBPasswordSettings - oldPass: ##
10-21 23:22:46.945  17043-17043/com.example            D/testapp: saveDBPasswordSettings - newPass: #newpass#
10-21 23:22:46.945  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 0
10-21 23:22:47.025    1462-1788/?                              E/ENSWrapper: return OMX_ErrorNotImplemented - GetExtensionIndex OMX.ST.AFM.pcmprocessing.transducer_equalizer h=0x003a8180  cParameterName=STE.ADM IndexType=unknown OMX_INDEXTYPE [ 0x00000002 ]
10-21 23:22:47.095  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 1
10-21 23:22:47.105  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 2
10-21 23:22:47.105  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 3
10-21 23:22:47.115  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 4
10-21 23:22:47.115  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 5
10-21 23:22:47.125  17043-17043/com.example            D/testapp: saveDBPasswordSettings - 6
10-21 23:22:47.145  17043-17043/com.example            D/testapp: setCacheDbPassword - Called with : newpass
10-21 23:22:47.205  17043-17043/com.example            D/testapp: setDbencriptionAskonstart - Called with : false
10-21 23:22:47.225  17043-17043/com.example            D/testapp: Finalizing DB Facade Object
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Trying to load lib /data/data/com.example/lib/libstlport_shared.so 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Shared lib '/data/data/com.example/lib/libstlport_shared.so' already loaded in same CL 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Trying to load lib /data/data/com.example/lib/libsqlcipher_android.so 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Shared lib '/data/data/com.example/lib/libsqlcipher_android.so' already loaded in same CL 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Trying to load lib /data/data/com.example/lib/libdatabase_sqlcipher.so 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/dalvikvm: Shared lib '/data/data/com.example/lib/libdatabase_sqlcipher.so' already loaded in same CL 0x4110ff20
10-21 23:22:47.235  17043-17043/com.example            D/testapp: USING PASSWORD :   newpass
10-21 23:22:47.325    1462-1788/?                              D/ADM: devset:588    STATUS API LEAVE: Opened new device 'Speaker', handle = 101
10-21 23:22:47.405  17043-17043/com.example            I/Database: sqlite returned: error code = 26, msg = file is encrypted or is not a database
10-21 23:22:47.405  17043-17043/com.example            E/Database: CREATE TABLE android_metadata failed
10-21 23:22:47.515  17043-17043/com.example            E/Database: Failed to setLocale() when constructing, closing the database
        net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
        at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
        at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
        at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
        at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
        at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
        at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
        at com.example.db.TestDataSource.open(TestDataSource.java:45)
        at com.example.db.DBFacade.init(DBFacade.java:91)
        at com.example.ui.config.DBPasswordActivity.saveDBPasswordSettings(DBPasswordActivity.java:118)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3098)
        at android.view.View.performClick(View.java:3574)
        at android.view.View$PerformClick.run(View.java:14293)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
        at dalvik.system.NativeStart.main(Native Method)
10-21 23:22:47.525  17043-17043/com.example            D/AndroidRuntime: Shutting down VM
10-21 23:22:47.525  17043-17043/com.example            W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40aac210)
10-21 23:22:47.626  17043-17043/com.example            E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3103)
        at android.view.View.performClick(View.java:3574)
        at android.view.View$PerformClick.run(View.java:14293)
        at android.os.Handler.handleCallback(Handler.java:605)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4448)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3098)
        ... 11 more
        Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
        at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
        at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
        at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
        at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
        at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
        at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
        at com.example.db.TestDataSource.open(TestDataSource.java:45)
        at com.example.db.DBFacade.init(DBFacade.java:91)
        at com.example.ui.config.DBPasswordActivity.saveDBPasswordSettings(DBPasswordActivity.java:118)
        ... 14 more
10-21 23:22:47.656    1439-1720/?                              D/SurfaceFlinger: screenshot: sw=120, sh=180, minZ=0, maxZ=21035
10-21 23:22:47.656    1862-1299/?                              W/ActivityManager: Force finishing activity com.example/.ui.config.DBPasswordActivity
10-21 23:22:47.896    1439-1720/?                              I/libblt_hw: Library opened (handle = 4, fd = 32)
10-21 23:22:48.196    1862-1896/?                              W/ActivityManager: Activity pause timeout for ActivityRecord{414ad5f8 com.example/.ui.config.DBPasswordActivity}
10-21 23:22:50.398    1462-1789/?                              D/ADM: devset:282    STATUS Close device 'Speaker'
10-21 23:22:57.695    1862-1896/?                              W/ActivityManager: Launch timeout has expired, giving up wake lock!
10-21 23:22:58.206    1862-1896/?                              W/ActivityManager: Activity idle timeout for ActivityRecord{4111e850 com.example/.TestApp}
10-21 23:23:03.791    1462-1788/?                              D/ADM: devset:414    STATUS Open device 'Speaker', 44100 Hz, format=2, 3 x 6144 bytes bufs
10-21 23:23:03.801  17043-17043/com.example            I/Process: Sending signal. PID: 17043 SIG: 9
10-21 23:23:03.811   1862-18347/?                              I/ActivityManager: Process com.example (pid 17043) has died.
10-21 23:23:03.821    1862-2211/?                              I/WindowManager: WIN DEATH: Window{41609620 com.example/com.example.ui.config.DBPasswordActivity paused=false}
10-21 23:23:03.831    1439-1892/?                              I/libblt_hw: Library closed (handle = 0, fd = 11)
10-21 23:23:03.831    1862-2816/?                              I/WindowManager: WIN DEATH: Window{4158a788 com.example/com.example.TestApp paused=false}
10-21 23:23:03.841   1862-18347/?                              W/ActivityManager: Force removing ActivityRecord{4111e850 com.example/.TestApp}: app died, no saved state
10-21 23:23:03.952    1439-1720/?                              I/libblt_hw: Library opened (handle = 0, fd = 11)
10-21 23:23:03.982    1462-1788/?                              E/ENSWrapper: return OMX_ErrorNotImplemented - GetExtensionIndex OMX.ST.AFM.pcmprocessing.transducer_equalizer h=0x0027f8e8  cParameterName=STE.ADM IndexType=unknown OMX_INDEXTYPE [ 0x00000002 ]
10-21 23:23:04.012    1862-2210/?                              W/InputManagerService: Got RemoteException sending setActive(false) notification to pid 17043 uid 10155
10-21 23:23:04.082    1439-1720/?                              I/libblt_hw: Library closed (handle = 4, fd = 32)
10-21 23:23:04.172    1462-1788/?                              D/ADM: devset:588    STATUS API LEAVE: Opened new device 'Speaker', handle = 101
10-21 23:23:07.295    1462-1789/?                              D/ADM: devset:282    STATUS Close device 'Speaker'

database.rawExecSQL("BEGIN IMMEDIATE TRANSACTION;");
database.rawExecSQL("PRAGMA rekey = '"+newPass+"';");
database.close();

紧接着是:

getWritableDatabase(newPass);

失败原因:

10-24 11:47:44.652    1204-1204/com.example D/example: USING PASSWORD :   aaa
10-24 11:47:44.952    1204-1204/com.example I/Database: sqlite returned: error code = 26, msg = file is encrypted or is not a database
10-24 11:47:44.952    1204-1204/com.example E/Database: CREATE TABLE android_metadata failed
10-24 11:47:44.972    1204-1204/com.example E/Database: Failed to setLocale() when constructing, closing the database
        net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
        at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
        at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2101)
        at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1967)
        at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:900)
        at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:943)
        at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
cvxl0en2

cvxl0en21#

SQLCipher API文档
无法使用Rekey来加密一开始未加密的数据库。如果您的使用者将密码从空白变更为非空白,则会以您所描述的方式失败。他们在此说明如何加密先前未加密的数据库。

rur96b6h

rur96b6h2#

SQLiteDatabase.rawExecSQL("PRAGMA key = 'old_password';");
SQLiteDatabase.rawExecSQL("PRAGMA rekey = 'new_password';");
yws3nbqq

yws3nbqq3#

发行

database.rawExecSQL("BEGIN IMMEDIATE TRANSACTION;");

database.rawExecSQL("PRAGMA rekey = '"+newPass+"';");

应确保在此期间没有其他数据库连接能够写入数据库。

n3h0vuf2

n3h0vuf24#

请确认您在恢复应用程序时使用的是新密码,因为它与SQLiteOpenHelper.getWritableDatabase(…)中的调用相关。

相关问题