AES返回字母数字 [英] AES to return Alphanumeric

查看:101
本文介绍了AES返回字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个aes加密代码,我想让它只返回字符数字字符,如 {0123456789ABCDEFGHIJKLMNOPQRSTWUVYZ}



  public class clsCrypto 
{
private string _KEY = string.Empty;
protected internal string KEY
{
get
{
return _KEY;
}
set
{
if(!string.IsNullOrEmpty(value))
{
_KEY = value;
}
}
}

私人字符串_IV = string.Empty;
protected internal string IV
{
get
{
return _IV;
}
set
{
if(!string.IsNullOrEmpty(value))
{
_IV = value;
}
}
}

私有字符串CalcMD5(string strInput)
{
string strOutput = string.Empty;
if(!string.IsNullOrEmpty(strInput))
{
try
{
StringBuilder strHex = new StringBuilder();
using(MD5 md5 = MD5.Create())
{
byte [] bytArText = Encoding.Default.GetBytes(strInput);
byte [] bytArHash = md5.ComputeHash(bytArText); (int i = 0; i< bytArHash.Length; i ++)
{
strHex.Append(bytArHash [i] .ToString(X2));

}
strOutput = strHex.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}

private byte [] GetBytesFromHexString(string strInput)
{
byte [] bytArOutput = new byte [] {};
if((!string.IsNullOrEmpty(strInput))&& strInput.Length%2 == 0)
{
SoapHexBinary hexBinary = null;
try
{
hexBinary = SoapHexBinary.Parse(strInput);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
bytArOutput = hexBinary.Value;
}
return bytArOutput;
}

private byte [] GenerateIV()
{
byte [] bytArOutput = new byte [] {};
try
{
string strIV = CalcMD5(IV);
bytArOutput = GetBytesFromHexString(strIV);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return bytArOutput;
}

private byte [] GenerateKey()
{
byte [] bytArOutput = new byte [] {};
try
{
string strKey = CalcMD5(KEY);
bytArOutput = GetBytesFromHexString(strKey);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return bytArOutput;
}

protected内部字符串加密(string strInput,CipherMode cipherMode)
{
string strOutput = string.Empty;
if(!string.IsNullOrEmpty(strInput))
{
try
{
byte [] bytePlainText = Encoding.Default.GetBytes(strInput);
using(RijndaelManaged rijManaged = new RijndaelManaged())
{
rijManaged.Mode = cipherMode;
rijManaged.BlockSize = 128;
rijManaged.KeySize = 128;
rijManaged.IV = GenerateIV();
rijManaged.Key = GenerateKey();
rijManaged.Padding = PaddingMode.Zeros;
ICryptoTransform icpoTransform = rijManaged.CreateEncryptor(rijManaged.Key,rijManaged.IV);
使用(MemoryStream memStream = new MemoryStream())
{
using(CryptoStream cpoStream = new CryptoStream(memStream,icpoTransform,CryptoStreamMode.Write))
{
cpoStream .Write(bytePlainText,0,bytePlainText.Length);
cpoStream.FlushFinalBlock();
}
strOutput = Encoding.Default.GetString(memStream.ToArray());
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}

protected internal string Decrypt(string strInput,CipherMode cipherMode)
{
string strOutput = string.Empty;
if(!string.IsNullOrEmpty(strInput))
{
try
{
byte [] byteCipherText = Encoding.Default.GetBytes(strInput);
byte [] byteBuffer = new byte [strInput.Length];
using(RijndaelManaged rijManaged = new RijndaelManaged())
{
rijManaged.Mode = cipherMode;
rijManaged.BlockSize = 128;
rijManaged.KeySize = 128;
rijManaged.IV = GenerateIV();
rijManaged.Key = GenerateKey();
rijManaged.Padding = PaddingMode.Zeros;
ICryptoTransform icpoTransform = rijManaged.CreateDecryptor(rijManaged.Key,rijManaged.IV);
使用(MemoryStream memStream = new MemoryStream(byteCipherText))
{
using(CryptoStream cpoStream = new CryptoStream(memStream,icpoTransform,CryptoStreamMode.Read))
{
cpoStream.Read(byteBuffer,0,byteBuffer.Length);
}
strOutput = Encoding.Default.GetString(byteBuffer);
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}

}


解决方案

加密和解密功能使用字节数组作为参数。
因此,您必须将这些数组转换为36字符串。



您可以使用以下类(Base36)进行这些转换:



所有你需要做的是调用这两个函数:

  byte [] byteArray ; 

//将字节数组转换为字符串
string byteArrayInBase36 = Base36.ByteArrayToBase36String(byteArray);

//将String转换为字节数组
byte [] byteArray2 = Base36.Base36StringToByteArray(byteArrayInBase36);

,这是类:

 使用系统; 
使用System.Collections.Generic;

class Base36
{
#region public methods
public static string ByteArrayToBase36String(byte [] bytes)
{
string result = string .Empty;
result = Encode36((ulong)bytes.Length).PadLeft(BASE36_LENGTH_BLOC_SIZE_36,'0');

if(bytes.Length> 0)
{
列表< byte []> byteslist = SplitBytes(bytes,8);
if(byteslist [byteslist.Count - 1] .Length< 8)
{
byte [] newLastArray = new byte [8];
byteslist [byteslist.Count - 1] .CopyTo(newLastArray,0);
byteslist [byteslist.Count - 1] = newLastArray;
}
foreach(byte [] byteArray in byteslist)
{
ulong value = 0;

// for(int i = 0; i< byteArray.Length; i ++)value = value * 256 + byteArray [i];
value = BitConverter.ToUInt64(byteArray,0);
result = result + Encode36(value).PadLeft(BASE36_BLOC_SIZE_36,'0');
}
}
返回结果;
}
public static byte [] Base36StringToByteArray(string input)
{
byte [] result = new byte [0];
if(input.Length> = BASE36_LENGTH_BLOC_SIZE_36)
{
int arrayLength =(int)Decode36(input.Substring(0,BASE36_LENGTH_BLOC_SIZE_36));
string data = input.Remove(0,BASE36_LENGTH_BLOC_SIZE_36);
列表< byte []> bytesList = new List< byte []>();
foreach(新的List< string>中的string value36(SplitStringByLength(data,BASE36_BLOC_SIZE_36)))
{
byte [] byteArray = BitConverter.GetBytes(Decode36(value36));
bytesList.Add(byteArray);
}
result = JoinBytes(bytesList);
Array.Resize(ref result,arrayLength);
}
返回结果;
}
#endregion

#region Const
private const int BASE36_LENGTH_BLOC_SIZE_36 = 6;
private const int BASE36_BLOC_SIZE_36 = 13; //Encode36(ulong.MaxValue).Length;
#endregion

#region私有方法
static string _CharList36 = string.Empty;
static private string CharList36
{
get
{
if(_CharList36.Length< 36)
{
char [] array =新炭[36]; (int i = 0; i< 10; i ++)array [i] =(char)(i + 48);
(int i = 0; i <26; i ++)array [i + 10] =(char)(i + 97);

_CharList36 = new string(array);
}
return _CharList36;
}
}

private static List< string> SplitStringByLength(string str,int chunkSize)
{
列表< string> list = new List< string>();
int i;
for(i = 0; i< str.Length / chunkSize; i ++)
{
list.Add(str.Substring(i * chunkSize,chunkSize));
}
i = i * chunkSize;
if(i< str.Length - 1)
list.Add(str.Substring(i,str.Length - i));
返回列表;
}

private static String Encode36(ulong input)
{
if(input< 0)throw new ArgumentOutOfRangeException(input,input,input can not为负);

char [] clistarr = CharList36.ToCharArray();
var result = new Stack< char>();
while(input!= 0)
{
result.Push(clistarr [input%36]);
输入/ = 36;
}
返回新的字符串(result.ToArray())ToUpper();
}

private static ulong Decode36(string input)
{
var revers = ReverseString(input.ToLower());
ulong result = 0;
int pos = 0;
foreach(char c in reverse)
{
result + =(ulong)CharList36.IndexOf(c)*(ulong)Math.Pow(36,pos);
pos ++;
}
返回结果;
}

private static string ReverseString(string text)
{
char [] cArray = text.ToCharArray();
string reverse = String.Empty; (int i = 0; i< cArray.Length / 2; i ++)
{
char c = cArray [i];
cArray [i] = cArray [cArray.Length - 1 - i];
cArray [cArray.Length - 1 - i] = c;
}
返回新字符串(cArray);
}

private static byte [] StringToBytes(string str)
{
byte [] bytes = new byte [str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(),0,bytes,0,bytes.Length);
返回字节;
}

private static List< byte []> SplitBytes(byte [] bytes,int length)
{
列表< byte []> result = new List< byte []>();

int position = 0;
while(bytes.Length - position> length)
{
byte [] temp = new byte [length]; (int i = 0; i< temp.Length; i ++)
temp [i] = bytes [i + position];
position + = length;
result.Add(temp);
}
if(position< bytes.Length)
{
byte [] temp = new byte [bytes.Length - position]; (int i = 0; i + position< bytes.Length; i ++)temp [i] = bytes [i + position];

result.Add(temp);
}
返回结果;
}

private static string BytesToString(byte [] bytes)
{
char [] chars = new char [bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes,0,chars,0,bytes.Length);
返回新的字符串(chars);
}

private static byte [] JoinBytes(List< byte []> listBytes)
{
int totalLength = 0;
foreach(listBytes中的byte []字节)totalLength + = bytes.Length;
byte [] result = new byte [totalLength];
int position = 0;
foreach(listBytes中的byte []字节)
(int i = 0; i< bytes.Length; i ++)
{
result [position] = bytes [i ]。
position ++;
}
返回结果;
}

#endregion
}


I have an aes encryption code, i want to make it only return alphanumerical characters like {0123456789ABCDEFGHIJKLMNOPQRSTWUVYZ}

But however i could not figure out how to do that. I have almost no idea about encryption, could not figure out where to fix. I would really apreciate your feedback. Regards...

public class clsCrypto
    {
        private string _KEY = string.Empty;
        protected internal string KEY
        {
            get
            {
                return _KEY;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _KEY = value;
                }
            }
        }

        private string _IV = string.Empty;
        protected internal string IV
        {
            get
            {
                return _IV;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _IV = value;
                }
            }
        }

        private string CalcMD5(string strInput)
        {
            string strOutput = string.Empty;
            if (!string.IsNullOrEmpty(strInput))
            {
                try
                {
                    StringBuilder strHex = new StringBuilder();
                    using (MD5 md5 = MD5.Create())
                    {
                        byte[] bytArText = Encoding.Default.GetBytes(strInput);
                        byte[] bytArHash = md5.ComputeHash(bytArText);
                        for (int i = 0; i < bytArHash.Length; i++)
                        {
                            strHex.Append(bytArHash[i].ToString("X2"));
                        }
                        strOutput = strHex.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return strOutput;
        }

        private byte[] GetBytesFromHexString(string strInput)
        {
            byte[] bytArOutput = new byte[] { };
            if ((!string.IsNullOrEmpty(strInput)) && strInput.Length % 2 == 0)
            {
                SoapHexBinary hexBinary = null;
                try
                {
                    hexBinary = SoapHexBinary.Parse(strInput);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                bytArOutput = hexBinary.Value;
            }
            return bytArOutput;
        }

        private byte[] GenerateIV()
        {
            byte[] bytArOutput = new byte[] { };
            try
            {
                string strIV = CalcMD5(IV);
                bytArOutput = GetBytesFromHexString(strIV);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return bytArOutput;
        }

        private byte[] GenerateKey()
        {
            byte[] bytArOutput = new byte[] { };
            try
            {
                string strKey = CalcMD5(KEY);
                bytArOutput = GetBytesFromHexString(strKey);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return bytArOutput;
        }

        protected internal string Encrypt(string strInput, CipherMode cipherMode)
        {
            string strOutput = string.Empty;
            if (!string.IsNullOrEmpty(strInput))
            {
                try
                {
                    byte[] bytePlainText = Encoding.Default.GetBytes(strInput);
                    using (RijndaelManaged rijManaged = new RijndaelManaged())
                    {
                        rijManaged.Mode = cipherMode;
                        rijManaged.BlockSize = 128;
                        rijManaged.KeySize = 128;
                        rijManaged.IV = GenerateIV();
                        rijManaged.Key = GenerateKey();
                        rijManaged.Padding = PaddingMode.Zeros;
                        ICryptoTransform icpoTransform = rijManaged.CreateEncryptor(rijManaged.Key, rijManaged.IV);
                        using (MemoryStream memStream = new MemoryStream())
                        {
                            using (CryptoStream cpoStream = new CryptoStream(memStream, icpoTransform, CryptoStreamMode.Write))
                            {
                                cpoStream.Write(bytePlainText, 0, bytePlainText.Length);
                                cpoStream.FlushFinalBlock();
                            }
                            strOutput = Encoding.Default.GetString(memStream.ToArray());
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return strOutput;
        }

        protected internal string Decrypt(string strInput, CipherMode cipherMode)
        {
            string strOutput = string.Empty;
            if (!string.IsNullOrEmpty(strInput))
            {
                try
                {
                    byte[] byteCipherText = Encoding.Default.GetBytes(strInput);
                    byte[] byteBuffer = new byte[strInput.Length];
                    using (RijndaelManaged rijManaged = new RijndaelManaged())
                    {
                        rijManaged.Mode = cipherMode;
                        rijManaged.BlockSize = 128;
                        rijManaged.KeySize = 128;
                        rijManaged.IV = GenerateIV();
                        rijManaged.Key = GenerateKey();
                        rijManaged.Padding = PaddingMode.Zeros;
                        ICryptoTransform icpoTransform = rijManaged.CreateDecryptor(rijManaged.Key, rijManaged.IV);
                        using (MemoryStream memStream = new MemoryStream(byteCipherText))
                        {
                            using (CryptoStream cpoStream = new CryptoStream(memStream, icpoTransform, CryptoStreamMode.Read))
                            {
                                cpoStream.Read(byteBuffer, 0, byteBuffer.Length);
                            }
                            strOutput = Encoding.Default.GetString(byteBuffer);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            return strOutput;
        }

    }

解决方案

Encryption and decryption functions use byte arrays as parameters. So, you must convert these arrays to base 36 string.

You can use the following class (Base36) to make these conversions:

All you have to do, is calling these two functions:

 byte[] byteArray;

 //To convert byte array to String 
 string byteArrayInBase36 = Base36.ByteArrayToBase36String(byteArray);

 //To convert String to byte Array
 byte[] byteArray2 = Base36.Base36StringToByteArray(byteArrayInBase36);

and, this is the class:

using System;
using System.Collections.Generic;

class Base36
{
#region public methods
public static string ByteArrayToBase36String(byte[] bytes)
{
    string result = string.Empty;
    result = Encode36((ulong)bytes.Length).PadLeft(BASE36_LENGTH_BLOC_SIZE_36, '0');

    if (bytes.Length > 0)
    {
        List<byte[]> byteslist = SplitBytes(bytes, 8);
        if (byteslist[byteslist.Count - 1].Length < 8)
        {
            byte[] newLastArray = new byte[8];
            byteslist[byteslist.Count - 1].CopyTo(newLastArray, 0);
            byteslist[byteslist.Count - 1] = newLastArray;
        }
        foreach (byte[] byteArray in byteslist)
        {
            ulong value = 0;

            //for (int i = 0; i < byteArray.Length; i++) value = value * 256 + byteArray[i];
            value = BitConverter.ToUInt64(byteArray, 0);
            result = result + Encode36(value).PadLeft(BASE36_BLOC_SIZE_36, '0');
        }
    }
    return result;
}
public static byte[] Base36StringToByteArray(string input)
{
    byte[] result = new byte[0];
    if (input.Length >= BASE36_LENGTH_BLOC_SIZE_36)
    {
        int arrayLength = (int)Decode36(input.Substring(0, BASE36_LENGTH_BLOC_SIZE_36));
        string data = input.Remove(0, BASE36_LENGTH_BLOC_SIZE_36);
        List<byte[]> bytesList = new List<byte[]>();
        foreach (string value36 in new List<string>(SplitStringByLength(data, BASE36_BLOC_SIZE_36)))
        {
            byte[] byteArray = BitConverter.GetBytes(Decode36(value36));
            bytesList.Add(byteArray);
        }
        result = JoinBytes(bytesList);
        Array.Resize(ref result, arrayLength);
    }
    return result;
}
#endregion

#region Const
private const int BASE36_LENGTH_BLOC_SIZE_36 = 6;
private const int BASE36_BLOC_SIZE_36 = 13; //Encode36(ulong.MaxValue).Length;
#endregion

#region private methods
static string _CharList36 = string.Empty;
static private string CharList36
{
    get
    {
        if (_CharList36.Length < 36)
        {
            char[] array = new char[36];
            for (int i = 0; i < 10; i++) array[i] = (char)(i + 48);
            for (int i = 0; i < 26; i++) array[i + 10] = (char)(i + 97);
            _CharList36 = new string(array);
        }
        return _CharList36;
    }
}

private static List<string> SplitStringByLength(string str, int chunkSize)
{
    List<string> list = new List<string>();
    int i;
    for (i = 0; i < str.Length / chunkSize; i++)
    {
        list.Add(str.Substring(i * chunkSize, chunkSize));
    }
    i = i * chunkSize;
    if (i < str.Length - 1)
        list.Add(str.Substring(i, str.Length - i));
    return list;
}

private static String Encode36(ulong input)
{
    if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");

    char[] clistarr = CharList36.ToCharArray();
    var result = new Stack<char>();
    while (input != 0)
    {
        result.Push(clistarr[input % 36]);
        input /= 36;
    }
    return new string(result.ToArray()).ToUpper();
}

private static ulong Decode36(string input)
{
    var reversed = ReverseString(input.ToLower());
    ulong result = 0;
    int pos = 0;
    foreach (char c in reversed)
    {
        result += (ulong)CharList36.IndexOf(c) * (ulong)Math.Pow(36, pos);
        pos++;
    }
    return result;
}

private static string ReverseString(string text)
{
    char[] cArray = text.ToCharArray();
    string reverse = String.Empty;
    for (int i = 0; i < cArray.Length / 2; i++)
    {
        char c = cArray[i];
        cArray[i] = cArray[cArray.Length - 1 - i];
        cArray[cArray.Length - 1 - i] = c;
    }
    return new string(cArray);
}

private static byte[] StringToBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

private static List<byte[]> SplitBytes(byte[] bytes, int length)
{
    List<byte[]> result = new List<byte[]>();

    int position = 0;
    while (bytes.Length - position > length)
    {
        byte[] temp = new byte[length];
        for (int i = 0; i < temp.Length; i++) temp[i] = bytes[i + position];
        position += length;
        result.Add(temp);
    }
    if (position < bytes.Length)
    {
        byte[] temp = new byte[bytes.Length - position];
        for (int i = 0; i + position < bytes.Length; i++) temp[i] = bytes[i + position];
        result.Add(temp);
    }
    return result;
}

private static string BytesToString(byte[] bytes)
{
    char[] chars = new char[bytes.Length / sizeof(char)];
    System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
    return new string(chars);
}

private static byte[] JoinBytes(List<byte[]> listBytes)
{
    int totalLength = 0;
    foreach (byte[] bytes in listBytes) totalLength += bytes.Length;
    byte[] result = new byte[totalLength];
    int position = 0;
    foreach (byte[] bytes in listBytes)
        for (int i = 0; i < bytes.Length; i++)
        {
            result[position] = bytes[i];
            position++;
        }
    return result;
}

#endregion
} 

这篇关于AES返回字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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