dien thoai di dong , dau gia , the gioi smartphone , download game mobile , smartphone , tang truong , khoa hoc cong nghe thong cong , mua ban sim , Smartphone gia re , cong nghe tuong lai , cong nghe 360 , giai tri guongmat.org , su kien trong ngay , thoi trang hi tech , thong tin 360 , may tinh bang , perfect body , kasuman.com , gia vang hom nay , tin tuc an toan , kinh te viet nam , xay dung viet nam , thoi trang , thoi trang , phu nu viet nam , tin tuc moi online , dich vu bao ve viet nam , bao ve viet nam , cong ty bao ve viet nam , tin tuc moi online , giai tri 24h , tin tuc 24h




 03 Dec 2009 @ 1:08 PM 

encrypting and decrypting between different languages and character sets can be painful. It took me many hours of tweaking to get to a working robust solution.

The PHP code will encrypt and decrypt a string.

The java code will successfully decrypt a string encrypted by the PHP code.

Encrypt and decrypt in PHP:

function encryptString($pin)
{
    $cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');
    // The block-size of the Blowfish algorithm is 64-bits, therefore our IV
    // is always 8 bytes:
    $iv =  '12345678';
    $key = encryptKey(); //32chars = 256bit encryption
    // The strengh of the encryption is determined by the length of the key
    // passed to mcrypt_generic_init
    if (mcrypt_generic_init($cipher, $key, $iv) != -1)
    {
        $cipherText = mcrypt_generic($cipher, $pin);
        mcrypt_generic_deinit($cipher);
        return $cipherText;
    } else
    {
        mcrypt_generic_deinit($cipher);
        return "";
    }
}
 
function decryptString($pin)
{
    $iv =  '12345678';
    $key = encryptKey();
    return mcrypt_cbc(MCRYPT_BLOWFISH, $pin, $ciphertext, MCRYPT_DECRYPT, $iv);
}
 
function hex2bin($h)
{
    //this function is the opposite of php's bin2hex
    if (!is_string($h)) return null;
    $r='';
    for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
    return $r;
}
 
//usage:
$encrypted_string = encryptString('Some text to encrypt');
$decrypted_string = decryptString($encrypted_string);

To decrypt in JAVA

private static String decryptBlowfish(String to_decrypt)
{
    try
    {
        String iv = "12345678";
        byte[] IVBytes = getBytes(iv);
        if (IVBytes == null)
        {
            return null;
        }
        IvParameterSpec IV = new IvParameterSpec(IVBytes);
        String strkey = "12345678901234567890123456789012"; //must be the same key we used to encrypt the data
        byte[] strkeyBytes = getBytes(strkey);
        if (strkeyBytes == null)
        {
            return null;
        }
        SecretKeySpec key = new SecretKeySpec(strkeyBytes, "Blowfish");
        try
        {
            Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
            try
            {
                cipher.init(Cipher.DECRYPT_MODE, key, IV);
                try
                {
                    byte[] encrypted = getBytes(to_decrypt);
                    if (encrypted == null)
                    {
                        return null;
                    }
                    byte[] decrypted = cipher.doFinal(encrypted);
                    return new String(decrypted);
                } catch(Exception e)
                {
                    errorMsg = "ERROR: decryptBlowfish(dofinal) : to_decrypt :" + to_decrypt + " : message : " + e;
                    return null;
                }
            }catch (Exception e)
            {
                errorMsg = "ERROR: decryptBlowfish(init) :" + e;
                return null;
            }
        }catch (Exception e)
        {
            errorMsg = "ERROR: decryptBlowfish(getInstance) :" + e;
            return null;
        }
    } catch (Exception e)
    {
        errorMsg = "ERROR: decryptBlowfish :" + e;
        return null;
    }
}
 
private static String hexToString(String input, int groupLength)
{
    try
    {
        StringBuilder sb = new StringBuilder(input.length() / groupLength);
        for (int i = 0; i < input.length() - groupLength + 1; i += groupLength)
        {
            String hex = input.substring(i, i + groupLength);
            sb.append((char) Integer.parseInt(hex, 16));
        }
 
        return sb.toString();
    } catch (Exception e)
    {
        errorMsg = "ERROR: hexToString :" + e;
        return null;
    }
}
 
private static byte[] getBytes(String toGet)
{
    try
    {
        byte[] retVal = new byte[toGet.length()];
        for (int i = 0; i < toGet.length(); i++)
        {
            char anychar = toGet.charAt(i);
            retVal[i] = (byte)anychar;
        }
        return retVal;
    }catch(Exception e)
    {
        errorMsg = "ERROR: getBytes :" + e;
        return null;
    }
}
 
 
//USAGE
String stringSignature = hexToString(signature, 2);
String decryptSignature = decryptBlowfish(stringSignature);
Posted By: Zayin
Last Edit: 03 Dec 2009 @ 01:08 PM

EmailPermalinkComments (19)
Tags
Tags: , ,
Categories: java, php
 01 Dec 2009 @ 4:18 PM 
public static String encryptBlowfish(String to_encrypt, String strkey) {
  try {
    SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.ENCRYPT_MODE, key);
     return new String(cipher.doFinal(to_encrypt.getBytes()));
  } catch (Exception e) { return null; }
}
 
public static String decryptBlowfish(String to_decrypt, String strkey) {
  try {
     SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.DECRYPT_MODE, key);
     byte[] decrypted = cipher.doFinal(to_decrypt.getBytes());
     return new String(decrypted);
  } catch (Exception e) { return null; }
}
Posted By: Zayin
Last Edit: 01 Dec 2009 @ 04:18 PM

EmailPermalinkComments (3)
Tags
Tags: , , ,
Categories: java

 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 40
  • Comments » 74
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Portfolio



    No Child Pages.