简体   繁体   English

将代码段从C#转换为VBScript

[英]Convert snippet from c# to vbscript

So I have a situation where I need to pass some parameters on url. 所以我有一种情况,我需要在url上传递一些参数。 In order to not have id=1 on the url, I added a simple encryption method to obfuscate the values. 为了使URL上没有id = 1,我添加了一种简单的加密方法来混淆值。 This has worked fine within the .Net land. 在.Net范围内,此方法运行良好。 Now however, I need to direct from a classic asp page to this .net page that is expecting the parameters to be encrypted. 但是,现在,我需要从经典的ASP页面定向到该.net页面,该页面希望对参数进行加密。 I really am not too familiar with encryption or classic asp and was hoping someone would be able to direct me to a good JS lib, or simply provide a classic asp version of this function? 我真的对加密或经典ASP不太熟悉,希望有人能够将我定向到一个好的JS库,或者只是提供此功能的经典ASP版本? If there's anything wrong with the .Net code, I'd love to hear feedback on that as well. 如果.Net代码有任何问题,我也希望听听对此的反馈。

Here's the encryption method: 这是加密方法:

public static string Encrypt(string Input)
{
    try
    {
        key = Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));
        var des = new DESCryptoServiceProvider();
        Byte[] inputByteArray = Encoding.UTF8.GetBytes(Input);
        var ms = new MemoryStream();
        var cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        return Convert.ToBase64String(ms.ToArray());
    }
    catch (Exception)
    {
        return "";
    }
}

And here's the decryption method (I need this to decrypt the classic asp encrypted text): 这是解密方法(我需要用它来解密经典的asp加密文本):

public static string Decrypt(string Input)
{
    try
    {
        key = Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));
        var des = new DESCryptoServiceProvider();
        var inputByteArray = Convert.FromBase64String(Input);
        var ms = new MemoryStream();
        var cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        Encoding encoding = Encoding.UTF8;
        return encoding.GetString(ms.ToArray());

    }
    catch (Exception)
    {
        return "";
    }
}

Thanks for any help! 谢谢你的帮助!

This isn't some simple translation! 这不是简单的翻译! Classic ASP doesn't have access to the .NET Framework. 经典ASP无法访问.NET Framework。 You would need to do all of this in Win32 code. 您将需要在Win32代码中完成所有这些操作。

You should package the C# code together as a COM component, which can then be accessed from the Classic ASP site. 您应该将C#代码打包为一个COM组件,然后可以从Classic ASP站点进行访问。

I ran into this one on our site which uses ASP & VB.NET. 我在我们使用ASP和VB.NET的网站上遇到了这个问题。 Also, in-house utility programs are written in C#, VB6 & VB.NET. 而且,内部实用程序是用C#,VB6和VB.NET编写的。 All of the programs needed to be able to exchange encrypted data. 所有程序都需要能够交换加密的数据。

To handle this problem I wrote a VB6 & VBScript encryption routine which I converted to .NET. 为了解决这个问题,我编写了一个VB6和VBScript加密例程,并将其转换为.NET。 It allows me to have identical data across the platforms. 它使我可以跨平台拥有相同的数据。 The encryption & hashing that I selected were RC4 and MD5. 我选择的加密和哈希是RC4和MD5。 Both of which were considerably enhanced with multiple features, such as the MD5 is a salted version and the RC4 contains a CRC check and an option for double encrypting using multiple passkeys. 两者都通过多种功能得到了显着增强,例如MD5是加盐版,RC4包含CRC检查和使用多个密钥进行双重加密的选项。

This is for the minimally sensitive data. 这是用于最小敏感数据的。 For the data that is very sensitive, I wrote a VB6 DLL that does a DES-3 encryption. 对于非常敏感的数据,我编写了一个进行DES-3加密的VB6 DLL。 This DLL is then made available to all platforms. 然后,此DLL可用于所有平台。

I put the passkeys in the registry, encrypted using hardware parameters for the password using another encryption method. 我将密码放在注册表中,并使用另一种加密方法使用密码的硬件参数对其进行加密。 (If you get them from the registry and try to put them on another system, they're no good.) (如果您从注册表中获取它们并尝试将它们放在另一个系统上,那么它们就不好了。)

Don't mean to answer my own question, but I couldn't get the code added as a comment. 并不是要回答我自己的问题,但我无法将代码添加为注释。 Anyway, I ended up implementing RC4 on the client anyway. 无论如何,我最终还是在客户端上实现了RC4。 For anyone who may be interested (wth are you doing working in classic asp?? :) ) Here's the relevant code - hope it helps! 对于任何有兴趣的人(您正在用经典asp做什么? As always, if anyone spots a problem with this snippet, please let me know! 与往常一样,如果有人发现此代码段有问题,请告诉我!

Thanks, 谢谢,

Matt 马特

Dim sbox(255)
Dim key(255)

Sub RC4Initialize(strPwd)
  dim tempSwap
  dim a
  dim b

  intLength = len(strPwd)
  For a = 0 To 255
     key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
     sbox(a) = a
  next

  b = 0
  For a = 0 To 255
     b = (b + sbox(a) + key(a)) Mod 256
     tempSwap = sbox(a)
     sbox(a) = sbox(b)
     sbox(b) = tempSwap
  Next

End Sub

Function EnDeCrypt(plaintxt)
  dim temp
  dim a
  dim i
  dim j
  dim k
  dim cipherby
  dim cipher

  i = 0
  j = 0

  RC4Initialize "somesortofpassword"

  For a = 1 To Len(plaintxt)
     i = (i + 1) Mod 256
     j = (j + sbox(i)) Mod 256
     temp = sbox(i)
     sbox(i) = sbox(j)
     sbox(j) = temp

     k = sbox((sbox(i) + sbox(j)) Mod 256)

     cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
     dim h
     h = hex(cipherby)
     if Len(h) = 1 then
        h = "0" & h
      end if
     cipher = cipher & h
  Next

  EnDeCrypt = cipher
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM