ECC中的Base64编码/解码加密和解密

时间:2017-10-02 18:02:23

标签: java android encryption cryptography spongycastle

我想测试ECC算法的加密和解密。

我将代码写入:

  • 生成密钥对(publickey和privatekey)--->将它们写入文件(可能不安全,但我只是测试)。

  • 然后使用公钥加密我的字符串(Write on Java)

  • 现在,我接受加密的字符串并尝试在Android上解密(使用私钥)

这是我的代码:

  1. 加密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();
    }
    
  2. 我的输出加密字符串如下:

    BB9Y4sKPbp28y7+FnpGnLGz5aTjD58GiIcXgjnNC4nXlUiqlHVVPc4K+ovlK5HK+Hz1Qps4ZWH9VdoYvm6VE36aEqvy53lJFnANDVpqAKuDUUaqEUrIQgF/TXFCFO22Il411atDsNhvJC3eRXbREL14duQbF8xjnLjru6WLN6GDYAQ==
    

    这是我的私钥

    -----BEGIN PRIVATE KEY-----
    MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgCybVICafKcZiEmGNw+Pj/FfL6GMgDc94/E0zJ/EKgdGgCgYIKoZIzj0DAQehRANCAATRTcV/M0OJE94qlZM7VksMOw35fcuGSxejdgzbyYllWtlIQw9gYhKPZx6t/PVOEOec4DyZq2XwaoNxBxWRboL1
    -----END PRIVATE KEY-----
    
    1. 在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();
              }
      
    2. 但是我的代码没有成功解密,它有一个问题:

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

      但我对这个问题没有多少经验。

      提前致谢

1 个答案:

答案 0 :(得分:1)

在ECIE方案中,您应该使用ECDH作为算法来生成用于加密/解密的密钥对而不是ECDSA,因为后者是数字签名算法。

KeyPairGenerator g = KeyPairGenerator.getInstance("ECDH",BouncyCastleProvider.PROVIDER)