如何使用Java插件加密文件

时间:2018-08-04 12:47:01

标签: java android

我正在为Droidscript(DS)编码一个插件,该插件可在android上加密文件。 我对此有一点疑问。插件名称为Utils。

这是Utils.inc文件

//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);}}

此文件从DS接收命令并发送到Utils.java

这是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
*/
/*

将从Bundle b中提取的用于加密的文件名发送到CipherExample.java类时遇到问题

*/ }


    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");

密钥实际上是用于加密的密码

*/
/*

如何将所有这些字符串发送到CipherExample类进行加密?

*/}

    private void Decrypt( Bundle b )
    {


/*It should decrypt the encrypted file

 when called
*/

String srcFil /*encrypted file */ =
 b.getString ("p1");
and so on...

    }
}

这是我的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();
}
}

我想要的是,在Droidscript中运行此代码

plg = app.CreateUtils ();
//plg.EncF (srcFil,dstFil,key);

 plg.EncF ("/sdcard/vdo.mp4","/sdcard/vdo.mp4.encrypted","encryptit");

Utils.inc文件会将文件名和目的地(包括密码)发送到Utils.java。

我的问题在这里,现在如何将文件从Utils.java发送到CipherExample进行加密,以及在解密中使用相同的情况。

0 个答案:

没有答案