php和j2me应用程序之间的En/decrypt

php和j2me应用程序之间的En/decrypt,php,encryption,java-me,bouncycastle,mcrypt,Php,Encryption,Java Me,Bouncycastle,Mcrypt,我有一个Blackberry应用程序(j2me),用于加密数据并将其发送到服务器、php页面,然后再发送回来。对于应用程序,我使用lcrypto-j2me-147(Bouncy Castle)和mcrypt作为php部分。问题在于,它们对相同的文本进行加密,并产生不同的结果。因此,两者不能互相发送加密数据 我的测试系统: PHP代码: <?php function printStringToHex($text) { $size = strlen($text); for($

我有一个Blackberry应用程序(j2me),用于加密数据并将其发送到服务器、php页面,然后再发送回来。对于应用程序,我使用lcrypto-j2me-147(Bouncy Castle)和mcrypt作为php部分。问题在于,它们对相同的文本进行加密,并产生不同的结果。因此,两者不能互相发送加密数据

我的测试系统: PHP代码:

<?php 
function printStringToHex($text)
{
    $size = strlen($text);
    for($i = 0; $i < $size; $i++)
    {
        echo dechex(ord($text[$i])) . " ";
    }
}

function encrypt($key, $input) 
{ 
    echo "<PRE>*** Encrypt *** </PRE>";
    echo "<PRE>Raw input: " . $input . "</PRE>";
    $size = mcrypt_get_block_size('twofish', 'ecb'); 
    echo "<PRE>Block: " . $size . "</PRE>";
    $input = pkcs5_pad($input, $size); 
    echo "<PRE>PKCS#5 padding: ";
    echo printStringToHex($input);
    echo "</PRE>";

    $td = mcrypt_module_open('twofish', '', 'ecb', ''); 
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
    mcrypt_generic_init($td, $key, $iv); 
    $data = mcrypt_generic($td, $input); 
    echo "<PRE>Raw output: " . $data . "</PRE>";
    echo "<PRE>Hex output: ";
    echo printStringToHex($data);
    echo "</PRE>";
    mcrypt_generic_deinit($td); 
    mcrypt_module_close($td); 
    $data = base64_encode($data); 
    echo "<PRE>B64 output: " . $data . "</PRE>";
    echo "<PRE>B64 output len: ";
    echo strlen($data) . "</PRE>";
    return $data; 
} 

function pkcs5_pad ($text, $blocksize) 
{ 
    $pad = $blocksize - (strlen($text) % $blocksize); 
    return $text . str_repeat(chr($pad), $pad); 
} 

$enc = encrypt("The key string", "The plain text string");
对于Java部分,我使用了一个示例:

public static byte[] encryptDecrypt(byte[] inputBytes, String keyString, boolean encrypt)
{ 
    try
    {
        if(encrypt) System.out.println("*** Encrypting *** "); else System.out.println("*** Decrypting *** ");
        System.out.println("Key: " + keyString);
        System.out.println("Input bytes (String): " +  new String(inputBytes));
        System.out.print("\nInput bytes (byte[]): ");
        printOutBytes(inputBytes);

        byte[] keyBytes = keyString.getBytes();
        KeyParameter key = new KeyParameter(keyBytes);
        TwofishEngine twofish = new TwofishEngine();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(twofish);
        cipher.init(encrypt, key);
        byte[] cipherBytes = new byte[cipher.getOutputSize(inputBytes.length)];
        int cipherLength = cipher.processBytes(inputBytes, 0, inputBytes.length, cipherBytes, 0);
        cipher.doFinal(cipherBytes, cipherLength);

        System.out.println("Cipher bytes (String): " + new String(cipherBytes));
        System.out.print("\nCipher bytes (byte[]): ");
        printOutBytes(cipherBytes);
        return cipherBytes;
    }   
    catch(InvalidCipherTextException e)
    {
        System.err.println(e.getMessage());
    }
    return null;
}
结果是:

Encrypt:
Raw input: The plain text string
Block: 16
PKCS#5 padding: 54 68 65 20 70 6c 61 69 6e 20 74 65 78 74 20 73 74 72 69 6e 67 b b b b b b b b b b b 
Raw output: Þˆ2L4ýé6öò8?‰ÛŒÇY˜W‘Š…HEÅ8=a[
Hex output: de 88 32 4c 34 8 fd e9 36 f6 f2 38 3f 89 db 8c c7 59 98 57 91 8a 85 48 45 16 2 c5 38 3d 61 5b 
B64 output: 3ogyTDQI/ek29vI4P4nbjMdZmFeRioVIRRYCxTg9YVs=
B64 output len: 44
Encrypting 
Key: The key string
Input bytes (String): The plain text string
Input bytes (byte[]): 54 68 65 20 70 6c 61 69 6e 20 74 65 78 74 20 73 74 72 69 6e 67 
Cipher bytes (String): ²Rð?=
r¾=??ùUí?9?rtæ?0ÊÃ(½~Å3
Cipher bytes (byte[]): b2 52 f0 87 81 3d 14 a 72 be 3d 85 80 f9 55 ed 9a 39 82 72 74 e6 91 30 ca c3 1 28 bd 7e c5 33 
B64 of encText: slLwh4E9FApyvj2FgPlV7Zo5gnJ05pEwysMBKL1+xTM=
B64 len: 44
有什么想法吗?我怀疑这是Java没有使用的初始化向量

许多人,
Andre

我最终使用Telmo Pimentel Mota建议的相同加密库创建了一个Servlet

你能创建一个Java Web服务而不是PHP吗?我这样问是因为,如果可以的话,两端都可以使用相同的jar库。谢谢你的评论。我认为你是对的,使用不同的语言是有问题的。我得看看在我的本地WAMP上设置什么来测试这个。。。