Ruby C# 的代码, 有没有大神帮忙翻译成 Ruby 感谢

yjiiyygy · 2015年02月04日 · 最后由 yjiiyygy 回复于 2015年02月05日 · 2949 次阅读

C#的代码,有没有大神帮忙翻译成 ruby 感谢

private static string myEncoder(string a, byte[] c, bool isToBase64)
{
  string result = "";
  List<Byte> bytesR = new List<byte>();
  int f = 0, h = 0, q = 0;
  int[] b = new int[256];
  for (int i = 0; i < 256; i++)
    b[i] = i;
  while (h < 256)
  {
    f = (f + b[h] + a[h % a.Length]) % 256;
    int temp = b[h];
    b[h] = b[f];
    b[f] = temp;
    h++;
  }
  f = 0; h = 0; q = 0;
  while (q < c.Length)
  {
    h = (h + 1) % 256;
    f = (f + b[h]) % 256;
    int temp = b[h];
    b[h] = b[f];
    b[f] = temp;
    byte[] bytes = new byte[] { (byte)(c[q] ^ b[(b[h] + b[f]) % 256]) };
    bytesR.Add(bytes[0]);
    result += System.Text.ASCIIEncoding.ASCII.GetString(bytes);
    q++;
  }
  if (isToBase64)
  {
    Byte[] byteR = bytesR.ToArray();
    result = Convert.ToBase64String(byteR);
  }
  return result;
}
public static void getEp(string vid, string ep, ref string pNew, ref string token, ref string sid)
{
  string template1 = "becaf9be";
  string template2 = "bf7e5f01";
  byte[] bytes = Convert.FromBase64String(ep);
  ep = ystem.Text.ASCIIEncoding.ASCII.GetString(bytes);
  string temp = myEncoder(template1, bytes, false);
  string[] part = temp.Split('_');
  sid = part[0];
  token = part[1];
  string whole = string.Format("{0}_{1}_{2}", sid, vid, token);
  byte[] newbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(whole);
  epNew = myEncoder(template2, newbytes, true);
}

@yjiiyygy 能格式化下么?https://help.github.com/articles/markdown-basics/ 这个是 markdown 的帮助文档

我刚好开着 VS,翻译的事,还是请有爱心和闲心的来帮忙吧。 楼主,你确定你不用学习 ruby?如果要学,可以自己试着一步步来啊。

private static string myEncoder(string a, byte[] c, bool isToBase64)
{
  string result = "";
  List<Byte> bytesR = new List<byte>();
  int f = 0, h = 0, q = 0;
  int[] b = new int[256];
  for (int i = 0; i < 256; i++)
    b[i] = i;
  while (h < 256)
  {
    f = (f + b[h] + a[h % a.Length]) % 256;
    int temp = b[h];
    b[h] = b[f];
    b[f] = temp;
    h++;
  }
  f = 0; h = 0; q = 0;
  while (q < c.Length)
  {
    h = (h + 1) % 256;
    f = (f + b[h]) % 256;
    int temp = b[h];
    b[h] = b[f];
    b[f] = temp;
    byte[] bytes = new byte[] { (byte)(c[q] ^ b[(b[h] + b[f]) % 256]) };
    bytesR.Add(bytes[0]);
    result += System.Text.ASCIIEncoding.ASCII.GetString(bytes);
    q++;
  }
  if (isToBase64)
  {
    Byte[] byteR = bytesR.ToArray();
    result = Convert.ToBase64String(byteR);
  }
  return result;
}
public static void getEp(string vid, string ep, ref string pNew, ref string token, ref string sid)
{
  string template1 = "becaf9be";
  string template2 = "bf7e5f01";
  byte[] bytes = Convert.FromBase64String(ep);
  ep = ystem.Text.ASCIIEncoding.ASCII.GetString(bytes);
  string temp = myEncoder(template1, bytes, false);
  string[] part = temp.Split('_');
  sid = part[0];
  token = part[1];
  string whole = string.Format("{0}_{1}_{2}", sid, vid, token);
  byte[] newbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(whole);
  epNew = myEncoder(template2, newbytes, true);
}

#2 楼 @Peter 我還以為你這代碼是翻譯,一看 private static string ....

#2 楼 @Peter 感谢格式化以下,哈哈。不是不学,是有急用,一时半会估计也搞不完,有劳哪位大神帮忙翻译以下,我还是比较喜欢 ruby 的,只是很多函数不知道如何用。看文档 也是一知半解的。

#4 楼 @yjiiyygy 我試了一下,別的都好說,但是我不知道 Convert.FromBase64String, Convert.ToBase64String Ruby 裡面有沒有,是 Base64::strict_encode64 之類的東西麼?

#5 楼 @blacktulip 哈哈 不是很清楚。。我看文档里的 我都用过了,不行。。。感谢哈

大神呢,求帮助啊...有几个函数不是很会啊。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号