C# TripleDES NoPadding 時對待加密內容進行補位元組(8個位元組為一個Block)

FrankYou發表於2017-03-22

補一個空格(半形):

private static byte[] FormatData(String str)
{
    var yu = str.Length % 8;
    if (yu == 0) return Encoding.GetEncoding(Consts.Charset).GetBytes(str);
    var size = 8 - yu;
    var arr = new byte[str.Length + size];
    var data = Encoding.GetEncoding(Consts.Charset).GetBytes(str);
    var i = 0;
    for (; i < data.Length; i++)
    {
        arr[i] = data[i];
    }
    for (var j = 0; j < size; j++,i++)
    {
        arr[i] = Convert.ToByte(' ');
    }
    return arr;
}

private static byte[] FormatByte(IList<byte> arr)
{
    var i = 0;
    for (; i < arr.Count; i++)
    {
        if (Convert.ToChar(arr[i]) == ' ')
        {
            break;
        }
    }
    var result = new byte[i];
    for (var j = 0; j < i; j++)
    {
        result[j] = arr[j];
    }
    return result;
}

 

相關文章