package rsa import ( "PaymentCenter/app/utils/encrypt/aes" "encoding/base64" "fmt" "testing" ) const ( //PRI = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmjK6SPkrP03ssex5Dxn4KZeVm9rQoJMslru13GNgoGKJAq13xwUGl6lpGlhzM9IMzg0ldOO2a0SQPPjc0fLrDjIgHPfIT6H1hDGkZdPaF4D0OfiHGzlLwXq7FyyyGc2JXILsniMBXcCxNFhbW0jAaJyYrBJb3q84a5y5AtK3IC+eDV6Bj2J4HlQVGKgW9u2ic6Jxl23sacXY6iifi+KEuoXNCJrmYlgRWTaQovLmTCLkErkzxzG9DFRDWGoz25LthDPqcCSUmWEbJ+obwIGB4r2WCbFXvaeVBQORlyVRyNUvYMItjHBQIKinDWZ6y8KzA0YKOoxEfr0KfE8Uk4PQBAgMBAAECggEABTAX0PzelN4uyvTT0sMi/R0YRKPgepP40vtBsNvF10E7Lp4ClAupHpYFSrJq178xu1/2dVBXEGM9hw8GUQd7RCjuD3cFwcp/EKU+Zy6uQ38iZRTskEDa3bC+q3EXzuFXDxqOfIhai262UTlkATw0sjUwJRdkbMxoeWHkSNuH7UBVddxFL8Bq1DKaPzRCqQ8zlkMZHy8Xbf2b8rFoi1oPBoPjHyxCo33zcnSg5xntIoA5pkD6x4z5cAnU55aBoYUiRv7jmG+MVp1jpDvAmJLfUayVZNakgX1r74bMPsl9kpA7dVdgOlWrIkbJE1plVXXswBb8QKN0/Yx2vv/YASSi0QKBgQDaO9BkRjvht/lrsQEur1wXf5ITnsVWsqlUhYQKGHihzOj7e0rGO9QICM4eQZH9ssHfxEXhmEoFdkaqo3Fo47NI+yinpWm+KruwrRFkCGejlKZ4bhn9zWPb8L1qJbN4UD1c5jUNk1B0EEdnLRFg0/O7xm602bGilvY5x2nf0v95+QKBgQDDXyiiGNV7GO4h8OQYJwq0IqenPyIanRgYI3rw//tg147mhWcxT6ms6dMUh9nEXEFali2J1En+aVvx1Xn47CuGrRmZOLaGkw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2Dl7GZ7Ak0gWy1iFvFQm/6Y4p7dA44foAg9MwbV1wYbrdGauC5o7VRg0x6Sklzu/z5LeU4YUMruT0cd/Ov2kXkqiDyMn3GzdmIIWFadXISTbr7s1yU4uovn8=" privateKeyPEM := `-----BEGIN RSA PRIVATE KEY----- ` + PRI + ` -----END RSA PRIVATE KEY-----` res, err := Decrypt(privateKeyPEM, data) fmt.Println(string(res), err) } func encrypt() string { data := "{\"pay_channel_id\":1729382256910270476,\"out_trade_no\":\"zxp1234D56711tuikuan\",\"order_type\":1,\"amount\":1,\"desc\":\"测试支付宝退款商品zxp\",\"ext_json\":\"\",\"app_id\":8070450532247928844,\"timestamp\":1723096731}" pub := `-----BEGIN PUBLIC KEY----- ` + PUB + ` -----END PUBLIC KEY-----` en, err := Encrypt(pub, data) if err != nil { panic(err) } return base64.StdEncoding.EncodeToString(en) } func encryptWithAes() string { data := "{\"pay_channel_id\":8935141660703064070,\"out_trade_no\":\"refundOutTreadNo001\",\"amount\":1,\"desc\":\"退款\",\"ext_json\":\"\",\"app_id\":5476377146882523138,\"timestamp\":53612533412643,\"refund_out_trade_no\":\"asdadasdas\"}" aes.Encrypt([]byte(data), []byte(aes.TestKey)) dataJson := base64.StdEncoding.EncodeToString([]byte(data)) pub := `-----BEGIN PUBLIC KEY----- ` + PUB + ` -----END PUBLIC KEY-----` en, err := Encrypt(pub, dataJson) if err != nil { panic(err) } return base64.StdEncoding.EncodeToString(en) } // 测试生成密钥对 func TestGenerateRSAKey(t *testing.T) { pub, pri, _ := GenerateKey() fmt.Println("pub:", pub, "\n", "pri:", pri) }