加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Ruby和Dot net之间的加密 – 解密

发布时间:2020-12-15 21:20:28 所属栏目:百科 来源:网络整理
导读:我需要tyhe dot net相当于以下代码.问题是,我在客户端使用 Ruby加密,这里是代码. 编码的字符串将传递给C#Web服务.那必须解密字符串. 如果有人可以提供与此代码等效的点网,那么它将会有所帮助. require 'rubygems'require 'ezcrypto'require 'crypt/rijndael'
我需要tyhe dot net相当于以下代码.问题是,我在客户端使用 Ruby加密,这里是代码.
编码的字符串将传递给C#Web服务.那必须解密字符串.

如果有人可以提供与此代码等效的点网,那么它将会有所帮助.

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"

解决方法

它将类似于下面显示的代码作为单元测试.第一部分进行加密 – 后半部分进行加密.

将代码粘贴到新的MSTest单元测试(创建新测试项目或添加到现有测试项目).

键和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,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,toDecrypt.Length);
          cStream2.FlushFinalBlock();
          decryptedString = 
            System.Text.Encoding.UTF8.GetString(mStream2.ToArray());
        }
      }
    }
  }

  Assert.AreEqual(toEncryptString,decryptedString);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读