Laravel中的AES 256 CBC加密和Android中的解密 [英] AES 256 CBC encryption in Laravel and Decryption in android

查看:216
本文介绍了Laravel中的AES 256 CBC加密和Android中的解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是这样的:我有一个密码,该密码在Laravel 5.6中使用AES-256-CBC进行加密并将其发送到android设备,问题是我无法找到一种方法来解密它,因为我知道找到了一种提取IV和加密值的方法,该密钥在Android设备上可用!

So my problem is this: i have a password that i'm encrypting in Laravel 5.6 with AES-256-CBC and send it to an android device, problem is i can't find a way to decrypt it knowing that i found a way to extract the IV and the encrypted value and the key is available on the android device !

如果我在Android设备上使用此代码使用AES-128-CBC,则我成功解密了该值,但未通过AES-256-CBC密码,并且我没有不明白问题出在哪里!
代码:

I'm successfully decrypting the value if i use AES-128-CBC using this code on the android device, but failing the AES-256-CBC cypher and i don't understand where the problem is ! The code :

public static String decrypt(byte[] keyValue, String ivValue, String encryptedData) throws Exception {
    Key key = new SecretKeySpec(keyValue, "AES");
    byte[] iv = Base64.decode(ivValue.getBytes("UTF-8"), Base64.DEFAULT);
    byte[] decodedValue = Base64.decode(encryptedData.getBytes("UTF-8"), Base64.DEFAULT);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS7Padding");
    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
    byte[] decValue = c.doFinal(decodedValue);

   return new String(decValue);
}

在什么情况下,指定此代码应使用AES-128而不是256 ?以及我该如何更改!

At what instance it's specified that this code should use AES-128 and not 256 ? and how can i change it !

谢谢!

编辑

PHP代码如下:

$cipher="AES-256-CBC";
$key='somerandomkeyof32byteslong';
$crypt=new Encrypter($key,$cipher);
$result=$crypt->encryptString('oussama');
//i'm sending the result to the android device


推荐答案

尝试这个

Try this one

Security.java

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

import org.apache.commons.codec.binary.Base64;

public class Security {
public static String encrypt(String input, String key){
  byte[] crypted = null;
  try{
    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, skey);
      crypted = cipher.doFinal(input.getBytes());
    }catch(Exception e){
        System.out.println(e.toString());
    }
    return new String(Base64.encodeBase64(crypted));
}

public static String decrypt(String input, String key){
    byte[] output = null;
    try{
      SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(Cipher.DECRYPT_MODE, skey);
      output = cipher.doFinal(Base64.decodeBase64(input));
    }catch(Exception e){
      System.out.println(e.toString());
    }
    return new String(output);
}

public static void main(String[] args) {
  String key = "1234567891234567";
  String data = "example";
  System.out.println(Security.decrypt(Security.encrypt(data, key), key));
  System.out.println(Security.encrypt(data, key));      
}   
}

Security.php

class Security {
    public static function encrypt($input, $key) {
    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);        
    $input = Security::pkcs5_pad($input, $size); 
    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); 
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv); 
    $data = mcrypt_generic($td, $input); 
    mcrypt_generic_deinit($td); 
    mcrypt_module_close($td); 
    $data = base64_encode($data); 
    return $data; 
} 

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

public static function decrypt($sStr, $sKey) {
    $decrypted= mcrypt_decrypt(
        MCRYPT_RIJNDAEL_128,
        $sKey, 
        base64_decode($sStr), 
        MCRYPT_MODE_ECB
    );
    $dec_s = strlen($decrypted); 
    $padding = ord($decrypted[$dec_s-1]); 
    $decrypted = substr($decrypted, 0, -$padding);
    return $decrypted;
}   
}?>

Example.php

<?php
include 'security.php';

$value = 'plain text';

$key = "your key"; //16 Character Key

echo "Encrypt =>"."<br><br>";
echo  Security::encrypt($value, $key);

echo "<br><br>"."Decrypt =>"."<br><br>";

echo Security::decrypt("AES Encrypted response",$key);
//echo Security::decrypt(Security::encrypt($value, $key), $key);

?>

这篇关于Laravel中的AES 256 CBC加密和Android中的解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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