Utilisation du code Java pour l'application Android (pour le cryptage AES)


Je suis nouveau sur Android. Je fais un projet sur le cryptage AES et je veux créer une application Android, j'ai un code de cryptage AES en Java qui fonctionne parfaitement:

 //AESAlgorithm Class

package com.example.pr1;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESalgorithm {
public static String encryptedText;
private static String algorithm = "AES";
private static byte[] keyValue =
new byte[] { 'A', 'S', 'e', 'c', 'u', 'r', 'e','S', 'e', 'c', 'r', 'e',   't', 'K', 'e', 'y' };

// Performs Encryption
public static String encrypt(String plainText) throws Exception {
Key key = generateKey();
Cipher chiper = Cipher.getInstance(algorithm);
chiper.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = chiper.doFinal(plainText.getBytes());
String encryptedValue = encode(encVal);
return encryptedValue;
}

// Performs decryption
 public static String decrypt(String encryptedText) throws Exception {
 // generate key
Key key = generateKey();
Cipher chiper = Cipher.getInstance(algorithm);
chiper.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = decode(encryptedText);
byte[] decValue = chiper.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}

// generateKey() is used to generate a secret key for AES algorithm
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, algorithm);
return key;
}

// performs encryption & decryption
public static void main(String[] args) throws Exception {

String  plainText = "This ";

//This is the variable i am passing to MainActivity
encryptedText = AESalgorithm.encrypt(plainText);
String decryptedText = AESalgorithm.decrypt(encryptedText);


 }

 private static String encode(byte[] byteArray) {
 StringBuilder buf = new StringBuilder();
  int intVal = 0;
 String frag = "";

 for (byte b : byteArray) {
  intVal = (int) (0xff & b);
 frag = Integer.toHexString(intVal);
 if (1 == frag.length()) {
 frag = "0" + frag;
  }
 buf.append(frag);
}
 return buf.toString();
}

private static byte[] decode(String textString) {
byte[] byteArray = new byte[(textString.length() / 2)];
int intVal = 0;
String frag = "";
 int c1 = 0;
 for (int i = 0; i < byteArray.length; i++) {
c1 = (i * 2);
frag = textString.substring(c1, (c1 + 2));
 intVal = Integer.parseInt(frag, 16);
byteArray[i] = (byte) (0xff & intVal);
}
return byteArray;
 }
 }

Maintenant, je veux écrire un code d'activité principal qui devrait avoir une vue d'édition et un bouton. L'EditView obtiendra le texte que je veux passer en tant que texte brut à l'Aesalgorithme.java qui à son tour retournerait du texte crypté à l'activité principale que je vais envoyer à une deuxième activité (show.java) qui aura un textview pour montrer la encryptedtext.

J'ai déjà une activité principale (exemple) comme:

MainActivity (Sample):

package com.example.pr1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;





public class PRMain extends Activity {

   protected static final int TAKE_PHOTO_CODE = 0;
   Button  mButton;
   EditText mEdit;
   int count;
   public static String encryptedtext,plaintext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prmain);


        mButton = (Button)findViewById(R.id.button);
        mEdit   = (EditText)findViewById(R.id.editText1);




        mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    AESalgorithm AES = new AESalgorithm();
                    plaintext = mEdit.getText().toString();


                Intent intent = new Intent(PRMain.this, Show.class);
                encryptedtext = AES.encryptedText;
                 Bundle b = new Bundle();
                 b.putString("name", encryptedtext ); 
                 intent.putExtras(b);
                 startActivity(intent);
                 finish();

                }

            });



    }


}

Ici " Montrer.java " est une activité simple avec juste un TextView dans la mise en page pour afficher le texte envoyé par l'activité principale. Je veux passer la variable en clair à AESAlgorithm.java pour le chiffrement, et je veux recevoir le texte crypté dans la variable "encryptedtext" à partir du fichier Java. Comment puis-je modifier le code java pour fonctionner dans cette application Android?

Voici mes données logcat:

04-30 21:03:49.968: D/AndroidRuntime(17765): Shutting down VM
04-30 21:03:49.968: W/dalvikvm(17765): threadid=1: thread exiting with uncaught exception (group=0x419d7d40)
04-30 21:03:49.968: W/dalvikvm(17765): threadid=1: uncaught exception occurred
04-30 21:03:49.969: W/System.err(17765): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.example.pr1/com.example.pr1.Show}: java.lang.NullPointerException
04-30 21:03:49.970: W/System.err(17765):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
04-30 21:03:49.970: W/System.err(17765):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)
 04-30 21:03:49.970: W/System.err(17765):   at android.app.ActivityThread.access$1100(ActivityThread.java:151)
04-30 21:03:49.970: W/System.err(17765):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
04-30 21:03:49.971: W/System.err(17765):    at android.os.Handler.dispatchMessage(Handler.java:110)
04-30 21:03:49.971: W/System.err(17765):    at android.os.Looper.loop(Looper.java:193)
04-30 21:03:49.971: W/System.err(17765):    at android.app.ActivityThread.main(ActivityThread.java:5551)
04-30 21:03:49.971: W/System.err(17765):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 21:03:49.971: W/System.err(17765):    at java.lang.reflect.Method.invoke(Method.java:515)
04-30 21:03:49.972: W/System.err(17765):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
 04-30 21:03:49.972: W/System.err(17765):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730)
04-30 21:03:49.972: W/System.err(17765):    at dalvik.system.NativeStart.main(Native Method)
04-30 21:03:49.972: W/System.err(17765): Caused by: java.lang.NullPointerException
04-30 21:03:49.973: W/System.err(17765):    at com.example.pr1.Show.onCreate(Show.java:34)
04-30 21:03:49.973: W/System.err(17765):    at android.app.Activity.performCreate(Activity.java:5310)
04-30 21:03:49.974: W/System.err(17765):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-30 21:03:49.974: W/System.err(17765):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
04-30 21:03:49.974: W/System.err(17765):    ... 11 more
04-30 21:03:49.974: W/dalvikvm(17765): threadid=1: calling UncaughtExceptionHandler
04-30 21:03:49.977: E/AndroidRuntime(17765): FATAL EXCEPTION: main
04-30 21:03:49.977: E/AndroidRuntime(17765): Process: com.example.pr1, PID: 17765
04-30 21:03:49.977: E/AndroidRuntime(17765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pr1/com.example.pr1.Show}: java.lang.NullPointerException
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.ActivityThread.access$1100(ActivityThread.java:151)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
 04-30 21:03:49.977: E/AndroidRuntime(17765):   at android.os.Handler.dispatchMessage(Handler.java:110)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.os.Looper.loop(Looper.java:193)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.ActivityThread.main(ActivityThread.java:5551)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at java.lang.reflect.Method.invoke(Method.java:515)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:730)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at dalvik.system.NativeStart.main(Native Method)
04-30 21:03:49.977: E/AndroidRuntime(17765): Caused by: java.lang.NullPointerException
04-30 21:03:49.977: E/AndroidRuntime(17765):    at com.example.pr1.Show.onCreate(Show.java:34)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.Activity.performCreate(Activity.java:5310)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
04-30 21:03:49.977: E/AndroidRuntime(17765):    at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381)
Author: Ka z, 2016-04-30

1 answers

Vous pouvez supprimer la méthode psvm pour Android.

Mais je ne comprends pas quel est le problème ?

Si vous ajoutez la classe AES à votre package dans votre projet Android, vous pouvez l'utiliser en appelant les méthodes statiques encrypt and decrypt ?

 0
Author: Chris Sherlock, 2016-04-30 13:07:51