签名说明
第一步:生成加密串
on Ramp是将您的address+appId参数拼接生成加密串
示例:address=0xeaf936e4bd0cf40958e74fce896e976459a83b90&appId=f83Is2y7L425rxl8
off Ramp是将需要签名的参数按照字母升序拼接生成字符串
示例:appId=f83Is2y7L425rxl8&callbackUrl=http://www.google.com/&cryptoAmount=55&fiat=USD&urlType=web&withdrawUrl=https://www.baidu.com
第二步:生成您的签名
将加密串和appsecret作为参数按照以下方法生成签名
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
public static String encrypt(String plainText, String secretKeyData) {
try {
byte[] plainTextData = plainText.getBytes(StandardCharsets.UTF_8);
byte[] secretKey = secretKeyData.getBytes(StandardCharsets.UTF_8);
String iv = new String(secretKey).substring(0, 16);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
byte[] dataBytes = plainTextData;
int plaintextLength = dataBytes.length;
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new String(Base64.getEncoder().encode(encrypted));
} catch (Exception e) {
System.out.println("AES encrypting exception , msg is {}" + e.toString());
}
return null;
}
public static String decrypt(String cipherText, String secretKeyData) {
try {
byte[] cipherTextData = cipherText.getBytes(StandardCharsets.UTF_8);
byte[] secretKey = secretKeyData.getBytes(StandardCharsets.UTF_8);
String iv = new String(secretKey).substring(0, 16);
byte[] encrypted = Base64.getDecoder().decode(cipherTextData);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println("AES decrypting exception: msg is {}" + e.toString());
}
return null;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String rawData = "address=0x755CA9224E613252c06ae9C20825113Dda1971aa11660287337793&appId=f83Is2**********;
String sign = encrypt(rawData, "4Yn*************");
System.out.println("sign is: " + sign);
System.out.println("\n");
String urlEncodeData = URLEncoder.encode(sign, "UTF-8");
System.out.println("encode sign is: " +urlEncodeData);
}
}
//Python 3.7.4
import base64
# pip install pycryptodomex
from Cryptodome.Cipher import AES
import urllib
import requests
def encrypt(plainText, secretKeyData):
plainTextData = plainText.encode('UTF-8')
secretKey = secretKeyData.encode('UTF-8')
iv = secretKey[0:16]
cipher = AES.new(secretKey, AES.MODE_CBC, iv)
padded_plaintext = pad(plainTextData, AES.block_size)
ciphertext = cipher.encrypt(padded_plaintext)
return base64.b64encode(ciphertext).decode()
def pad(data, block_size):
padding_len = block_size - (len(data) % block_size)
return data + (bytes([padding_len]) * padding_len)
rawdata = rawData = "address=0x755CA9224E613252c06ae9C2082s5113Dda1971aa11660287337793&appId=f83Is2**********
sign = encrypt(rawData, "sy**gl*k*khiov")
print("sign is:", sign)
urlEncodeData = urllib.parse.quote_plus(sign)
print("encode sign is: ", urlEncodeDat)
//PHP 8.0.7
<?php
function encrypt($plaintext,$secretKey){
$plaintextData = utf8_encode($plaintext);
$secretKeyData = utf8_encode($secretKey);
$iv = substr($secretKey, 0, 16);
$ciphertext = openssl_encrypt($plaintextData, 'AES-128-CBC', $secretKeyData, OPENSSL_RAW_DATA, $iv);
return base64_encode($ciphertext);
}
$rawData = "address=0x755CA9224E613252c06ae9C20825113Dda1971aa11660287337793&appId=f83Is2**********;
$sign = encrypt($rawData, "4Yn*************" );
printf("sign is: %s\n",$sign);
$urlEncodeData = urlencode($sign);
printf("encode sign is:: %s",$urlEncodeData);
?>
//node v14.15.1
const crypto = require('crypto');
function encrypt(plainText, secretKeyData) {
try {
const plainTextData = Buffer.from(plainText, 'utf8');
const secretKey = Buffer.from(secretKeyData, 'utf8');
const iv = secretKeyData.substring(0, 16);
const cipher = crypto.createCipheriv('aes-128-cbc', secretKey, iv);
let encrypted = cipher.update(plainTextData);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString('base64');
} catch (e) {
console.log(`AES encrypting exception, msg is ${e.toString()}`);
}
return null;
}
const plaintext = 'address=0x890D761D8E8c519BE37f82A15e0391B99Eb796A9&appId=f83Is2**********&fiat=USD&fiatAmount=185';
const ciphertext = encrypt(plaintext, "4Yn*************"); //appSecret
console.log("sign is ",ciphertext);
const urlEncodeText = encodeURIComponent(ciphertext)
console.log("encode sign is ",urlEncodeText);
//go version go1.18.2
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"net/url"
)
func encrypt(plainText, secretKeyData string) string {
plainTextData := []byte(plainText)
secretKey := []byte(secretKeyData)
iv := secretKey[:16]
c, err := aes.NewCipher(secretKey)
if err != nil {
fmt.Println("AES encrypting exception, msg is:", err)
return ""
}
blockSize := c.BlockSize()
plaintext := PKCS5Padding(plainTextData, blockSize)
ivSpec := iv
mode := cipher.NewCBCEncrypter(c, ivSpec)
encrypted := make([]byte, len(plaintext))
mode.CryptBlocks(encrypted, plaintext)
return base64.StdEncoding.EncodeToString(encrypted)
}
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func main() {
rawData := "address=0x755CA9224E613252c06ae9C20825113Dda1971aa11660287337793&appId=f83Is2**********"
sign := encrypt(rawData, "4Yn*************")
fmt.Printf("sign is %s \n", sign)
encodeSign := url.QueryEscape(sign)
fmt.Printf("encode sign is %s \n", encodeSign)
}
import 'dart:convert';
import 'package:encrypt/encrypt.dart';
class AESUtil {
static String encrypt(String plainText, String secretKeyData) {
try {
final plainTextData = utf8.encode(plainText);
final secretKey = utf8.encode(secretKeyData);
final iv = secretKeyData.substring(0, 16);
final key = Key(secretKey);
final ivSpec = IV(utf8.encode(iv));
final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: 'PKCS5'));
final encrypted = encrypter.encryptBytes(plainTextData, iv: ivSpec).bytes;
return base64.encode(encrypted);
} catch (e) {
print('AES encrypting exception, msg is $e');
}
return null;
}
static String decrypt(String cipherText, String secretKeyData) {
try {
final cipherTextData = utf8.encode(cipherText);
final secretKey = utf8.encode(secretKeyData);
final iv = secretKeyData.substring(0, 16);
final key = Key(secretKey);
final ivSpec = IV(utf8.encode(iv));
final encrypter = Encrypter(AES(key, mode: AESMode.cbc, padding: 'PKCS5'));
final decrypted = encrypter.decryptBytes(Encrypted(cipherTextData), iv: ivSpec).bytes;
return utf8.decode(decrypted);
} catch (e) {
print('AES decrypting exception: msg is $e');
}
return null;
}
}
void main() {
final rawData = 'address=0x755CA9224E613252c06ae9C20825113Dda1971aa11660287337793&appId=f83Is2**********';
final sign = AESUtil.encrypt(rawData, '4Yn*************');
print('sign is: $sign\n');
final urlEncodeData = Uri.encodeQueryComponent(sign);
print('encode sign is: $urlEncodeData');
}
Updated about 1 year ago