public function _encrypt($string, $key='')
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
$data = strtolower(bin2hex($data));
return $data;
}
public function _decrypt($string, $key='')
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
return json_decode($decrypted,true);
}
```php
```java
package com.gdin.yhk.common.util;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Demo {
protected static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
protected static final String KEY_ALGORITHM = "AES";
protected static final String ENCRYPT_CHARSET = "UTF-8";
static {
Security.addProvider(new BouncyCastleProvider());
}
protected static SecretKeySpec createKey(String password, Integer keysize) throws NoSuchAlgorithmException, UnsupportedEncodingException {
KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes(ENCRYPT_CHARSET));
kgen.init(keysize, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
return key;
}
public static void main(String[] args) throws Exception {
String content = "{\"paperNum\":\"440101200001011111\",\"phone\":\"13800000000\"}";
String password = "0c23c0ae-dfe3-4cec-9b6e-83d7fd1f2485";
try {
System.out.println("原内容:\n"+content);
String tempContent = encrypt(content,password);
System.out.println("加密后:\n"+tempContent);
tempContent = decrypt(tempContent, password);
System.out.println("解密后:\n"+tempContent);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String content, String password) {
try {
SecretKeySpec key = createKey(password, 128);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
byte[] byteContent = content.getBytes(ENCRYPT_CHARSET);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
if (result != null && result.length > 0) {
return parseByte2HexStr(result);
}
} catch (Exception e) {
}
return "";
}
public static String decrypt(String content, String password) {
try {
byte[] newContent = parseHexStr2Byte(content);
SecretKeySpec key = createKey(password, 128);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(newContent);
if (result != null && result.length > 0) {
return new String(result, ENCRYPT_CHARSET);
}
} catch (Exception e) {
}
return "";
}
protected static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
protected static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}