<code>public static void main(String[] args) throws Exception {
String data = "4097c9863a1ee0b82d53e103cc6262e56bab259f5f3c01b0c3a3be1ddfd511ff";
String des_key = "cputest";
String decode_data = aes_decode(des_key,data);
System.out.println(decode_data);
}
private static Key GetAESKey(String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(256, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
return secretKeySpec;
}
private static String aes_encode(String des_key, String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, key);
return byteArr2HexStr(cipher.doFinal(args[0].getBytes()));
}
private static String aes_decode(String des_key,String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, key);
return new String(cipher.doFinal(hexStr2ByteArr(args[0])));
}
public static String byteArr2HexStr(byte[] value) throws Exception {
int i = value.length;
StringBuffer buf = new StringBuffer(i * 2);
for(int j = 0; j < i; ++j) {
int k;
for(k = value[j]; k < 0; k += 256) {
}
if (k < 16) {
buf.append("0");
}
buf.append(Integer.toString(k, 16));
}
return buf.toString();
}
public static byte[] hexStr2ByteArr(String value) throws Exception {
byte[] bs1 = value.getBytes();
int i = bs1.length;
byte[] bs2 = new byte[i / 2];
for(int j = 0; j < i; j += 2) {
String str = new String(bs1, j, 2);
bs2[j / 2] = (byte)Integer.parseInt(str, 16);
}
return bs2;
}
</code>
<code>public static void main(String[] args) throws Exception {
String data = "4097c9863a1ee0b82d53e103cc6262e56bab259f5f3c01b0c3a3be1ddfd511ff";
String des_key = "cputest";
String decode_data = aes_decode(des_key,data);
System.out.println(decode_data);
}
private static Key GetAESKey(String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(256, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
return secretKeySpec;
}
private static String aes_encode(String des_key, String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, key);
return byteArr2HexStr(cipher.doFinal(args[0].getBytes()));
}
private static String aes_decode(String des_key,String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, key);
return new String(cipher.doFinal(hexStr2ByteArr(args[0])));
}
public static String byteArr2HexStr(byte[] value) throws Exception {
int i = value.length;
StringBuffer buf = new StringBuffer(i * 2);
for(int j = 0; j < i; ++j) {
int k;
for(k = value[j]; k < 0; k += 256) {
}
if (k < 16) {
buf.append("0");
}
buf.append(Integer.toString(k, 16));
}
return buf.toString();
}
public static byte[] hexStr2ByteArr(String value) throws Exception {
byte[] bs1 = value.getBytes();
int i = bs1.length;
byte[] bs2 = new byte[i / 2];
for(int j = 0; j < i; j += 2) {
String str = new String(bs1, j, 2);
bs2[j / 2] = (byte)Integer.parseInt(str, 16);
}
return bs2;
}
</code>
public static void main(String[] args) throws Exception {
String data = "4097c9863a1ee0b82d53e103cc6262e56bab259f5f3c01b0c3a3be1ddfd511ff";
String des_key = "cputest";
String decode_data = aes_decode(des_key,data);
System.out.println(decode_data);
}
private static Key GetAESKey(String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(key.getBytes());
kgen.init(256, random);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
return secretKeySpec;
}
private static String aes_encode(String des_key, String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, key);
return byteArr2HexStr(cipher.doFinal(args[0].getBytes()));
}
private static String aes_decode(String des_key,String data) throws Exception {
String[] args = new String[]{data};
Key key = GetAESKey(des_key);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, key);
return new String(cipher.doFinal(hexStr2ByteArr(args[0])));
}
public static String byteArr2HexStr(byte[] value) throws Exception {
int i = value.length;
StringBuffer buf = new StringBuffer(i * 2);
for(int j = 0; j < i; ++j) {
int k;
for(k = value[j]; k < 0; k += 256) {
}
if (k < 16) {
buf.append("0");
}
buf.append(Integer.toString(k, 16));
}
return buf.toString();
}
public static byte[] hexStr2ByteArr(String value) throws Exception {
byte[] bs1 = value.getBytes();
int i = bs1.length;
byte[] bs2 = new byte[i / 2];
for(int j = 0; j < i; j += 2) {
String str = new String(bs1, j, 2);
bs2[j / 2] = (byte)Integer.parseInt(str, 16);
}
return bs2;
}
//This is a java code example, how to use php to achieve this equivalent decryption method, please ask me how to do so to write it. This is a java code example, how to use php to achieve this equivalent decryption method, please ask me how to do so to write it.That’s all the details I need
New contributor
xiaolan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.