08-30-2012 04:54 AM - edited 08-30-2012 04:54 AM
I usually use these codes to read/write text files:
http://stackoverflow.com/questions/1519328/j2me-bl
I changed the codes a bit, so ExtendedFileConnection is used, instead of FileConnection
public void writeSecuredTextFile(String fName, String text){
int moduleHandle = ApplicationDescriptor.currentApplicationDescriptor ().getModuleHandle();
CodeSigningKey csk = CodeSigningKey.get(moduleHandle, "ANTA" );
DataOutputStream os = null;
ExtendedFileConnection efconn = null;
try {
efconn = (ExtendedFileConnection) Connector.open(fName, Connector.READ_WRITE);
efconn.setControlledAccess(csk);
efconn.enableDRMForwardLock();
if (!efconn.exists()) efconn.create();
os = efconn.openDataOutputStream();
os.write(text.getBytes());
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
try {
if (null != os) os.close();
if (null != efconn) efconn.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
private String readSecuredTextFile(String fName) {
int moduleHandle = ApplicationDescriptor.currentApplicationDescriptor ().getModuleHandle();
CodeSigningKey csk = CodeSigningKey.get(moduleHandle, "ANTA" );
String result = null;
ExtendedFileConnection efconn = null;
DataInputStream is = null;
try {
efconn = (ExtendedFileConnection) Connector.open(fName, Connector.READ_WRITE);
efconn.setControlledAccess(csk);
is = efconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
finally {
try {
if (null != is) is.close();
if (null != efconn) efconn.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
} I assume that writeSecuredTextFile() works as expected. For example if I call writeSecuredTextFile("file:///SDCard/BlackBerry/do
The problem is I cannot read the file. The result of calling readSecuredTextFile("file:///SDCard/BlackBerry/doc
08-30-2012 08:43 PM
08-30-2012 11:10 PM - edited 08-30-2012 11:13 PM
Actually the reason I'm using ExtendedFileConnection is not to encrypt the files, but rather to obscure them so they are not easily searchable. But actually they do (I just tried it). ![]()
Yes I think I misunderstood ExtendedFileConnection, but at least that was my initial impression after reading this:
If controlled access is enabled for a file, the file that is written out may be wrapped in an encrypted format so that other applications cannot read it. Wrapping of the file may depend on the underlying file system root as some file systems may store the controlled access key directly with the file, for example, the SDCard root.
So maybe you have better snippet about reading & writing file using ExtendedFileConnection?
09-01-2012 10:39 PM - edited 09-01-2012 10:41 PM
Here's some code I use to write files:
...........
CodeSigningKey signer = getMyKey();
ExtendedFileConnection secure = (ExtendedFileConnection) Connector.open( "file://"+location+value );
if( !secure.exists() ){
if( drm ) secure.enableDRMForwardLock();
secure.setControlledAccess(signer);
secure.create(); secure.setHidden(true);
} else {
if( !drm || (drm && secure.isContentDRMForwardLocked()) ){
secure.setHidden(true);
secure.close();
lockConn.close();
return;
} else if( drm && !secure.isContentDRMForwardLocked() ){
secure.delete();
secure.close();
secure = (ExtendedFileConnection) Connector.open( "file://"+location+value );
secure.enableDRMForwardLock();
secure.setControlledAccess(signer);
secure.create(); secure.setHidden(true);
}
}
long fSize = lockConn.fileSize();
OutputStream printer = secure.openOutputStream();
InputStream reader = lockConn.openInputStream();
for( int i=0; i<fSize; ++i ){
printer.write( reader.read() );
}
Here is some code I use to read the file into temporary file that you can manipulate:
ExtendedFileConnection lockConn = (ExtendedFileConnection) Connector.open( "file://"+loc+locked );
if( lockConn.exists() ){
FileConnection unsecure = (FileConnection) Connector.open("file://"+loc+orig );
unsecure.create();
long fSize = lockConn.fileSize();
OutputStream printer = unsecure.openOutputStream();
InputStream reader = lockConn.openInputStream();
int check = 0;
for( int l=0; l<fSize; ++l ){
check = reader.read();
printer.write(check);
}
reader.close();
printer.close();
unsecure.close();
}
lockConn.close();
As I said, I read the contents into a temporary file because its hard to skip/seek inside an encrypted file.