我想测试ECC算法加密和解密。
我写代码:
生成密钥对(公钥和私钥)---> 将它们写入文件(可能不安全,但我只是测试)。
然后使用公钥加密我的字符串(在 Java 上编写)
现在,我采用加密字符串并尝试在 Android 上对其进行解密(使用私钥)
这是我的代码:
在 Java 中加密字符串:
/// generate key pair ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1"); try { KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA",BouncyCastleProvider.PROVIDER_NAME); g.initialize(spec, new SecureRandom()); KeyPair keyPair = g.generateKeyPair(); privateKey = keyPair.getPrivate(); publicKey = keyPair.getPublic(); System.out.println("PublicKey:"+publicKey+"\n"); System.out.println("PrivateKey:"+privateKey+"\n"); } catch (Exception e) { e.printStackTrace(); } try { Cipher c = Cipher.getInstance("ECIES",BouncyCastleProvider.PROVIDER_NAME); c.init(Cipher.ENCRYPT_MODE,publicKey); encodeBytes = c.doFinal(origin.getBytes()); String encrypt = Base64.getEncoder().encodeToString(encodeBytes); System.out.println(encrypt); } catch (Exception e) { e.printStackTrace(); }
我的输出加密字符串是这样的:
BB9Y4sKPbp28y7+FnpGnLGz5aTjD58GiIcXgjnNC4nXlUiqlHVVPc4K+ovlK5HK+Hz1Qps4ZWH9VdoYvm6VE36aEqvy53lJFnANDVpqAKuDUUaqEUrIQgF/TXFCFO22Il411atDsNhvJC3eRXbREL14duQbF8xjnLjru6WLN6GDYAQ==
这是我的私钥
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgCybVICafKcZiEmGNw+Pj/FfL6GMgDc94/E0zJ/EKgdGgCgYIKoZIzj0DAQehRANCAATRTcV/M0OJE94qlZM7VksMOw35fcuGSxejdgzbyYllWtlIQw9gYhKPZx6t/PVOEOec4DyZq2XwaoNxBxWRboL1
-----END PRIVATE KEY-----
在Android上解密字符串
encodeBytes = Base64.decode(encrypted_string_above,Base64.DEFAULT); try { KeyFactory factory = KeyFactory.getInstance("ECDSA", "SC"); String state; state = Environment.getExternalStorageState(); File root = Environment.getExternalStorageDirectory(); File dir = new File(root.getAbsolutePath()+"/disk"); privateKeyFromFile = generatePrivateKey(factory,dir+"/private.pem"); Cipher c = Cipher.getInstance("ECIES","SC"); c.init(Cipher.DECRYPT_MODE,privateKeyFromFile); decodeBytes = c.doFinal(encodeBytes); String deCrypt = new String(decodeBytes,"UTF-8"); txtHiden.setText(deCrypt); Toast.makeText(activity, deCrypt, Toast.LENGTH_SHORT).show(); } catch (Exception ex) { ex.printStackTrace(); }
但是我的代码没有成功解密,有问题:
10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err: org.spongycastle.jcajce.provider.util.BadBlockException: unable to process block
10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err: at org.spongycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(IESCipher.java:476)
10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err: at javax.crypto.Cipher.doFinal(Cipher.java:2056)
10-03 00:52:18.264 28656-28656/com.example.napoleon.luanvana W/System.err: at com.example.napoleon.luanvana.MessageFragment$3.onClick(MessageFragment.java:156)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.view.View.performClick(View.java:6213)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.widget.TextView.performClick(TextView.java:11074)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.view.View$PerformClick.run(View.java:23645)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.os.Looper.loop(Looper.java:154)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6692)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: Caused by: org.spongycastle.crypto.InvalidCipherTextException: unable to recover ephemeral public key: Sender's public key has invalid point encoding 0x51
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at org.spongycastle.crypto.engines.IESEngine.processBlock(IESEngine.java:409)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at org.spongycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(IESCipher.java:472)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: ... 12 more
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: Caused by: java.io.IOException: Sender's public key has invalid point encoding 0x51
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at org.spongycastle.crypto.parsers.ECIESPublicKeyParser.readKey(ECIESPublicKeyParser.java:46)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: at org.spongycastle.crypto.engines.IESEngine.processBlock(IESEngine.java:405)
10-03 00:52:18.265 28656-28656/com.example.napoleon.luanvana W/System.err: ... 13 more
我认为这可能是由于错误的编码/解码字符串 - 字节 - Base64
(Enctypt side)String encrypt = Base64.getEncoder().encodeToString(encodeBytes);
(Decrypt side): encodeBytes = Base64.decode(encrypted_string_above,Base64.DEFAULT);
但是我在这个问题上没有太多经验。