如何在Visual Studio 2005中使用C#加密和解密字符串 [英] How to encrypt and decrypt a string using C# in Visual Studio 2005

查看:82
本文介绍了如何在Visual Studio 2005中使用C#加密和解密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在C#Visual Studio 2005中对字符串进行加密和解密。我在谷歌上做了一些搜索,并在VB.Net中获得了代码,我将它转换为C#。但它在加密时返回此字符串......指定的密钥不是此算法的有效大小。请有人帮帮我。这是Visual Studio 2005中的正确方法吗?

以下是代码...

Hi,

I wanna do encryption and decryption of a string in C# Visual Studio 2005. I did some search on google and got a code in VB.Net, and I converted it to C#. But its returning this string while encrypting... "Specified key is not a valid size for this algorithm.". Can anybody help me please. is this the right way in Visual Studio 2005 ?
Here is the code...

public static string Decrypt(string encryptedText)
      {
          byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };

          try
          {
              byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
              byte[] inputByteArray = Convert.FromBase64String(encryptedText.Replace(' ', '+'));
              MemoryStream ms = new MemoryStream();
              CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write);
              cs.Write(inputByteArray, 0, inputByteArray.Length);
              cs.FlushFinalBlock();
              Encoding encoding = Encoding.UTF8;
              return encoding.GetString(ms.ToArray());
          }
          catch (Exception e)
          {
              return e.Message;
          }
      }
      public static string Encrypt(string stringToEncrypt)
      {
          byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
          byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };
          try
          {
              byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
              DESCryptoServiceProvider des = new DESCryptoServiceProvider();
              MemoryStream ms = new MemoryStream();
              CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
              cs.Write(inputByteArray, 0, inputByteArray.Length);
              cs.FlushFinalBlock();
              return Convert.ToBase64String(ms.ToArray());
          }
          catch (Exception e)
          {
              return e.Message;
          }
      }

推荐答案

错误说明说明错误是什么。
error description says what the error is.
Quote:

指定的密钥不是此算法的有效大小。

"Specified key is not a valid size for this algorithm."





让我们检查钥匙是A0D1X0Q,长度为7.



你可以在这里找到钥匙尺寸:

http://msdn.microsoft .com / zh-cn / library / system.security.cryptography.symmetricalgorithm.legalkeysizes%28v = vs.110%29.aspx [ ^ ]



msdn说DES算法的密钥大小是64位= 8字节,这不是你的密钥大小。



let's check key which is "A0D1X0Q" which has the length of 7.

you can find key sizes here:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.legalkeysizes%28v=vs.110%29.aspx[^]

msdn says the key size for DES algorithm is 64bits=8 bytes which is not your key size.


这篇关于如何在Visual Studio 2005中使用C#加密和解密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆