Sorry If I ask a stupid question or request too much for your help. I have stuck with this problem. I need to convert java codes to PHP.
The string: ‘LITE_9788248903420’, and run blowfish algorithm on this. Will get a string in return that we run base64 encoding on. Should end up with this string ‘ZRkY0Ttgp852xLv7PVrCHMHlco-nK-Fx’ after run "EncryptString"
I tried to convert but not succeed. Is there anyone can help me for this.
public static string EncryptString(string value){
return EncodeBfString(BfEncrypt(value));
}
private static string EncodeBfString(byte[] value)
{
StringBuilder result = new StringBuilder(value.Length * 3);
byte last = 0;
int inputLength = value.Length;
if (inputLength % 3 != 0) inputLength++;
for (int i = 0; i < inputLength; i++)
{
// in the original Pascal code, the input string is appended with an extra #0 if Length mod 3 <> 0
byte thisByte = i < value.Length ? value[i] : (byte)0;
switch (i % 3)
{
case 0:
result.Append(BinToAsc[thisByte >> 2]);
last = thisByte;
break;
case 1:
result.Append(BinToAsc[((last & 0x03) << 4) | ((thisByte & 0xf0) >> 4)]);
last = thisByte;
break;
case 2:
result.Append(BinToAsc[((last & 0x0f) << 2) | ((thisByte & 0xc0) >> 6)]);
if (i < inputLength - 1 || value.Length % 3 == 0) // avoid flushing the rest of the partial virtually appended #0 character
{
result.Append(BinToAsc[thisByte & 0x3f]);
}
last = 0;
break;
}
}
return result.ToString();
}
private static byte[] BfEncrypt(string value)
{
byte[] plain = Encoding.UTF8.GetBytes(value);
int originalLength = plain.Length;
// aligns and zeropads input buffer
int padding = BlowfishECB.BLOCK_SIZE - (originalLength % BlowfishECB.BLOCK_SIZE); // number of bytes to pad
int length = originalLength + padding;
byte[] inBuffer = new byte[length];
for (int i = originalLength; i < inBuffer.Length; i++)
{
inBuffer[i] = (byte)padding;
}
Array.Copy(plain, 0, inBuffer, 0, originalLength);
BlowfishECB bfe = new BlowfishECB(BfKey, 0, BfKey.Length);
byte[] encrypted = new byte[inBuffer.Length];
// Make sure that the output buffers are clean. (redundant, as they are already zero-initialized)
Array.Clear(encrypted, 0, encrypted.Length);
bfe.Encrypt(inBuffer, 0, encrypted, 0, inBuffer.Length);
// Clean up the instance.
bfe.Invalidate();
return encrypted;
}
The Blowfish key (BfKey) is defined like this:
private static readonly byte[] BfKey = Encoding.UTF8.GetBytes("Albatros");
Is that possible to convert this function to PHP ? Thanks for your help.
Here is my codes:
$value = "LITE_9788248903420";
$key = "Albatros";
function pkcs_pad($text,$blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text.str_repeat(chr($pad),$pad);
}
$blockSize = mcrypt_get_block_size(MCRYPT_BLOWFISH,MCRYPT_MODE_ECB);
$padded = pkcs_pad($value,$blockSize);
$cipher = mcrypt_encrypt("blowfish",$key,$padded,"ecb");
echo "ENCRYPTED TEXT : ".base64_encode($cipher);
The output is : "ldwkCf5s1KHE9X7Jbh3OTYTxo0BzWBR9", NOT "ZRkY0Ttgp852xLv7PVrCHMHlco-nK-Fx"
Aucun commentaire:
Enregistrer un commentaire