Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Please give the code for all three php iphone and android

have to display the same encrypt code...

and the resultant value has to reproduce the same text what we given for encryption

i had java android code and iphone code has result the same value
Java
package com.example.aesalg;

import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;

public class AESCrypt {

    private final Cipher cipher;
    private final SecretKeySpec key;
    private AlgorithmParameterSpec spec;

    public AESCrypt(String password) throws Exception
    {
        // hash password with SHA-256 and crop the output to 128-bit for key
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        digest.update(password.getBytes("UTF-8"));
        byte[] keyBytes = new byte[32];
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

        cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        key = new SecretKeySpec(keyBytes, "AES");
        spec = getIV();
    }

    public AlgorithmParameterSpec getIV()
    {
        byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
        IvParameterSpec ivParameterSpec;
        ivParameterSpec = new IvParameterSpec(iv);

        return ivParameterSpec;
    }

    public String encrypt(String plainText) throws Exception
    {
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
        String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
        System.out.println("Encrypt Data"+ encryptedText);
        return encryptedText;
    }

    public String decrypt(String cryptedText) throws Exception
    {
        cipher.init(Cipher.DECRYPT_MODE, key, spec);
        byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
        byte[] decrypted = cipher.doFinal(bytes);
        String decryptedText = new String(decrypted, "UTF-8");
        System.out.println("Encrypt Data"+ decryptedText);
        return decryptedText;
    }
}

but i can t able to get in php, i am getting result for
text:muthu
key:u

in mobile i am getting result as
MhPPaUb7eMbol6zUKjHcrA==

this is my php code
PHP
$data_to_encrypt = "muthu";
$key128 = "u";
$iv = "0000000000000000";

$cc = $data_to_encrypt;
$key = $key128;
$iv =  $iv;
$length = strlen($cc);

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc',$iv);

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);

echo "encrypted: " . $encrypted;
echo "";
echo "length:".strlen($encrypted);
echo "<br />";
echo "decrypted: " . substr($decrypted, 0, $length);

i am getting result as
hlxZgzP+0afprqIzYyEKSw==

help me to finish

thanks in advance
Posted
Updated 29-Aug-13 2:39am
v4
Comments
[no name] 29-Aug-13 6:38am    
"its very urgent", no it's not urgent at all. Your lack of planning does not make it an emergency for anyone else.
Member 10239759 29-Aug-13 7:07am    
ok whatever it might be i need the solution for this problem can you help me
[no name] 29-Aug-13 7:21am    
And what problem is that? You just dumped your code here and rudely asked us to do your job for you. No mention of what input you were putting in, what output you are getting, how much you are paying for someone to do your job for you, how you are going to pay them... Not that you will find someone here that will do this for you anyway.
Member 10239759 29-Aug-13 7:26am    
i am getting result for

text:muthu
key:u

in mobile i am getting result as

MhPPaUb7eMbol6zUKjHcrA==


$data_to_encrypt = "muthu";
$key128 = "u";
$iv = "0000000000000000";

$cc = $data_to_encrypt;
$key = $key128;
$iv = $iv;
$length = strlen($cc);

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc',$iv);

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);

echo "encrypted: " . $encrypted;
echo "<br/?-->";
echo "length:".strlen($encrypted);
echo "<br/>";
echo "decrypted: " . substr($decrypted, 0, $length);




this is my php code
i am getting hlxZgzP+0afprqIzYyEKSw==

1 solution

Hello Member,

I think PHP mcrypt function uses a zero byte padded input string. So in your java code set the transformation as AES/CBC/NoPadding and then pad the input string with 0. Following code snippets yields identical results.

PHP Code
PHP
<?php
$plaintext = "muthu";
$key256    = "12345678901234561234567890123456";
$iv        = "1234567890123456";

$plaintext_utf8 = utf8_encode($plaintext);
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', $iv);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key256, $plaintext_utf8, MCRYPT_MODE_CBC, $iv);
echo base64_encode($ciphertext);
?>

Java Code
Java
String strKey = "12345678901234561234567890123456";
byte[] arrKey = strKey.getBytes("UTF-8");
byte[] ivKey = new byte[16];
byte[] bytIn = new byte[] {'m', 'u', 't', 'h', 'u', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
System.arraycopy(arrKey, 0, ivKey, 0, 16);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(arrKey, "AES"), new IvParameterSpec(ivKey));
byte[] enc = cipher.doFinal(bytIn);
return new String(Base64.encodeBase64(enc));

Regards,
 
Share this answer
 
Comments
Member 10239759 29-Aug-13 10:28am    
return new String(Base64.encodeBase64(enc));

Base64.encodeBase64(enc)

showing undefined function android

thanks in advance
Prasad Khandekar 29-Aug-13 12:48pm    
Use Base64.encode(enc, Base64.DEFAULTS) instead. (http://developer.android.com/reference/android/util/Base64.html)

Regards,
Member 10239759 30-Aug-13 6:55am    
thanks mr.Prasad khandekar its working fine
i have few question

byte[] bytIn = new byte[] {'m', 'u', 't', 'h', 'u', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

but i want place a big string.

how to place a string and

also another help

decryption in php and java

encryption and decryption in iphone needed

for iphone also need to produce same output as got in php and android

thanks in advance mr.Prasad khandekar
Member 10239759 2-Sep-13 5:42am    
thanks i found solution for this with PKCS7Padding in php
now i fixed the issue

thank for you reply
Prasad Khandekar 2-Sep-13 10:17am    
Great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900