leaningtech / cheerpj-meta

Run Java 8 applications, libraries, applets, Java Web Start, and Oracle Forms on the web without legacy plugins.
https://labs.leaningtech.com/cheerpj
464 stars 21 forks source link

error: package org.bouncycastle.jcajce.provider.asymmetric.ec does not exist import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; #172

Closed xlei1123 closed 1 year ago

xlei1123 commented 1 year ago

i use online demo(https://javafiddle.leaningtech.com/) then replace my code, but it throw error; Here is my code ` import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PublicKey; import java.security.SecureRandom; import java.security.spec.ECGenParameterSpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.util.Base64Utils;

public class SM2Util { public SM2Util() { }

public static KeyPair generateSm2KeyPair() {
    ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");

    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
        kpg.initialize(sm2Spec, new SecureRandom());
        return kpg.generateKeyPair();
    } catch (Exception var3) {
        var3.printStackTrace();
        return null;
    }
}

public static byte[] getRawPublicKey(String publicKey) {
    PublicKey publicKeyObject = createPublicKey(publicKey);
    if (publicKeyObject != null) {
        byte[] src65 = ((BCECPublicKey)publicKeyObject).getQ().getEncoded(false);
        byte[] rawXY = new byte[64];
        System.arraycopy(src65, 1, rawXY, 0, rawXY.length);
        return rawXY;
    } else {
        return null;
    }
}

private static PublicKey createPublicKey(String publicKey) {
    PublicKey publickey = null;

    try {
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
        KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
        publickey = keyFactory.generatePublic(publicKeySpec);
        return publickey;
    } catch (Exception var4) {
        var4.printStackTrace();
        return null;
    }
}

public static void main(String[] args) throws Exception {
    KeyPair keyPair = generateSm2KeyPair();
    String plaintext = "test";
    System.out.println(Base64Utils.encode(keyPair.getPublic().getEncoded()));
    byte[] bytes = getRawPublicKey(new String(Base64Utils.encode(keyPair.getPublic().getEncoded())));
    System.out.println(new String(Base64Utils.encode(bytes)));
}

} `

alexp-sssup commented 1 year ago

JavaFiddle only supports Java classes that are part of the Java runtime (OpenJDK)

bouncycastle (or any other third party library) cannot be used out of the box.

You can use this library by manually integrating CheerpJ into your own HTML page, please start from our tutorial here: https://docs.leaningtech.com/cheerpj/Getting-Started

For further support please join our discord: https://discord.gg/CHeATR6E9k