I am coding a plugin for Droidscript(DS) that encrypts file on android. I have a bit problem with it.plugin name is Utils.
here is the Utils.inc file
//Add CreateUtils method to global app object.
app.CreateUtils = function() { return new Utils();}
//Plugin wrapper class.
function Utils( options )
{
this.plg = _CreatePlugin( "u.cH.UnikCHD.plugin.libutils.Utils", options );
this.GetVersion = function( num, txt ) { return parseFloat( this.plg.Send( "GetVersion" ) ); }
this.SaveMyImage = function( img ) { this.plg.SendImg( "SaveMyImage", img ); }
this.CryptF = function(srcFil,dstFil,key){this.plg.Send("CryptF",srcFil,dstFil,key);}
this.DecF = function(encryptedFile,dstFil,key){this.plg.Send("DecF",encryptedFile,dstFil,key);}
this.EncF = function(srcFil/*Source file to encrypt*/,dstFil,key/* is a password to encrypt file*/){this.plg.Send("DecF",encryptedFile,dstFil,key);}}
this file receives the commands from DS and sends to Utils.java
here is the Utils.java:
package u.cH.UnikCHD.plugin.libutils;
import android.os.*;
import android.content.*;
import android.util.Log;
import android.graphics.*;
import java.io.*;
import java.lang.reflect.*;
public class Utils{
public static String TAG = "Utils";
public static float VERSION = 1.0f;
private Method m_callscript;
private Object m_parent;
//Script callbacks.
private String m_OnMyReply;
//Contruct plugin.
public Utils()
{
Log.d( TAG, "Creating plugin object");
}
//Initialise plugin.
public void Init( Context ctx, Object parent )
{
try {
Log.d( TAG, "Initialising plugin object");
//Save reference to parent (DroidScript).
m_parent = parent;
//Use reflection to get 'CallScript' method
Log.d( TAG, "Getting CallScript method");
m_callscript = parent.getClass().getMethod( "CallScript", Bundle.class );
}
catch (Exception e) {
Log.e( TAG, "Failed to Initialise plugin!", e );
}
}
//Call a function in the user's script.
private void CallScript( Bundle b )
{
try {
m_callscript.invoke( m_parent, b );
}
catch (Exception e) {
Log.e( TAG, "Failed to call script function!", e );
}
}
//Handle commands from DroidScript.
public String CallPlugin( Bundle b )
{
//Extract command.
String cmd = b.getString("cmd");
//Process commands.
String ret = null;
try {
if( cmd.equals("GetVersion") )
return GetVersion( b );
else if( cmd.equals("SaveMyImage") )
SaveMyImage( b );
else if( cmd.equals("CryptF"))
crypt( b );
else if( cmd.equals("EncF"))
Encrypt( b );
else if( cmd.equals("DecF"))
Decrypt( b );
}
catch (Exception e) {
Log.e( TAG, "Plugin command failed!", e);
}
return ret;
}
//Handle the GetVersion command.
private String GetVersion( Bundle b )
{
Log.d( TAG, "Got GetVersion" );
return Float.toString( VERSION );
}
private void SaveMyImage( Bundle b )
{
//Get byte array from bundle.
byte[] byteArray = b.getByteArray("img");
//Convert image to bitmap.
Bitmap bmp = BitmapFactory.decodeByteArray( byteArray, 0, byteArray.length );
//Save image to sdcard.
String file = "/sdcard/exe.jpg";
Log.d( TAG, "Saving jpeg " + file );
try {
FileOutputStream outStream = new FileOutputStream( file );
bmp.compress( Bitmap.CompressFormat.JPEG, 95, outStream );
outStream.close();
}
catch(Exception e) {
Log.e( TAG, "SaveMyImage failed", e );
}
}
private void crypt( Bundle b )
{
//Extract params.
String srcFil = b.getString("p1");
String dstFil = b.getString("p2");
String key = b.getString("p3");
CipherExample crp = new CipherExample();
/* srcFile is the source file to be encrypted
dstFil is its filename after encryption
key is the password for the encryption
*/
/*
I am having a problem to send the file names extracted from Bundle b for encryption to the CipherExample.java class
*/ }
private void Encrypt( Bundle b )
{
/* it should encrytp the file when called.
String srcFil = b.getString ("p1");
String dstFil = b.getString ("p2");
String key = b.getString ("p3");
key is actually password for encryption
*/
/*
How can I send all these strings to CipherExample class to encrypt?
*/}
private void Decrypt( Bundle b )
{
/*It should decrypt the encrypted file
when called
*/
String srcFil /*encrypted file */ =
b.getString ("p1");
and so on...
}
}
here is my CipherExample.java:
package u.cH.UnikCHD.plugin.libutils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class CipherExample {
public static void main(String[] args) {
try {
String key = "squirrel123"; // needs to be at least 8 characters for DES
FileInputStream fis = new FileInputStream("/sdcard/3.txt");
FileOutputStream fos = new FileOutputStream("/sdcard/3.txt.enc");
encrypt(key, fis, fos);
FileInputStream fis2 = new FileInputStream("/sdcard/3.txt.enc.txt");
FileOutputStream fos2 = new FileOutputStream("/sdcard/3.dec.txt");
decrypt(key, fis2, fos2);
} catch (Throwable e) {
e.printStackTrace();
}
}
public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
}
public static void doCopy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
}
what I want is, in Droidscript this code is run
plg = app.CreateUtils ();
//plg.EncF (srcFil,dstFil,key);
plg.EncF ("/sdcard/vdo.mp4","/sdcard/vdo.mp4.encrypted","encryptit");
the Utils.inc file will send the file name and destination including password to the Utils.java.
and my problem is here, now how can I send the files from Utils.java to CipherExample to encrypt it and the same case in Decryption.