Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Trusted Contributor
anta40
Posts: 213
Registered: ‎07-26-2010
My Carrier: Telkomsel

How to read file created using ExtendedFileConnection?

[ Edited ]

I usually use these codes to read/write text files:

http://stackoverflow.com/questions/1519328/j2me-blackberry-how-to-read-write-text-file

 

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/documents/supersecret.txt", "Hello this is a secret");, then file:///SDCard/BlackBerry/documents/supersecret.txt.rem will be created.

 

The problem is I cannot read the file. The result of calling readSecuredTextFile("file:///SDCard/BlackBerry/documents/supersecret.txt.rem"); is null.

Please use plain text.
Developer
Developer
CMY
Posts: 1,115
Registered: ‎02-10-2009
My Carrier: Verizon

Re: How to read file created using ExtendedFileConnection?

I've never tried to write a stream of data using encryption, I've always done it byte by byte and it works fine for me. Also, you don't need to set the key when reading the file. It will automatically check to see if you have permissions when trying to read the file. And I've never had luck reading the actual encrypted file because its hard to seek/read into the file more than one byte at a time, so I've always written the data back out to a temporary unencrypted file and deleted it when done.
Please use plain text.
Trusted Contributor
anta40
Posts: 213
Registered: ‎07-26-2010
My Carrier: Telkomsel

Re: How to read file created using ExtendedFileConnection?

[ Edited ]

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). :smileyindifferent:

 

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.

http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/io/file/ExtendedFileConnection...

 

So maybe you have better snippet about reading & writing file using ExtendedFileConnection?

Please use plain text.
Developer
Developer
CMY
Posts: 1,115
Registered: ‎02-10-2009
My Carrier: Verizon

Re: How to read file created using ExtendedFileConnection?

[ Edited ]

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.

Please use plain text.