Java、PHP中的AES 128加密-输出差异
接着编写Java和PHP之间aes 128加密的代码(decryption-Java,encryption-PHP) Java代码Java、PHP中的AES 128加密-输出差异,java,php,encryption,cryptography,aes,Java,Php,Encryption,Cryptography,Aes,接着编写Java和PHP之间aes 128加密的代码(decryption-Java,encryption-PHP) Java代码 import java.security.MessageDigest; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Se
import java.security.MessageDigest;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.sun.jersey.core.util.Base64;
public class CipherUtils {
private static Cipher cipher;
private static SecretKeySpec key;
private static AlgorithmParameterSpec spec;
public static final String SEED_16_CHARACTER = "hello";
public CipherUtils() throws Exception {
// hash password with SHA-256 and crop the output to 128-bit for key
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(SEED_16_CHARACTER.getBytes("UTF-8"));
byte[] keyBytes = new byte[16];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
key = new SecretKeySpec(keyBytes, "AES");
spec = getIV();
}
public AlgorithmParameterSpec getIV() {
byte[] iv = { 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x46, 0x00, 0x23, 0x00, 0x00, 0x00 };
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));
return encryptedText;
}
public String decrypt(String cryptedText) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, key, spec);
byte[] bytes = Base64.decode(cryptedText);
byte[] decrypted = cipher.doFinal(bytes);
String decryptedText = new String(decrypted, "UTF-8");
return decryptedText;
}
}
PHP代码
class MCrypt {
private $hex_iv = '00500000000072000000460023000000 '; # converted JAVA byte code in to HEX and placed it here
private $key = 'hello'; #Same as in JAVA
function __construct() {
$this->key = hash('sha256', $this->key, true);
//echo $this->key.'<br/>';
}
function encrypt($str) {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($encrypted);
}
function decrypt($code) {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
$str = mdecrypt_generic($td, base64_decode($code));
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->strippadding($str);
}
private function addpadding($string, $blocksize = 16) {
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
private function strippadding($string) {
$slast = ord(substr($string, -1));
$slastc = chr($slast);
$pcheck = substr($string, -$slast);
if (preg_match("/$slastc{" . $slast . "}/", $string)) {
$string = substr($string, 0, strlen($string) - $slast);
return $string;
} else {
return false;
}
}
function hexToStr($hex) {
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2) {
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
}
到
因为我不想陷入安装jce unlimited strength策略文件的混乱。
任何帮助都将不胜感激。PKCS#5填充与PKCS#7填充相同,尽管官方只针对8字节块(请参阅)
但是,您不能仅在一端更改关键帧大小并期望它正常工作。更改密钥的一位就足以生成完全不同的密文(以及解密后的明文)。更改大小甚至会更改AES的基础密码
如果您不想麻烦处理辖区文件,那么最好也调整PHP键的大小;只取最左边的字节。您也可以直接使用Java中的Bouncy Castle轻量级API来使用256位AES。您确切地指的是哪一个PHP键$十六进制四='0050000000007200000046023000000';或者$key='hello'?现在,当我尝试加密字符串“Google 1324567389 20-12-2013”时,Java代码返回加密值“eln9uvq1fi2pue+xk8RvENBg5TMxpZuzEcAJkFFM/U=”而PHP代码返回值“IGiWwLcdKghmybgIPVYzqBGtWVn/Yw+hehmDywRy0z8=”。当我尝试将PHP加密字符串放入Java代码进行解密时,它会给我一个“javax.crypto.BadPaddingException:gived final block not ally padding”(给定的最后一个块没有正确填充),如果我在这里有点迂腐,我很抱歉,但是PKCS#5和PKCS#7填充并不相同,正如它们在您给出的链接中正确指出的那样。一些实现将错误地将PKCS#7填充称为PKCS#5填充。这没什么大不了的,但当我第一次寻找PKCS#7填充物时,却找不到,这确实让我很恼火。@MikkelK。在这个网站上有点迂腐也没关系。然而,没有“他们”,就是我在提供的链接中给出了Q和A:)哦,我甚至没有注意到。幸好你也把它写在了答案里:)
byte[] keyBytes = new byte[32];
byte[] keyBytes = new byte[16];