在Laravel中的AES 256 CBC加密和在android中的解密

时间:2018-05-16 13:02:57

标签: android laravel encryption aes

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

如果我在Android设备上使用此代码使用AES-128-CBC,我成功解密了该值,但是AES-256-CBC cypher失败了,我不明白问题出在哪里! 代码:

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?我怎么能改变它!

提前致谢!

修改

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

2 个答案:

答案 0 :(得分:1)

试试这个

<强> 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);

?>

答案 1 :(得分:0)

如果你需要256位密钥长度的connection.CreateTable<Product>(); ,你可以这样做:

AES
当你想使用java类的android时,Android参考有时比oracle更好。这是reference

但请记住,这只是api 26+。如果您需要支持以前的版本(我认为您需要这样做),您可以编译openssl并在JNI中使用它。或者为java找到另一个加密库。

相关问题