I’ve followed the guidance for a few days now trying to debug where my code is incorrect. I get the 26 error after it runs trying to convert a plaintext to encrypted. Any pointers?
This code executes and used to work. Something changed either in the libraries being used or perhaps something else. If I create a brand new database with encryption, it works fine. If this code is called to convert a previous version, the database is unuseable after the conversion.
try
{
if (_con == null)
throw new InvalidDataException("No database connection exists");
using var command = _con.CreateCommand();
command.CommandText = @"ATTACH DATABASE '" + dbEncrypted + "' AS encrypted KEY '" + encryptionKey + "'";
command.ExecuteNonQuery();
TracingAndLogging.TraceInformation(command.CommandText);
TracingAndLogging.TraceInformation("AddEncryption: Created and attached to the encrypted database");
command.CommandText = @"SELECT sqlcipher_export('encrypted');";
int converted = command.ExecuteNonQuery();
TracingAndLogging.TraceInformation(command.CommandText);
TracingAndLogging.TraceInformation("AddEncryption: Export into the encrypted database: " + converted.ToString());
TracingAndLogging.TraceInformation("AddEncryption: Exporting complete");
command.CommandText = @"DETACH DATABASE encrypted;";
command.ExecuteNonQuery();
TracingAndLogging.TraceInformation(command.CommandText);
TracingAndLogging.TraceInformation("AddEncryption: Detached from the encrypted database");
}
catch (Exception ex)
{
TracingAndLogging.TraceWarning("AddEncryption: " +
String.Format(Properties.Resources.Trace_15, ex.Message));
}
CloseDB();