前端加密后,传入C#后端,使用BC解密时抛出错误 Invalid point encoding xxx

使用BC 库解密时,如果密文不是 04 开头,前面要加 04. 否则会报 Invalid point encoding 77

if (!sourceData.StartsWith("04")) {
    sourceData = "04" + sourceData;
}

同理,如果是C#后端进行加密,那么传给前端进行解密时,要么在前端去掉04,要么传给前端时就去掉04



System.ArgumentException: illegal object in GetInstance: Org.BouncyCastle.As

不能使用Pkcs证书的字符串,需要使用ECKey,形如

string privateKey = @"00afe48b1181cc73af7914363e1ab986aa4041540ac8e887e8eb1874b75a9edc2e";
string publicKey = @"045101ff59e3e5b25ce549a3b5d20431c8c33afeb4128b94787b9e6acd6c457a87a51ddba7c730057e1340df4e0d07d7846ecfe1dae2279fa5c3898ad78acb9bdd";

,C#中可以使用如下生成

SMCrypto.GenerateSm2KeyHex(out string publicKey, out string privateKey)

SMCrypto使用 https://cscoder.cn/docs/base/sm_crypto/smcrypto-cs.html