Android Aes128

邹言 2018-02-01 05:52:38
密钥是16进制字符串: 313730313231DFF3DC98800000000000
要解密的也是十六进制的字符串:346364310A9EA01D1C4E8FA7BFA64083
遇到的问题是Aes128的密钥要求是16位的,我密钥这边应该怎么转化?
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
String key16 = new String(Conversion.hexStringToBytes(key));
用上面那个方法好像不行,求解决,或者是我的想法是错的,,求大神给个解决方法
或者求个能用的库
硬件给了工具解码出来的截图,没有偏移应该是ECB吧,


最后附上我现在用的解码工具类
public class AESUtils {
/** 算法/模式/填充 **/
private static final String CipherMode = "AES/ECB/NoPadding";
/**
* 创建密钥
*
* @param password
* 例如:"0123456701234567" 128位 16*8 所有密钥长度不能超过16字符中文占两个。192 24;
* 256 32
* @return SecretKeySpec 实例
*/
private static SecretKeySpec createKey(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(16);
sb.append(password);
while (sb.length() < 16) {
sb.append("0");
}
if (sb.length() > 16) {
sb.setLength(16);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
/**
* 加密字节数据
*
* @param content
* 需要加密的字节数组
* @param password
* 密钥 128 <16个字节 192 <24,256 <32个字节
* @return 加密完后的字节数组
*/
public static byte[] encrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 加密(结果为16进制字符串)
*
* @param content
* 要加密的字符串
* @param password
* 密钥
* @return 加密后的16进制字符串
*/
public static String encrypt(String content, String password) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, password);
String result = byte2hex(data);
return result;
}
/** 解密字节数组 **/
public static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/** 解密16进制的字符串为字符串 **/
public static String decrypt(String content, String password) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password);
if (data == null)
return null;
String result = null;
result = Conversion.byteArrayToHexStr(data);
return result;
}
/**
* 字节数组转成16进制字符串
*
* @param b
* @return 16进制字符串
*/
public static String byte2hex(byte[] b) { // 一个字节的数,
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 转成大写
}
/**
* 将hex字符串转换成字节数组 *
*
* @return 字节数组
*/
private static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}

}
...全文
356 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
邹言 2018-02-03
  • 打赏
  • 举报
回复
感谢回复,是我理解错了,当晚就发现了
绝世酱油瓶 2018-02-02
  • 打赏
  • 举报
回复
你是不是理解错了?你的工具128bit密钥对应16字节,而你的密钥是16进制字符串: 313730313231DFF3DC98800000000000。这个不是刚好是16字节吗?2位字符表示一个16进制字节。不需要转换啊
邹言 2018-02-01
  • 打赏
  • 举报
回复
求回复在线等急

80,344

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧