package sm2 import ( "encoding/base64" "encoding/json" "fmt" "testing" ) const ( SELF_PRI = "BD13D44C74422F80ADDF54AD2DE2888C4092AF6F61E41FABADA6C47F17574A41" SELF_PUB = "04F26C6CDA5E58153999FDF3023259F16EDE0A556C39FCF4BC97C04A10A8A6CF1D52297B53DB4DD72FEEF8221394C8478E6424F4E84E4A6E2784C1A4A1C99F2DC2" ) func TestGenerateSM2KeyPair(t *testing.T) { // Generate a new SM2 key pair privateKey, publicKey := GenerateKey() // Print the private and public keys fmt.Printf("Private Key: %s\n", privateKey) fmt.Printf("Public Key: %s\n", publicKey) data := "{\"name\":\"张三\",\"sex\":1,\"is_human\":true}" en, err := SM2Encrypt(data, publicKey) if err != nil { panic(err) } decrypt, err := SM2Decrypt(en, publicKey, privateKey) if err != nil { panic(err) } t.Log(decrypt) } func TestSM2Encrypt(t *testing.T) { t.Log(encrypt()) } func TestSM2Decrypt(t *testing.T) { en := "TURRek1HUXlOemhqTkdNMVl6aGxPRFZpTURCalpHVmhaV1E1WkRSaVpqVTFOekJsWkdJMk4yVTRPR0ptWVRrME5tRTROMkkzWW1FM09EUm1aVFptWVRGak0yUTRaakV3WW1FMFlqVTBZMll6TVRBek9EZGxabVJoWVRRMk1HUmpNRFU0WkdReE9XSmhZelV6TkRaak5XSmxNVE14Wm1Wa1ltRTJZVE0yWWpnelpXWTBNbVl4T1RabU5EWXpNMlk1WVdNeE4yVXpOalZpT0RabU1qQXhZbU0wTXpjM01qRmtZV1U1WVdJeE5ESTVPVE00TmpabVpXRXhNelV5Tm1aaE5EVmhaRGxrTTJWaU4yUm1aR1U0T0dRNVpUQXpabUl3TmpabE16TTFOekl4TnpsaFpETXhOV05oWmpNMk4yRmlOVEl5TlRRM1lUVTVNakJqWW1Nek1HRXpOR1l3T1RKa00ySmpPV0ZsTVRBeVkyVm1ZVEZqTkRRM01EaG1aV1UzTnpSallqWmpZVEZtWTJVMVpUYzJOMlE0WmpoaE1UTXlOMkU1WWpVek5tUTBORGcyTjJaaFlqazJaRGd5T0RBME9ETmtZVFZqTUdFMk1qUmhZV1ptWTJSa09UZGlOemN4WlRaaFltSmhNR0kzT0RNNFpETXdOV1UyWTJGbU5tVmtZak5pTURCbU4yVTJaakkxTkRWa1pqWTBPR05rTURBeE5XTmxOamt5TkRJNE1tTmlORE14T0dRM01USXpaREkxWW1ZeE1qRmxPRFU0TlRkall6RTJPVFJoWTJWak9EbGxPV0kyTlRVeFpUYzJOemsxTTJJMU56VTNNall4T1dGbU1EWTBabU0yTkdOaVkyUXhaVFZsTUdGa09EUTBNVFZqTkdVNE5tWXlaakpsT1dVM09Ea3pZVEZsTmpJMllqRmlaakl5T1dFeE9USXpNR0pqWVRjeU1ERmpOV1V4TXprME9UbGtOemN6TWpReVkySm1ORGMxTVdObFpHUTJOR05tTlRjPQ==" decrypt, err := SM2Decrypt(en, SELF_PUB, SELF_PRI) if err != nil { panic(err) } t.Log(decrypt) } func encrypt() string { data := "{\"pay_channel_id\":1729382256910270476,\"out_trade_no\":\"zxp1234D56711tuikuan\",\"order_type\":1,\"amount\":1,\"desc\":\"测试支付宝退款商品zxp\",\"ext_json\":\"\",\"app_id\":1729382256910270475,\"timestamp\":1723096731}" //data := "{\"refund_out_trade_no\":\"zxp1234D56711tuikuan\",\"pay_channel_id\":1729382256910270476,\"out_trade_no\":\"4123412341234\",\"order_type\":2,\"amount\":1,\"desc\":\"测试支付宝退款商品zxp\",\"ext_json\":\"\",\"app_id\":1729382256910270475,\"timestamp\":1723096731}" en, err := SM2Encrypt(data, SELF_PUB) if err != nil { panic(err) } return en } func TestSm2Decode(t *testing.T) { en := "TURRd1lUZGlZamt4T1RGbU9XWmhOR1pqTkRKa05HWmpORFkyTjJNd05qVmtNR1EwTTJNellUVmhPR1ZsTm1GbVpqZzFPRGMwTW1Oak5EQmtaRGszTkRrek5tRTVNREEwTkdFMFlXRXhaREl5T0RVME1ESmtObVk1TWpWaVpqYzNORGhoT1RObE0yVmtaVGc1T0dVellUZGlabU15WkRNNU1EQXpNR0ZsTm1Oa05XSTBPVEJpT1dFMU9XUTVObU15TURCaU9EVTNNR0UzWTJFNU9UYzVNalF3TnpFeVl6RTVZemd4TW1WbVpUQTNZbVV5WVRoaU1tRmtaak5tWkRjek1XVXdabVZrTlRnMVpUTXlNR0U0WldWaVlURXlNREExWWpnek1XRTBNVFk1WlRCa09ERXpaVGN6WWpJMFpEUmpOVFV5WlRRMU1HTmxNbVEwWlRkbU5tTTFaR1l5TUdZeU5XTXdNV1kwTVdFNU5EZGxaRFV3TnpjME9EWTFaVFJoWVRreU5HTm1ZVFU1WXpoa01USmxNREF3WTJOa01qRTVNell6T1RObFpXWXpNelEyTnpObU9HTTJaV00xTkdVd1ltRmhOVFZsWVRka09UQTFaRFZrTXpobVlqSTJabVEzWldFelptWmpNbU5tTldFd01tUmhOak5qTWpBeVl6azVaR0poT0RRMFpHTTJNbVJtTkdRM05Ea3dZems0TmprMk9UTmpNbVF3TnpFeE1qUmlaVFJpT1RrMk9UY3pNR0prTmpZNVpqYzFPRFJpWVRCbE5tTXdZV1JqTnpOak4yVmpaR1ExWmpJellUWmtaR1EwTVRCbE1qSTVZV1F5WW1GbFl6YzRPRGhsWXpjME5HTmlNekk0TnpRek1UZGhNak0xWVRWbFlUYzNNR1JsTVRZMU1tWmpOVEEzT1dVMllqSTNabUl5TVRNM09URmpPRFF4TXpsa09ETm1OelExWTJVNVlUTXlZams0TVRZd1pqUXdNV1k1TjJSbVpEazRZV1kxWXpZeE1USTRaREEzT0dRME5tVXlaQT09" content, _ := base64.StdEncoding.DecodeString(en) decrypt, err := SM2Decrypt(string(content), SELF_PUB, SELF_PRI) if err != nil { panic(err) } t.Log(decrypt) tempMap := make(map[string]interface{}) json.Unmarshal([]byte(decrypt), &tempMap) t.Log(tempMap) }