php7 解密 java aes ecb pkcs5padding(求教php AES/CBS/PKCS5Padding加密)

本文目录
- 求教php AES/CBS/PKCS5Padding加密
- php aes ecb pkcs7padding 加密
- java 的cipher AES/CBC/PKCS5Padding 加密后,使用openssl的AES_cbc_encrypt无法解密
- Cipher.getInstance(“AES/ECB/PKCS5Padding“)怎么解决
- Java和js使用AES/CBC/PKCS5Padding(或者7)得到相同的密文
- java实现aes加密或者解密,不用工具包的怎么做
求教php AES/CBS/PKCS5Padding加密
ecb加密方式,其实底层是调用AES_encrypt接口,你可以去看源码。 下面的代码给你一个参考,只不过,我是在调用aes_encrypt外面,自己严格控制了in和out的长度,out的长度大于in,并且必须是大于等于16的整数倍。
php aes ecb pkcs7padding 加密
《?php
class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = ’’;
protected $iv = ’’;
public function set_cipher($cipher)
{
$this-》cipher = $cipher;
}
public function set_mode($mode)
{
$this-》mode = $mode;
}
public function set_iv($iv)
{
$this-》iv = $iv;
}
public function set_key($key)
{
$this-》secret_key = $key;
}
public function require_pkcs5()
{
$this-》pad_method = ’pkcs5’;
}
protected function pad_or_unpad($str, $ext)
{
if ( is_null($this-》pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . ’::’ . $this-》pad_method . ’_’ . $ext . ’pad’;
if ( is_callable($func_name) )
{
$size = mcrypt_get_block_size($this-》cipher, $this-》mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
}
protected function pad($str)
{
return $this-》pad_or_unpad($str, ’’);
}
protected function unpad($str)
{
return $this-》pad_or_unpad($str, ’un’);
}
public function encrypt($str)
{
$str = $this-》pad($str);
$td = mcrypt_module_open($this-》cipher, ’’, $this-》mode, ’’);
if ( empty($this-》iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this-》iv;
}
mcrypt_generic_init($td, $this-》secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
$rt=base64_encode($cyper_text);
//$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $rt;
}
public function decrypt($str){
$td = mcrypt_module_open($this-》cipher, ’’, $this-》mode, ’’);
if ( empty($this-》iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this-》iv;
}
mcrypt_generic_init($td, $this-》secret_key, $iv);
//$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this-》unpad($rt);
}
public static function hex2bin($hexdata) {
$bindata = ’’;
$length = strlen($hexdata);
for ($i=0; $i《$length;$i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad 》 strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
$keyStr = ’187522’; //私钥
$aes = new CryptAES();
$aes-》set_key($keyStr);
/* $arr = array(’1’=》’aaa1111’,’2’=》array(1,2));
$bb = serialize($arr);
//echo $aes-》encrypt($arr);
$a = $aes-》decrypt($aes-》encrypt($bb));
$c=unserialize($a); */
?》
java 的cipher AES/CBC/PKCS5Padding 加密后,使用openssl的AES_cbc_encrypt无法解密
***隐藏网址***
likang。me/blog/2013/06/05/python-pycrypto-aes-ecb-pkcs-5/
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import os
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s
key = os.urandom(16) # the length can be (16, 24, 32)
text = ’to be encrypted’
cipher = AES.new(key)
encrypted = cipher.encrypt(pad(text)).encode(’hex’)
print encrypted # will be something like ’f456a6b0e54e35f2711a9fa078a76d16’
decrypted = unpad(cipher.decrypt(encrypted.decode(’hex’)))
print decrypted # will be ’to be encrypted’
Cipher.getInstance(“AES/ECB/PKCS5Padding“)怎么解决
在生成密钥时,我用自己提供的一个密码,用这句SecretKeySpec aesKey = new SecretKeySpec("12345678".getBytes(), "AES"); “12345678”提供的个任意长度的密码,通过它生成。
package com.sun.aes;
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
public class AES {
/**
* 加密
* @param String src 加密字符串
* @param String key 密钥
* @return 加密后的字符串
*/
public static String Encrypt(String src, String key) throws Exception {
// 判断密钥是否为空
if (key == null) {
System.out.print("密钥不能为空");
return null;
}
// 密钥补位
int plus= 16-key.length();
byte data = key.getBytes("utf-8");
byte;
byte plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
for(int i=0;i《16;i++)
{
if (data.length 》 i)
raw;
else
raw;
}
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // 算法/模式/补码方式
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte encrypted = cipher.doFinal(src.getBytes("utf-8"));
//return new Base64().encodeToString(encrypted);//base64
return binary(encrypted, 16); //十六进制
}
/**
* 解密
* @param String src 解密字符串
* @param String key 密钥
* @return 解密后的字符串
*/
public static String Decrypt(String src, String key) throws Exception {
try {
// 判断Key是否正确
if (key == null) {
System.out.print("Key为空null");
return null;
}
// 密钥补位
int plus= 16-key.length();
byte data = key.getBytes("utf-8");
byte;
byte plusbyte={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
for(int i=0;i《16;i++)
{
if (data.length 》 i)
raw;
else
raw;
}
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//byte encrypted1 = new Base64().decode(src);//base64
byte encrypted1 = toByteArray(src);//十六进制
try {
byte original = cipher.doFinal(encrypted1);
String originalString = new String(original,"utf-8");
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}
/**
* 将byte转为各种进制的字符串
* @param bytes byte
* @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
* @return 转换后的字符串
*/
public static String binary(byte bytes, int radix){
return new BigInteger(1, bytes).toString(radix); // 这里的1代表正数
}
/**
* 16进制的字符串表示转成字节数组
*
* @param hexString 16进制格式的字符串
* @return 转换后的字节数组
**/
public static byte toByteArray(String hexString) {
if (hexString.isEmpty())
throw new IllegalArgumentException("this hexString must not be empty");
hexString = hexString.toLowerCase();
final byte;
int k = 0;
for (int i = 0; i 《 byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
byteArray = (byte) (high 《《 4 | low);
k += 2;
}
return byteArray;
}
public static void main(String args) throws Exception {
// 密钥
String key = "smph30151208shao";
// 需要加密的字符串
String src = "出版社";
System.out.println(src);
// 加密
String enString = Encrypt(src, key);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = Decrypt(enString, key);
System.out.println("解密后的字串是:" + DeString);
}
}
Java和js使用AES/CBC/PKCS5Padding(或者7)得到相同的密文
首先准备一份明文和秘钥:
var plaintText = ’aaaaaaaaaaaaaaaa’; // 明文
var keyStr = ’bbbbbbbbbbbbbbbb’; // 一般key为一个字符串
参看官网文档,AES方法是支持AES-128、AES-192和AES-256的,加密过程中使用哪种加密方式取决于传入key的类型,否则就会按照AES-256的方式加密。
CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.
由于Java就是按照128bit给的,但是由于是一个字符串,需要先在前端将其转为128bit的才行。
java实现aes加密或者解密,不用工具包的怎么做
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
*
* @author wchun
*
* AES128 算法,加密模式为ECB,填充模式为 pkcs7(实际就是pkcs5)
*
*
*/
public class AES {
static final String algorithmStr="AES/ECB/PKCS5Padding";
static private KeyGenerator keyGen;
static private Cipher cipher;
static boolean isInited=false;
//初始化
static private void init()
{
//初始化keyGen
try {
keyGen=KeyGenerator.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
keyGen.init(128);
//初始化cipher
try {
cipher=Cipher.getInstance(algorithmStr);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isInited=true;
}
public static byte GenKey()
{
if(!isInited)//如果没有初始化过,则初始化
{
init();
}
return keyGen.generateKey().getEncoded();
}
public static byte keyBytes)
{
byte encryptedText=null;
if(!isInited)//为初始化
{
init();
}
Key key=new SecretKeySpec(keyBytes,"AES");
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
encryptedText=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encryptedText;
}
//解密为byte
public static byte keyBytes)
{
byte originBytes=null;
if(!isInited)
{
init();
}
Key key=new SecretKeySpec(keyBytes,"AES");
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//解密
try {
originBytes=cipher.doFinal(content);
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return originBytes;
}
}

更多文章:
sometime(英语词组 sometimes, some times, sometime和some time的区别)
2026年1月12日 03:00
acquisition词组(雅思词汇应用:达到雅思7分不得不知道的词组!)
2026年1月31日 17:15
finder 翻译(英语翻译 怎么翻译顺口 Be a better deal finder!)
2025年12月29日 04:15
网页视频下载器手机版(iPhone 手机有什么浏览器可以下载网页链接视频)
2025年9月11日 07:15
嵌入式开发板6818属于armv8架构(工业arm板,像三星6818开发板能做吗)
2026年7月4日 15:00
苹果imgplay怎么导入字体(imgplay字体特效怎么加描边)
2025年10月18日 07:15
headfirstjava中文版pdf(有木有关于Java虚拟机的书籍推荐感激不尽)
2025年12月29日 01:45
51单片机接口线怎么连(USB接口怎么接51单片机最小系统的引脚的求大神赐教)
2026年4月22日 12:15
查找替换中的任意数字和任意字符的区别?表示任意 字符 的正则表达式 符号
2026年9月13日 16:30
background image属性(cssbackground-image属性怎么用)
2025年10月6日 02:30
paper软件下载(小红车wallpaper电脑端怎么下载)
2025年12月21日 15:15
accepting of(下面的句子里accepting是什么从句啊)
2025年12月6日 23:45
editorial assignment什么意思啊(Assignment 代表什么意思)
2025年12月11日 14:15








