适用于php,iphone和android的aes cbc 128位algorthim [英] aes cbc 128 bit algorthim for php ,iphone and android

查看:51
本文介绍了适用于php,iphone和android的aes cbc 128位algorthim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给出所有三个php iphone和android的代码



必须显示相同的加密代码...



和结果值必须重现我们给加密的相同文本



i有java android代码和iphone代码有相同的值

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

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;
    }
}



但是我无法进入php,我得到的结果是


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==



这是我的PHP代码


this is my php code

$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得到结果


i am getting result as

hlxZgzP+0afprqIzYyEKSw==



帮我完成



提前感谢


help me to finish

thanks in advance

推荐答案

data_to_encrypt = muthu;
data_to_encrypt = "muthu";


key128 = U;
key128 = "u";


iv = 0000000000000000\" ;

iv = "0000000000000000";


这篇关于适用于php,iphone和android的aes cbc 128位algorthim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆