首页 新闻 会员 周边 捐助

有没有大神可以把Java代码翻译成C#的

0
悬赏园豆:10 [待解决问题]

本人小白,求帮忙
把Java代码翻译成C#的

import java.net.URL;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.Formatter;
import java.util.Random;

/**

  • 计算dd.config的签名参数
    **/
    public class DdConfigSign {

    /**

    • 计算dd.config的签名参数
    • @param jsticket 通过微应用appKey获取的jsticket
    • @param nonceStr 自定义固定字符串
    • @param timeStamp 当前时间戳
    • @param url 调用dd.config的当前页面URL
    • @return
    • @throws Exception
      */
      public static String sign(String jsticket, String nonceStr, long timeStamp, String url) throws Exception {
      String plain = "jsapi_ticket=" + jsticket + "&noncestr=" + nonceStr + "&timestamp=" + String.valueOf(timeStamp)
      + "&url=" + decodeUrl(url);
      try {
      MessageDigest sha1 = MessageDigest.getInstance("SHA-256");
      sha1.reset();
      sha1.update(plain.getBytes("UTF-8"));
      return byteToHex(sha1.digest());
      } catch (Exception e) {
      throw new Exception(e.getMessage());
      }
      }

    // 字节数组转化成十六进制字符串
    private static String byteToHex(final byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
    formatter.format("%02x", b);
    }
    String result = formatter.toString();
    formatter.close();
    return result;
    }

    /**

    • 因为ios端上传递的url是encode过的,android是原始的url。开发者使用的也是原始url,
    • 所以需要把参数进行一般urlDecode
    • @param url
    • @return
    • @throws Exception
      */
      private static String decodeUrl(String url) throws Exception {
      URL urler = new URL(url);
      StringBuilder urlBuffer = new StringBuilder();
      urlBuffer.append(urler.getProtocol());
      urlBuffer.append("😊;
      if (urler.getAuthority() != null && urler.getAuthority().length() > 0) {
      urlBuffer.append("//");
      urlBuffer.append(urler.getAuthority());
      }
      if (urler.getPath() != null) {
      urlBuffer.append(urler.getPath());
      }
      if (urler.getQuery() != null) {
      urlBuffer.append('?');
      urlBuffer.append(URLDecoder.decode(urler.getQuery(), "utf-8"));
      }
      return urlBuffer.toString();
      }

    public static String getRandomStr(int count) {
    String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < count; i++) {
    int number = random.nextInt(base.length());
    sb.append(base.charAt(number));
    }
    return sb.toString();
    }
    }

合仔茶的主页 合仔茶 | 初学一级 | 园豆:143
提问于:2022-01-12 01:48
< >
分享
所有回答(1)
1

好多年没写java了,不过大致能明白要做什么,不保证代码是对的,你可以参考下


using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;

public class DdConfigSign {
    public static string sign(string jsticket, string nonceStr, long timeStamp, string url) {
        string plain = "jsapi_ticket=" + jsticket + "&noncestr=" + nonceStr + "&timestamp=" + timeStamp
+ "&url=" + decodeUrl(url);
        try {
            //对字符串进行sha256加密,输出十六进制小写字符串
            byte[] hash = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(plain));
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        } catch (Exception e) {
            throw new Exception(e.ToString());
        }
    }
    /// <summary>
    /// 对url的query中的值进行urlDecode
    /// </summary> 
    private static string decodeUrl(string url) {
        string[] ss = url.Split('?');
        StringBuilder sb = new StringBuilder();
        sb.Append(ss[0]);
        sb.Append("?");
        foreach (var query in ss[1].Split('&')) {
            if (query.Length > 0 && query.Contains("=")) {
                string[] kv = query.Split('=');
                if (kv.Length == 2) {
                    sb.Append(kv[0] + "=" + HttpUtility.UrlDecode(kv[1], Encoding.UTF8));
                }
            }
        }
        return sb.ToString();
    }
    /// <summary>
    /// 获得指定长度的随机字符串
    /// </summary> 
    public static string getRandomStr(int count) {
        string baseStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random rd = new Random();
        for (int i = 0; i < count; i++) {
            sb.Append(baseStr[rd.Next(0, baseStr.Length)]);
        }
        return sb.ToString();
    }
}
WmW | 园豆:424 (菜鸟二级) | 2022-01-12 10:09

卧槽,不讲武德,我还想着挣几个豆呢

支持(0) 反对(0) echo_lovely | 园豆:1581 (小虾三级) | 2022-01-12 10:26

感谢大神,高效

支持(0) 反对(0) 合仔茶 | 园豆:143 (初学一级) | 2022-01-12 14:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册