我刚刚得到这个工作。要做的是在填充之前将字符串编码为UTF-8,然后发送字节数组进行加密。UTF-8 将重音字符表示为两个十六进制字节 c3 xx,因此如果包含需要编码的字符,转换后的字符串会更长。顺便说一句,c3 字符是“A”,顶部有一个“~”,所以如果你的解密出现了这些奇怪的字符,那么你还没有重新编码解密。
import java.security.NoSuchAlgorithmException;
导入 javax.crypto.Cipher;导入 javax.crypto.NoSuchPaddingException;导入 javax.crypto.spec.IvParameterSpec;导入 javax.crypto.spec.SecretKeySpec;
导入android.util.Log;
公共类 MCrypt {
private IvParameterSpec ivspec;
private SecretKeySpec keyspec;
private Cipher cipher;
private String iv = "cant hear you";
private String SecretKey = "top secret";
public MCrypt()
{
ivspec = new IvParameterSpec(iv.getBytes());
keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
public byte[] encrypt(String text) throws Exception{
if(text == null || text.length() == 0) throw new Exception("Empty string");
byte[] bs = text.getBytes("UTF-8");
byte[] toEncrypt = padBytes(bs);
byte[] encrypted = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(toEncrypt);
} catch (Exception e){
throw new Exception("[encrypt] " + e.getMessage());
}
return encrypted;
}
public byte[] decrypt(String code) throws Exception{
if(code == null || code.length() == 0) throw new Exception("Empty string");
byte[] decrypted = null;
try {
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
decrypted = cipher.doFinal(hexToBytes(code));
} catch (Exception e){
throw new Exception("[decrypt] " + e.getMessage());
}
return decrypted;
}
public static String bytesToHex(byte[] data){
if (data==null){
return null;
}
int len = data.length;
String str = "";
for (int i=0; i<len; i++) {
if ((data[i]&0xFF)<16)
str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
else
str = str + java.lang.Integer.toHexString(data[i]&0xFF);
}
return str;
}
public static byte[] hexToBytes(String str) {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
}
return buffer;
}
}
private static byte[] padBytes(byte[] source){
char paddingChar = ' ';
int size = 16;
int x = source.length % size;
int padLength = size - x;
int bufferLength = source.length + padLength;
byte[] ret = new byte[bufferLength];
int i = 0;
for ( ; i < source.length; i++){
ret[i] = source[i];
}
for ( ; i < bufferLength; i++){
ret[i] = (byte)paddingChar;
}
return ret;
}
} // class close
pHp 方面是相似的......(我不知道如何在这里输入 pHp 代码!!!!
class MCrypt
{
private $iv = 'adfdadfgfd'; #Same as in JAVA
private $key = 'adfadfdfdafadfa'; #Same as in JAVA
function __construct()
{
}
function encrypt($str) {
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$s = padString(utf8_encode($str));
$encrypted = mcrypt_generic($td, $s);
//echo $encrypted;
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return bin2hex($encrypted);
}
function decrypt($code) {
$code = $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $this->key, $iv);
$decrypted = mdecrypt_generic($td, $code);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return (trim(utf8_decode($decrypted)));
}
protected function hex2bin($hexdata) {
$bindata = '';
for ($i = 0; $i < strlen($hexdata); $i += 2) {
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
}
就是这样,真的很简单......哦,顺便说一句,如果你自己做填充,那么方法是填充还是NoPadding都没有关系!