简体   繁体   English

Ruby与点网加解密

[英]Encryption-decryption between Ruby and Dot net

I need tyhe dot net equivalent of the following code.我需要以下代码的 tyhe dot net 等效项。 Matter is, I am encrypting using Ruby on client side, here is the code.问题是,我在客户端使用 Ruby 加密,这是代码。 The encoded string will be passed to a C# web service.编码后的字符串将传递给 C# web 服务。 That has to decrypt the string.那必须解密字符串。

If someone can provide the dot net equivalent of this code, then it will be helpful.如果有人可以提供此代码的 dot net 等效项,那将很有帮助。

require 'rubygems'
require 'ezcrypto'
require 'crypt/rijndael'

plaintext = '24.9195N 17.821E'


aes_key = Crypt::Rijndael.new('0123456789abcdef0123456789abcdef')
aes_cyphertext = aes_key.encrypt_string(plaintext)
print "\n"

print aes_cyphertext +"\n"

print Base64.encode64(aes_cyphertext)
print "\n"

print aes_key.decrypt_string(aes_cyphertext)
print "\n"

It's going to be something like this code shown below as a unit test.它将类似于下面显示的代码作为单元测试。 The first part does the encryption - the second half does the decryption.第一部分进行加密 - 后半部分进行解密。

Paste the code into a new MSTest unit test (Create New Test Project or add to an existing one).将代码粘贴到新的 MSTest 单元测试中(创建新的测试项目或添加到现有的测试项目)。

The key and the iv are what you'll need to set accordingly.密钥和 iv 是您需要相应设置的内容。

//needed to convert from hex string
public static byte[] FromHexString(string hexString)
{
  int NumberChars = hexString.Length;
  byte[] bytes = new byte[NumberChars / 2];
  for (int i = 0; i < NumberChars; i += 2)
    bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
  return bytes;
}

[TestMethod]
public void Test()
{
  string toEncryptString = "24.9195N 17.821E";
  //initialise key and IV (note - all zero IV is not recommended!)
  byte[] key = FromHexString("0123456789abcdef0123456789abcdef");
  byte[] iv = FromHexString("00000000000000000000000000000000");
  byte[] toEncrypt = System.Text.Encoding.UTF8.GetBytes(toEncryptString);

  byte[] cipherBytes = null;
  string cipherText = null;

  //encrypt
  using (System.Security.Cryptography.Rijndael r = new RijndaelManaged())
  {
    r.Key = key;
    r.IV = iv;
    using(System.Security.Cryptography.ICryptoTransform transform 
      = r.CreateEncryptor())
    {
      using (var mStream = new System.IO.MemoryStream())
      {
        using (var cStream = 
          new CryptoStream(mStream, transform, CryptoStreamMode.Write))
        {
          cStream.Write(toEncrypt, 0, toEncrypt.Length);
          cStream.FlushFinalBlock();
          cipherBytes = mStream.ToArray();
          cipherText = Convert.ToBase64String(cipherBytes);
        }
      }
    }
  }

  //decrypt
  byte[] toDecrypt = Convert.FromBase64String(cipherText);
  string decryptedString = null;
  using (System.Security.Cryptography.Rijndael r = new RijndaelManaged())
  {
    r.Key = key;
    r.IV = iv;
    using(System.Security.Cryptography.ICryptoTransform transform2
      = r.CreateDecryptor()) // <-- difference here
    {
      using (var mStream2 = new System.IO.MemoryStream())
      {
        using (var cStream2 = 
          new CryptoStream(mStream2, transform2, CryptoStreamMode.Write))
        {
          cStream2.Write(toDecrypt, 0, toDecrypt.Length);
          cStream2.FlushFinalBlock();
          decryptedString = 
            System.Text.Encoding.UTF8.GetString(mStream2.ToArray());
        }
      }
    }
  }

  Assert.AreEqual(toEncryptString, decryptedString);
}

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

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