我正在寻找基于 Java 中椭圆曲线的密码算法的实现,用于 Android 项目。我找到了这个回购。
我写了一个简短的代码只是为了测试并在https://replit.com上运行它,它工作正常。但是我在 Android 中使用了相同的代码,它抛出了一个异常。
Java 代码
import software.pando.crypto.nacl.*;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
class Main {
public static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a)
sb.append(String.format("%02x", b));
return sb.toString();
}
public static void main(String[] args) {
var aliceKeys = CryptoBox.keyPair();
var bobKeys = CryptoBox.keyPair();
var alicePrivateKey = aliceKeys.getPrivate();
var alicePublicKey = aliceKeys.getPublic();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("ECCPublicKey.txt"));
dos.writeBytes(byteArrayToHex(alicePublicKey.getEncoded()));
dos.flush();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
CryptoBox box = CryptoBox.encrypt(aliceKeys.getPrivate(), bobKeys.getPublic(), "Hello, World!");
String msg = box.decryptToString(bobKeys.getPrivate(), aliceKeys.getPublic());
System.out.println("Decrypted: " + msg.toString());
}
}
安卓
btnBecomeCustomer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
var cipherText = Encrypt("Hello World!");
Toast.makeText(MainActivity.this, cipherText, Toast.LENGTH_LONG).show();
}
public static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a)
sb.append(String.format("%02x", b));
return sb.toString();
}
public static String Encrypt(String plainText) {
var aliceKeys = CryptoBox.keyPair();
var bobKeys = CryptoBox.keyPair();
var alicePrivateKey = aliceKeys.getPrivate();
var alicePublicKey = aliceKeys.getPublic();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("ECCPublicKey.txt"));
dos.writeBytes(byteArrayToHex(alicePublicKey.getEncoded()));
dos.flush();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
CryptoBox box = CryptoBox.encrypt(aliceKeys.getPrivate(), bobKeys.getPublic(), plainText);
var cipherText = box.toString();
return cipherText;
}
我收到此错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demoappjava, PID: 12092
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/security/spec/NamedParameterSpec;
at software.pando.crypto.nacl.CryptoBox.<clinit>(CryptoBox.java:97)
at software.pando.crypto.nacl.CryptoBox.keyPair(CryptoBox.java:108)
at com.example.demoappjava.MainActivity.Encrypt(MainActivity.java:484)
at com.example.demoappjava.MainActivity$7.onClick(MainActivity.java:408)
at android.view.View.performClick(View.java:7044)
at android.view.View.performClickInternal(View.java:7017)
at android.view.View.access$3200(View.java:784)
at android.view.View$PerformClick.run(View.java:26596)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.security.spec.NamedParameterSpec" on path: DexPathList[[zip file "/data/app/com.example.demoappjava-Uys_XXr7pCGFSFr8O20CdQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.demoappjava-Uys_XXr7pCGFSFr8O20CdQ==/lib/arm, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at software.pando.crypto.nacl.CryptoBox.<clinit>(CryptoBox.java:97)
at software.pando.crypto.nacl.CryptoBox.keyPair(CryptoBox.java:108)
at com.example.demoappjava.MainActivity.Encrypt(MainActivity.java:484)
at com.example.demoappjava.MainActivity$7.onClick(MainActivity.java:408)
at android.view.View.performClick(View.java:7044)
at android.view.View.performClickInternal(View.java:7017)
at android.view.View.access$3200(View.java:784)
at android.view.View$PerformClick.run(View.java:26596)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)