Java FlexProvider-如何使用格式化密钥对进行加密/解密

Java FlexProvider-如何使用格式化密钥对进行加密/解密,java,cryptography,encryption-asymmetric,Java,Cryptography,Encryption Asymmetric,Im使用flexiprovider加密/解密,采用基于椭圆曲线的非对称算法。稍加修改,我将把公钥和私钥转换成Base64,用于个人目的,比如存储到数据库或文本文件中。我也在看这个,答案是参考这个。但我的代码是为dekstop运行的,而不是在android设备上,android和java版本的dekstop,我认为这是一个非常大的区别,只是为了清理我的问题信息。好的,在我的代码中,当我要从生成的公钥创建格式化的公钥时,我遇到了一个错误,我认为当我尝试对私钥执行此操作时,同样的问题也会发生。 现在,

Im使用flexiprovider加密/解密,采用基于椭圆曲线的非对称算法。稍加修改,我将把公钥和私钥转换成Base64,用于个人目的,比如存储到数据库或文本文件中。我也在看这个,答案是参考这个。但我的代码是为dekstop运行的,而不是在android设备上,android和java版本的dekstop,我认为这是一个非常大的区别,只是为了清理我的问题信息。好的,在我的代码中,当我要从生成的公钥创建格式化的公钥时,我遇到了一个错误,我认为当我尝试对私钥执行此操作时,同样的问题也会发生。 现在,这是我的代码:

密钥对生成器类。 根据上面的代码,我将存储字符串privateKey和publicKey。现在我要加密消息

发送方 如何使用命名曲线生成公钥:1.3.36.3.3.2.8.1.1.1

接收方只是为了获取其他信息 如果发件人已成功加密邮件,现在我将解密邮件,以下是我的代码,但不确定此操作是否运行时未出现类似上面发件人代码的错误:

 byte[] decodedPrivateKey = Base64.decode(privateKey);
 PKCS8EncodedKeySpec formatted_private = new PKCS8EncodedKeySpec(decodedPrivateKey);

 cipher.init(Cipher.DECRYPT_MODE, privKey, iesParams);
 byte[] decryptedCipher = cipher.doFinal(Ciphered);
 System.out.println("Decrypted message : "+ new String (decryptedCipher));

由于我上面的代码存在未解决的错误,因为我认为此FlexProvider与jdk-8不兼容,其中错误位于KeyFactory kf=KeyFactory.getInstanceECIES,FlexiEC line中,无法找到指定的曲线,因此我更改并决定使用BouncyCastle provider进行EC加密ECIES,它可以工作。但是我的脑海中又出现了一个问题,正如我在Flexiprovider中看到的,它使用AES128_CBC,HmacSHA1,null,null;如IESSParameterSpec所示。 但是对于bouncycastle提供程序,我找不到使用AES128_CBC作为IESSParameterSpec的IESSParameterSpec在哪里,如果我在使用此BouncyProvider时想将IESSParam更改为AES128_CBC,我该如何做? 有人请在bouncy provider中解释一下这个iesparam规范,我对这个很陌生

以下是我的信息代码:

密钥对生成器类 加密类发送方 解密类接收方
任何帮助都是非常感激的

接下来,我可以找到1.3.36.3.3.2.8.1.1.1或160位用brainpoolP160r1命名的曲线,但为什么错误会说未知命名曲线:1.3.36.3.3.2.8.1.1?我为我的jdk-8添加了jce 8,但仍然遇到了这个问题。现在我得到了答案,我只是做了这个密码;只需阅读有关标准算法名称的文档。百科全书代码对您有用吗?我在线程main java.security.InvalidKeyException中遇到异常:javax.crypto.Cipher.checkCryptoPermCipher.java:1011在javax.crypto.Cipher.initCipher.java:1209在javax.crypto.Cipher.initCipher.java:1153在FlexiEC.mainFlexiEC.java:40@DovakinSkyrim你试过用谷歌搜索那个例外吗?您可能会错过一些关于库的标准算法密钥的内容。
import (same as code above)
...
  Security.addProvider(new FlexiCoreProvider());
  Security.addProvider(new FlexiECProvider());
  Cipher cipher = Cipher.getInstance("ECIES","FlexiEC");
  IESParameterSpec iesParams = new IESParameterSpec ("AES128_CBC","HmacSHA1", null, null);

  byte[] decodedPubKey = Base64.decode(publicKey);

  X509EncodedKeySpec formatted_public = new X509EncodedKeySpec(decodedPubKey);
  KeyFactory kf = KeyFactory.getInstance("ECIES","FlexiEC");

  PublicKey publicKey = kf.generatePublic(formatted_public); // <--- I got error when hit this row

  cipher.init(Cipher.ENCRYPT_MODE, publicKey, iesParams);

  byte[] block = "this my message".getBytes();
  System.out.println("Plaintext: "+ new String(block));

  byte [] Ciphered = cipher.doFinal(block);
  System.out.println("Ciphertext : "+ Base64.encodeBytes(Ciphered));
Exception in thread "main" de.flexiprovider.api.exceptions.InvalidKeySpecException:     java.lang.RuntimeException: java.security.InvalidAlgorithmParameterException: Caught IOException("Unknown named curve: 1.3.36.3.3.2.8.1.1.1")
at de.flexiprovider.ec.keys.ECKeyFactory.generatePublic(ECKeyFactory.java:205)
at de.flexiprovider.api.keys.KeyFactory.engineGeneratePublic(KeyFactory.java:39)
at java.security.KeyFactory.generatePublic(KeyFactory.java:328)
 byte[] decodedPrivateKey = Base64.decode(privateKey);
 PKCS8EncodedKeySpec formatted_private = new PKCS8EncodedKeySpec(decodedPrivateKey);

 cipher.init(Cipher.DECRYPT_MODE, privKey, iesParams);
 byte[] decryptedCipher = cipher.doFinal(Ciphered);
 System.out.println("Decrypted message : "+ new String (decryptedCipher));
import codec.Base64; // or whatever which can use to base64 encoding
import static java.lang.System.out;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

...

   Security.addProvider(new BouncyCastleProvider());

   KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES", "BC");
   ECGenParameterSpec brainpoolP160R1 = new ECGenParameterSpec("brainpoolP160R1");

   // I'm Still using this 160 bit GF(*p*) to keep the algorithm running fast rather than 256 or above 
   kpg.initialize(brainpoolP160R1);

   KeyPair kp = kpg.generateKeyPair();

   PublicKey publicKey = kp.getPublic();
   PrivateKey privateKey = kp.getPrivate();

   byte[] PublicKey = publicKey.getEncoded();
   byte[] PrivateKey = privateKey.getEncoded();

   out.println("Encoded Public : "+Base64.encode(PublicKey));
   out.println("\nEncoded Private : "+Base64.encode(PrivateKey));
...
import codec.Base64;
import codec.CorruptedCodeException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
...
    Security.addProvider(new BouncyCastleProvider());

// Assume that we know the encoded public key then return it by decode the public key
    byte[] decodedPublic = Base64.decode("MEIwFAYHKoZIzj0CAQYJKyQDAwIIAQEBAyoABNXclcmtUt8/rlGN47pc8ZpxkWgNgtKeeHdsVD0kIWLUMEULnchGZPA=".getBytes());

    X509EncodedKeySpec formatted_public = new X509EncodedKeySpec(decodedPublic);
    KeyFactory kf = KeyFactory.getInstance("EC","BC");
    PublicKey pubKey = kf.generatePublic(formatted_public);

    Cipher c = Cipher.getInstance("ECIES", "BC");

    c.init(Cipher.ENCRYPT_MODE, pubKey); // How can i put the AES128_CBC for ies parameter ? is that possible

    byte[] cipher = c.doFinal("This is the message".getBytes());
    System.out.println("Ciphertext : "+ Base64.encode(cipher));
...
import codec.Base64;
import codec.CorruptedCodeException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
...
    Security.addProvider(new BouncyCastleProvider());
// Assume that we know the encoded private key
    byte[] decodedPrivate = Base64.decode("MHECAQAwFAYHKoZIzj0CAQYJKyQDAwIIAQEBBFYwVAIBAQQUQmA9ifs472gNHBc5NSGYq56TlOKgCwYJKyQDAwIIAQEBoSwDKgAE1dyVya1S3z+uUY3julzxmnGRaA2C0p54d2xUPSQhYtQwRQudyEZk8A==".getBytes());

    PKCS8EncodedKeySpec formatted_private = new PKCS8EncodedKeySpec(decodedPrivate);
    KeyFactory kf = KeyFactory.getInstance("EC", "BC");
    PrivateKey privKey = kf.generatePrivate(formatted_private);

    Cipher c = Cipher.getInstance("ECIES");
    c.init(Cipher.DECRYPT_MODE, privKey); //How can i adding the **AES128_CBC** ies param ?

// Assume that we know the encoded cipher text
    byte[] plaintext = c.doFinal(Base64.decode("BKbCsKY7gDPld+F4jauQXvKSayYCt6vOjIGbsyXo5fHWo4Ax+Nt5BQ5FlkAGksFNRQ46agzfxjfuoxWkVfnt4gReDmpPYueUbiRiHp1Gwp0="));
    System.out.println("\nPlaintext : "+ new String (plaintext));
...