C# Function to Visual Basic 2010 Error with BitShift Operator "<<" - c#

I am currently translating a C# code to Visual Basic 2010. Works good but now I dont get the correct line. everytime i got a bitshift error
Original Code looks like:
Code:
private static string Checksum(string url)
{
uint Magic = 0xE6359A60;
uint a, b;
uint c = Magic;
a = b = 0x9E3779B9;
int k = 0;
int length = url.Length;
//Algorithm
while (length >= 12)
{
a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
Mix(ref a, ref b, ref c);
k += 12;
length -= 12;
}
and my translation is:
Private Function Checksum(ByVal url) As String
Dim Magic As UInteger = &HE6359A60UI
Dim a As UInteger, b As UInteger
Dim c As UInteger =Magic
a = b = &H9E3779B9UI
Dim k As Integer = 0
Dim length As Integer = url.Length
'Algorithm
While length >= 12
a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))
b += CUInt(url(k + 4) + (url(k + 5) << 8) + (url(k + 6) << 16) + (url(k + 7) << 24))
c += CUInt(url(k + 8) + (url(k + 9) << 8) + (url(k + 10) << 16) + (url(k + 11) << 24))
Mix(a, b, c)
k += 12
length -= 12
End While
But when the debugger comes to
a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))
it throw an exception:The operator "<<" is not defined for the Typ Char.

In C# the compiler automatically converts from char to int. As the VB compiler doesn't do that, you have to do an explicit conversion:
a += CUInt(AscW((url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24))

a += CUInt(AscW(url(k + 0))) + (CUInt(AscW(url(k + 1))) << 8) + (CUInt(AscW(url(k + 2))) << 16) + (CUInt(AscW(url(k + 3))) << 24)
Assuming your characters are Unicode. If ASCII, use Asc() instead of AscW()

Related

Get string from UTF8 (Invalid URI: There is an invalid sequence in the string)

Could you please help me understand why second line in following code throws exception?
string line1 = Uri.UnescapeDataString("Disk:%2FFolder");
string line2 = Uri.UnescapeDataString("Disk:%C0%AFFolder");
Maybe there is some other function which is able to decode "Disk:%C0%AFFolder" successfully?
URLS ares 7bits ASCII. W3schools says :
URLs can only be sent over the Internet using the ASCII character-set.
%C0 and %AF are not 8bits encoded. That's why it's not decoded by Uri.UnescapeDataString.
I spent quite a lot of time trying to find answer to my question.
In the end I gave up and decided to write decoder myself.
So this is it. If you want to decode UTF-8 bytes (which might contain overlong sequences) consider using code below:
private static string GetString(byte[] bytes)
{
StringBuilder builder = new StringBuilder();
int index = 0;
while (index < bytes.Length)
{
if ((bytes[index] & 0x7F) == bytes[index])
{
Int32 code = bytes[index];
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 1;
}
else if (((bytes[index] & 0xDF) == bytes[index]) && (index + 1) < bytes.Length
&& ((bytes[index + 1] & 0xBF) == bytes[index + 1]))
{
Int32 code = ((bytes[index + 0] & 0x1F) << 06) +
((bytes[index + 1] & 0x7F) << 00);
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 2;
}
else if (((bytes[index] & 0xEF) == bytes[index]) && (index + 2) < bytes.Length
&& ((bytes[index + 1] & 0xBF) == bytes[index + 1])
&& ((bytes[index + 2] & 0xBF) == bytes[index + 2]))
{
Int32 code = ((bytes[index + 0] & 0x0F) << 12) +
((bytes[index + 1] & 0x7F) << 06) +
((bytes[index + 2] & 0x7F) << 00);
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 3;
}
else if (((bytes[index] & 0xF7) == bytes[index]) && (index + 3) < bytes.Length
&& ((bytes[index + 1] & 0xBF) == bytes[index + 1])
&& ((bytes[index + 2] & 0xBF) == bytes[index + 2])
&& ((bytes[index + 3] & 0xBF) == bytes[index + 3]))
{
Int32 code = ((bytes[index + 0] & 0x07) << 18) +
((bytes[index + 1] & 0x7F) << 12) +
((bytes[index + 2] & 0x7F) << 06) +
((bytes[index + 3] & 0x7F) << 00);
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 4;
}
else if (((bytes[index] & 0xFB) == bytes[index]) && (index + 4) < bytes.Length
&& ((bytes[index + 1] & 0xBF) == bytes[index + 1])
&& ((bytes[index + 2] & 0xBF) == bytes[index + 2])
&& ((bytes[index + 3] & 0xBF) == bytes[index + 3])
&& ((bytes[index + 4] & 0xBF) == bytes[index + 4]))
{
Int32 code = ((bytes[index + 0] & 0x03) << 24) +
((bytes[index + 1] & 0x7F) << 18) +
((bytes[index + 2] & 0x7F) << 12) +
((bytes[index + 3] & 0x7F) << 06) +
((bytes[index + 4] & 0x7F) << 00);
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 5;
}
else if (((bytes[index] & 0xFD) == bytes[index]) && (index + 5) < bytes.Length
&& ((bytes[index + 1] & 0xBF) == bytes[index + 1])
&& ((bytes[index + 2] & 0xBF) == bytes[index + 2])
&& ((bytes[index + 3] & 0xBF) == bytes[index + 3])
&& ((bytes[index + 4] & 0xBF) == bytes[index + 4])
&& ((bytes[index + 5] & 0xBF) == bytes[index + 5]))
{
Int32 code = ((bytes[index + 0] & 0x01) << 30) +
((bytes[index + 1] & 0x7F) << 24) +
((bytes[index + 2] & 0x7F) << 18) +
((bytes[index + 3] & 0x7F) << 12) +
((bytes[index + 4] & 0x7F) << 06) +
((bytes[index + 5] & 0x7F) << 00);
byte[] codeBytes = BitConverter.GetBytes(code);
builder.Append(UnicodeEncoding.UTF32.GetString(codeBytes));
index += 6;
}
else
throw new Exception("Wrong UTF-8 format");
}
return builder.ToString();
}
public static void Main()
{
string source = "Disk:%FC%80%80%80%80%AFFolder";
byte[] bytes = HttpUtility.UrlDecodeToBytes(source);
string result = GetString(bytes);
}

Pack and unpack multiple integers into and from an Uint64

Need to pack and unpack the following into an UInt64
UInt25
UInt5
UInt7
UInt27
Have the following for packing and unpacking UInt27 and UInt5 to from UInt32
But I cannot get past 2
My background is math (not computer science)
UInt32 highlow;
UInt32 high;
byte low;
int two27 = (Int32)Math.Pow(2, 27);
for (UInt32 i = 0; i < two27; i++)
{
highlow = ((UInt32)i) << 5;
high = highlow >> 5;
if (high != i)
{
Debug.WriteLine("high wrong A " + high.ToString() + " " + i.ToString());
}
for (byte j = 0; j < 32; j++)
{
highlow = (((UInt32)i) << 5) | j;
high = highlow >> 5;
if (high != i)
{
Debug.WriteLine("high wrong B " + high.ToString() + " " + i.ToString());
}
low = (byte)(highlow & 0x1f);
if (low != j)
{
Debug.WriteLine("low wrong " + low.ToString() + " " + j.ToString());
}
}
}
Code based on accepted answer (did not test the full loop the i27 loop got to 2)
UInt32 bits27;
UInt32 bits25;
UInt32 bits7;
UInt32 bits5;
UInt32 int27 = (UInt32)Math.Pow(2,27);
UInt32 int25 = (UInt32)Math.Pow(2,25);
UInt32 int7 = (UInt32)Math.Pow(2,7);
UInt32 int5 = (UInt32)Math.Pow(2,5);
UInt64 packed;
//ulong packed = (bits27) | ((ulong)bits25 << 27) | ((ulong)bits7 << 52) | ((ulong)bits5 << 59);
for (UInt32 i27 = 0; i27 < int27; i27++)
{
for (UInt32 i25 = 0; i25 < int25; i25++)
{
for (UInt32 i7 = 0; i7 < int7; i7++)
{
for (UInt32 i5 = 0; i5 < int5; i5++)
{
packed = (UInt64)(i27) | ((UInt64)i25 << 27) | ((UInt64)i7 << 52) | ((UInt64)i5 << 59);
bits27 = (UInt32)(packed & ((1 << 27) - 1));
bits25 = (UInt32)((packed >> 27) & ((1 << 25) - 1));
bits7 = (UInt32)((packed >> 52) & ((1 << 7) - 1));
bits5 = (UInt32)((packed >> 59) & ((1 << 5) - 1));
if (bits27 != i27) Debug.WriteLine("bits27 != i27");
if (bits25 != i25) Debug.WriteLine("bits25 != i25");
if (bits7 != i7) Debug.WriteLine("bits7 != i7");
if (bits5 != i5) Debug.WriteLine("bits5 != i5");
}
}
}
}
The shift operators are the right solution, but note that they won't automatically make the result wider than the inputs -- you need to cast the input.
Pack:
ulong packed = (bits27) | ((ulong)bits25 << 27) | ((ulong)bits7 << 52) | ((ulong)bits5 << 59);
Unpack:
bits27 = (uint) (packed & ((1 << 27) - 1));
bits25 = (uint)((packed >> 27) & ((1 << 25) - 1));
bits7 = (uint)((packed >> 52) & ((1 << 7) - 1));
bits5 = (uint)((packed >> 59) & ((1 << 5) - 1));
It seems like it would be far easier to convert the numbers to binary, pad or truncate to the correct length, concatenate them and then construct your 64-bit type from binary.
var packedInt64 = Convert.ToInt64(Convert.ToString(ui25, 2).PadLeft(25, '0') +
Convert.ToString(ui5, 2).PadLeft(5, '0') +
Convert.ToString(ui7, 2).PadLeft(7, '0') +
Convert.ToString(ui27, 2).PadLeft(2, '0'), 2);
To unpack:
var binary = Convert.ToString(packedInt64, 2);
ui25 = Convert.ToUInt32(binary.Substring(0, 24));
ui5 = Convert.ToUInt32(binary.Substring(24, 5));
etc.

Faster way to swap endianness in C# with 32 bit words

In this question, the following code:
public static void Swap(byte[] data)
{
for (int i = 0; i < data.Length; i += 2)
{
byte b = data[i];
data[i] = data[i + 1];
data[i + 1] = b;
}
}
was rewritten in unsafe code to improve its performance:
public static unsafe void SwapX2(Byte[] Source)
{
fixed (Byte* pSource = &Source[0])
{
Byte* bp = pSource;
Byte* bp_stop = bp + Source.Length;
while (bp < bp_stop)
{
*(UInt16*)bp = (UInt16)(*bp << 8 | *(bp + 1));
bp += 2;
}
}
}
Assuming that one wanted to do the same thing with 32 bit words:
public static void SwapX4(byte[] data)
{
byte temp;
for (int i = 0; i < data.Length; i += 4)
{
temp = data[i];
data[i] = data[i + 3];
data[i + 3] = temp;
temp = data[i + 1];
data[i + 1] = data[i + 2];
data[i + 2] = temp;
}
}
how would this be rewritten in a similar fashion?
public static unsafe void SwapX4(Byte[] Source)
{
fixed (Byte* pSource = &Source[0])
{
Byte* bp = pSource;
Byte* bp_stop = bp + Source.Length;
while (bp < bp_stop)
{
*(UInt32*)bp = (UInt32)(
(*bp << 24) |
(*(bp + 1) << 16) |
(*(bp + 2) << 8) |
(*(bp + 3) ));
bp += 4;
}
}
}
Note that both of these functions (my SwapX4 and your SwapX2) will only swap anything on a little-endian host; when run on a big-endian host, they are an expensive no-op.
This version will not exceed the bounds of the buffer. Works on both Little and Big Endian architectures. And is faster on larger data. (Update: Add build configurations for x86 and x64, predefine X86 for 32 bit(x86) and X64 for 64 bit(x64) and it'll be slightly faster.)
public static unsafe void Swap4(byte[] source)
{
fixed (byte* psource = source)
{
#if X86
var length = *((uint*)(psource - 4)) & 0xFFFFFFFEU;
#elif X64
var length = *((uint*)(psource - 8)) & 0xFFFFFFFEU;
#else
var length = (source.Length & 0xFFFFFFFE);
#endif
while (length > 7)
{
length -= 8;
ulong* pulong = (ulong*)(psource + length);
*pulong = ( ((*pulong >> 24) & 0x000000FF000000FFUL)
| ((*pulong >> 8) & 0x0000FF000000FF00UL)
| ((*pulong << 8) & 0x00FF000000FF0000UL)
| ((*pulong << 24) & 0xFF000000FF000000UL));
}
if(length != 0)
{
uint* puint = (uint*)psource;
*puint = ( ((*puint >> 24))
| ((*puint >> 8) & 0x0000FF00U)
| ((*puint << 8) & 0x00FF0000U)
| ((*puint << 24)));
}
}
}

ushort array to byte array

I have an array of ushorts, with each ushort representing a 12-bit word. This needs to be tightly packed into an array of bytes. It should look like this in the end:
| word1 | word2 | word3 | word4 |
| byte1 | byte2 | byte3 | byte4 | byte5 | byte6|
Since each word only uses 12 bits, 2 words will be packed into 3 bytes.
Could someone help? I'm a bit stuck on how to do this in C#.
You're probably going to have to brute-force it.
I'm not a C# guy, but you are looking at something along the lines of (in C):
unsigned incursor, outcursor;
unsigned inlen = length(inputarray); // not literally
for(incursor=0,outcursor=0;incursor < inlen; incursor+=2,outcursor+=3{
outputarray[outcursor+0] = ((inputarray[incursor+0]) >> 4) & 0xFF;
outputarray[outcursor+1] = ((inputarray[incursor+0] & 0x0F)<<4 | ((inputarray[incursor+1]>>8) & 0x0F);
outputarray[outcursor+2] = inputarray[incursor+1] & 0xFF;
}
If you want to use the array as an array of UInt16 while in-memory, and then convert it to a packed byte array for storage, then you'll want a function to do one-shot conversion of the two array types.
public byte[] PackUInt12(ushort[] input)
{
byte[] result = new byte[(input.Length * 3 + 1) / 2]; // the +1 leaves space if we have an odd number of UInt12s. It's the unused half byte at the end of the array.
for(int i = 0; i < input.Length / 2; i++)
{
result[i * 3 + 0] = (byte)input[i * 2 + 0];
result[i * 3 + 1] = (byte)(input[i * 2 + 0] >> 8 | input[i * 2 + 1] << 4);
result[i * 3 + 2] = (byte)(input[i * 2 + 1] >> 4);
}
if(input.Length % 2 == 1)
{
result[i * 3 + 0] = (byte)input[i * 2 + 0];
result[i * 3 + 1] = (byte)(input[i * 2 + 0] >> 8);
}
return result;
}
public ushort[] UnpackUInt12(byte[] input)
{
ushort[] result = new ushort[input.Length * 2 / 3];
for(int i = 0; i < input.Length / 3; i++)
{
result[i * 2 + 0] = (ushort)(((ushort)input[i * 3 + 1]) << 8 & 0x0F00 | input[i * 3 + 0]);
result[i * 2 + 1] = (ushort)(((ushort)input[i * 3 + 1]) << 4 | input[i * 3 + 1] >> 4;)
}
if(result.Length % 2 == 1)
{
result[i * 2 + 0] = (ushort)(((ushort)input[i * 3 + 1]) << 8 & 0x0F00 | input[i * 3 + 0]);
}
return result;
}
If, however, you want to be efficient about memory usage while the application is running, and access this packed array as an array, then you'll want to have a class that returns ushorts, but stores them in byte[].
public class UInt12Array
{
// TODO: Constructors, etc.
private byte[] storage;
public ushort this[int index]
{
get
{
// TODO: throw exceptions if the index is off the array.
int i = index * 2 / 3;
if(index % 2 == 0)
return (ushort)(((ushort)storage[i * 3 + 1]) << 8 & 0x0F00 | storage[i * 3 + 0]);
else
return (ushort)(((ushort)storage[i * 3 + 1]) << 4 | storage[i * 3 + 1] >> 4;)
}
set
{
// TODO: throw exceptions if the index is off the array.
int i = index * 2 / 3;
if(index % 2 == 0)
storage[i * 3 + 0] = (byte)value;
storage[i * 3 + 1] = (byte)(value >> 8 | storage[i * 3 + 1] & 0xF0);
else
storage[i * 3 + 1] = (byte)(storage[i * 3 + 1] & 0x0F | value << 4);
storage[i * 3 + 2] = (byte)(value >> 4);
}
}
}
Why not store the 12-bit words in a byte array and provide a getter and a setter method that read and write the ushort's byte to the correct index in the array?
Trying to solve this with LINQ was fun!
Warning: For entertainment purposes only - do not use the below performance abominations in real code!
First try - group pairs of uints, create three bytes out of each pair, flatten list:
byte[] packedNumbers = (from i in Enumerable.Range(0, unpackedNumbers.Length)
group unpackedNumbers[i] by i - (i % 2) into pairs
let n1 = pairs.First()
let n2 = pairs.Skip(1).First()
let b1 = (byte)(n1 >> 4)
let b2 = (byte)(((n1 & 0xF) << 4) | (n2 & 0xF00) >> 8)
let b3 = (byte)(n2 & 0xFFFF)
select new[] { b1, b2, b3 })
.SelectMany(b => b).ToArray();
Or slightly more compact, but less readable:
byte[] packedNumbers = unpackedNumbers
.Select((Value, Index) => new { Value, Index })
.GroupBy(number => number.Index - (number.Index % 2))
.SelectMany(pair => new byte[] {
(byte)(pair.First().Value >> 4),
(byte)(((pair.First().Value & 0xF) << 4) | (pair.Skip(1).First().Value & 0xF00) >> 8),
(byte)(pair.Skip(1).First().Value & 0xFFFF) }).ToArray();
Strings anyone?
char[] hexChars = unpackedNumbers.SelectMany(n => n.ToString("X4").Substring(1, 3)).ToArray();
byte[] packedNumbers = (from i in Enumerable.Range(0, hexChars.Length / 2)
select byte.Parse(hexChars[i * 2].ToString() + hexChars[i * 2 + 1], NumberStyles.HexNumber))
.ToArray();
According to the comments given, I suppose, the current answers is preferable.
But about this should do it also:
public byte[] ushort2byteArr(ushort[] arr) {
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
for (int i = 0; i < arr.Length-1;) { // check upper limit!
// following is wrong! must extend this to pack 8 12 bit words into 3 uint32!
UInt32 tmp = arr[i++] | (arr[i++] << 12) ... ;
bw.Write(tmp);
}
return ms.ToArray();
}
its not tested. take it as pseudocode to get the clue. especially the word -> uint32 conversion. May need some padding at the end?
#edit: made a function out of it for better clearance

C#: Blowfish doesnt work with fewer letters

I'm using Blowfish.NET newest version,but there is one problem.
responce = new byte[6]
{
0x00, 0x80 ,0x01, 0x61, 0x00, 0x00
};
byte[] encrypted = new byte[responce.Length];
blowfish.Encrypt(responce, 2, encrypted, 2, input.Length - 2);
I called it the right way,I want it to start read/write from the third byte and the length is 6 - 2,because i dont use two bytes.
The problem:
public int Encrypt(
byte[] dataIn,
int posIn,
byte[] dataOut,
int posOut,
int count)
{
uint[] sbox1 = this.sbox1;
uint[] sbox2 = this.sbox2;
uint[] sbox3 = this.sbox3;
uint[] sbox4 = this.sbox4;
uint[] pbox = this.pbox;
uint pbox00 = pbox[ 0];
uint pbox01 = pbox[ 1];
uint pbox02 = pbox[ 2];
uint pbox03 = pbox[ 3];
uint pbox04 = pbox[ 4];
uint pbox05 = pbox[ 5];
uint pbox06 = pbox[ 6];
uint pbox07 = pbox[ 7];
uint pbox08 = pbox[ 8];
uint pbox09 = pbox[ 9];
uint pbox10 = pbox[10];
uint pbox11 = pbox[11];
uint pbox12 = pbox[12];
uint pbox13 = pbox[13];
uint pbox14 = pbox[14];
uint pbox15 = pbox[15];
uint pbox16 = pbox[16];
uint pbox17 = pbox[17]; // till this line count is 4
count &= ~(BLOCK_SIZE - 1); //count becomes 0 after that calc :((
int end = posIn + count; // 2 + 0 = 2
while (posIn < end) //no loop :[
{
uint hi = (((uint)dataIn[posIn + 3]) << 24) |
(((uint)dataIn[posIn + 2]) << 16) |
(((uint)dataIn[posIn + 1]) << 8) |
dataIn[posIn ];
uint lo = (((uint)dataIn[posIn + 7]) << 24) |
(((uint)dataIn[posIn + 6]) << 16) |
(((uint)dataIn[posIn + 5]) << 8) |
dataIn[posIn + 4];
posIn += 8;
hi ^= pbox00;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox01;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox02;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox03;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox04;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox05;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox06;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox07;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox08;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox09;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox10;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox11;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox12;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox13;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox14;
lo ^= (((sbox1[(int)(hi >> 24)] + sbox2[(int)((hi >> 16) & 0x0ff)]) ^ sbox3[(int)((hi >> 8) & 0x0ff)]) + sbox4[(int)(hi & 0x0ff)]) ^ pbox15;
hi ^= (((sbox1[(int)(lo >> 24)] + sbox2[(int)((lo >> 16) & 0x0ff)]) ^ sbox3[(int)((lo >> 8) & 0x0ff)]) + sbox4[(int)(lo & 0x0ff)]) ^ pbox16;
lo ^= pbox17;
dataOut[posOut + 3] = (byte)(lo >> 24);
dataOut[posOut + 2] = (byte)(lo >> 16);
dataOut[posOut + 1] = (byte)(lo >> 8);
dataOut[posOut ] = (byte) lo;
dataOut[posOut + 7] = (byte)(hi >> 24);
dataOut[posOut + 6] = (byte)(hi >> 16);
dataOut[posOut + 5] = (byte)(hi >> 8);
dataOut[posOut + 4] = (byte) hi;
posOut += 8;
}
return count;
}
The blowfish works fine if i pass longer data,but I need to encrypt this one.We've found the problem,but my question is:How to fix it?!
I'm not super familiar with the Blowfish algorithm or, for that matter, the library you're using. But, in general, I'd say that if you're having issues with your data not being long enough to encrypt, you could pad it. In other words... If you data isn't long enough, add a bunch of zeros (or some other character if zeros hold special meaning to you) to the beginning or end of the data before you encrypt it. Then, when you decrypt it, trim the padding characters.
You're trying to use a block cipher on something which is not exactly one block in size. If your data is not going to be exactly 8 bytes long, you should be using an existing padding algorithm along with a well-defined mode. That said, why are you implementing your own blowfish function instead of using a pre-made library?
There are standard ways to pad data so that it has the right length for a given block encryption algorithm.
This page contains a good overview of the available options.
We need to pad the block with padding
bytes to make it up to the required
length. There are at least five common
conventions:
Pad with bytes all of the same value as the number of padding bytes
Pad with 0x80 followed by zero bytes
Pad with zeroes except make the last byte equal to the number of
padding bytes
Pad with zero (null) characters
Pad with space characters
Method one is the method described in PKCS#5, PKCS#7 and RFC 3852 Section 6.3 (formerly RFC 3369 and RFC 2630). It is the most commonly used, and the one we recommend in the absence of any other considerations.
But since you seem to be always encrypting a short fixed-length message, padding could effectively weaken the encryption. You could pad it with random numbers and throw them away after decryption.

Categories