将整数转换为加密字符串,反之亦然

时间:2014-02-01 09:30:08

标签: c# asp.net

我想在URL中传递pid作为查询字符串参数,但不是像?pid = 102或?pid = 493这样的int,我想以加密形式传递这些pid,如?pid = D109150A13F0EA4或其他加密字符串。我尝试了内置的加密方法,但它们给出了长长度的字符串 ?PID = F3D165BAF8D84FB17CF8E5B4A04AC9022BFF5F987A6EDC42D109150A13F0EA4D847527287C8013154E2E8A2D8DAB6B686751C079092713C0DDA3E2E932D5892361E1B486FE2F46C2E288EA54F64B8B4C

我想要小的字母数字字符串,如?pid = D109150A13F0EA4或类似的

3 个答案:

答案 0 :(得分:1)

您是否尝试过使用Guid

var g = Guid.NewGuid(productId.ToString());

它将产生38个字符的结果:8个十六进制数字,后跟三组每组4个十六进制数字,后跟一组12个十六进制数字。

Guid示例:6B29FC40-CA47-1067-B31D-00DD010662DA

因此,与您的示例相比,它实际上相当短。 Guid的唯一缺点是你不能将它解密回int(但你可以比较两个Guids是否代表相同的int值)。

如果您需要加密和解密,除了内置加密(我假设您在上面的示例中使用了Encrypt方法),System.Security.Cryptography命名空间中还有许多其他加密算法可用,像:

  • DES,例如:2120357ccd3e0142
  • Aes,例如:73054ef012f6ea6d47757a37a84381f7
  • HMACSHA256,例如:6723ace2ec7b0348e1270ccbaab802bfa5c1bbdddd108aece88c739051a8a767

答案 1 :(得分:0)

对于那些我使用小型EncryptDecrypt类的东西,也许这对你的方法有帮助。

Simle用法,例如EncryptDecrypt.ToEncrypt(yourstring)EncryptDecrypt.ToDecrypt(yourEncryptedString)。因此,您应该能够在添加到查询字符串之前进行加密。整数和数字等,你必须先转换为字符串。

希望这有帮助。

using System;
using System.Security.Cryptography;
using System.IO;

namespace YourNameSpace

{
    public class EncryptDecrypt
    {
        #region Declaration

        static readonly byte[] TripleDesKey1 = new byte[] { 15, 11, 7, 21, 34, 32, 33, 5, 23, 13, 23, 41, 43, 41, 7, 19, 91, 91, 47, 7, 37, 13, 19, 41 };
        static readonly byte[] TripleDesiv1 = new byte[] { 5, 23, 13, 23, 41, 43, 41, 7 };

        #endregion


        /// <summary>
        /// To Encrypt String
        /// </summary>
        /// <param name="value">String To Encrypt</param>
        /// <returns>Returns Encrypted String</returns>
        public static string ToEncrypt(string value)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider
                                                     {
                                                         Key = TripleDesKey1,
                                                         IV = TripleDesiv1
                                                     };

            MemoryStream ms;

            if (value.Length >= 1)
                ms = new MemoryStream(((value.Length * 2) - 1));
            else
                ms = new MemoryStream();

            ms.Position = 0;
            CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(value);
            encStream.Write(plainBytes, 0, plainBytes.Length);
            encStream.FlushFinalBlock();
            encStream.Close();

            return Convert.ToBase64String(plainBytes);
        }

        /// <summary>
        /// To Decrypt Data Encrypted From TripleDEC Algoritham
        /// </summary>
        /// <param name="value">String Value To Decrypt</param>
        /// <returns>Return Decrypted Data</returns>
        public static string ToDecrypt(string value)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            //System.IO.MemoryStream ms = new System.IO.MemoryStream(((value.Length * 2) - 1));
            MemoryStream ms;
            if (value.Length >= 1)
                ms = new MemoryStream(((value.Length * 2) - 1));
            else
                ms = new MemoryStream();

            ms.Position = 0;
            CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(TripleDesKey1, TripleDesiv1), CryptoStreamMode.Write);
            byte[] plainBytes = Convert.FromBase64String(value);
            encStream.Write(plainBytes, 0, plainBytes.Length);
            return System.Text.Encoding.UTF8.GetString(plainBytes);
        }

    }
}

答案 2 :(得分:0)

此方法用于生成随机字符串

private string GetRandomString(int iStringLength)
    {
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
        char[] chars = new char[iStringLength];           
            Random rd = new Random();    
            for (int i = 0; i < iStringLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }       
            return new string(chars);           
    }

或使用

var gid = Guid.NewGuid(productId.ToString());

下面的方法加密字符串。只需传递一个普通/随机字符串来加密字符串。

 protected string EncryptString(String strString)
    {            
            UnicodeEncoding uEncode = new UnicodeEncoding();
            Byte[] bytstrString = uEncode.GetBytes(strString);
            SHA256Managed sha1 = new SHA256Managed();
            Byte[] hash = sha1.ComputeHash(bytstrString);

            return Convert.ToBase64String(hash);

    }