gpt4 book ai didi

java - 将 Java 代码用于 Android 应用程序(用于 AES 加密)

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:38 27 4
gpt4 key购买 nike

我是 Android 新手。我正在做一个关于 AES 加密的项目,我想制作一个 Android 应用程序,我有 Java 中的 AES 加密代码,它运行得很好:

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

现在,我想编写一个主要的 Activity 代码,其中应该有一个 Editview 和一个 Button。EditView 将获取我想要以纯文本形式传递给 AESAlgorithm.java 的文本,AESAlgorithm.java 反过来会将加密文本返回到主 Activity ,我将其发送到第二个 Activity (show.java),该 Activity 将有一个 TextView 来显示加密文本。

我已经有一个(示例)主要 Activity :

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

}

});



}


}

这里的“Show.java”是一个简单的 Activity ,布局中只有一个 TextView 来显示主 Activity 发送的文本。我想将 Plaintext 变量传递给 AESAlgorithm.java 进行加密,我想从 Java 文件接收“encryptedtext”变量中的加密文本。我如何修改java代码以在这个android应用程序中工作?

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

最佳答案

您可以删除 Android 的 psvm 方法。

但我不明白问题出在哪里?

如果您将 AES 类添加到 Android 项目的包中,您可以通过调用静态方法加密和解密来使用它?

关于java - 将 Java 代码用于 Android 应用程序(用于 AES 加密),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com