Android AES的ECB和CTR加解密代码实现

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (InvalidAlgorithmParameterException e) {

e.printStackTrace();

}

return encodeStr;

}

/**

  • AES CTR 解密

  • 填充为PKCS5

  • @param encode 加密过后的文件

  • @param iv 偏移量

  • @param key 秘钥文件

*/

public static String decryptCRT(String encode, String iv, String key) {

String decoded = “”;

try {

byte[] bytes = Hex.decode(encode);

IvParameterSpec ivSpec = new IvParameterSpec(

iv.getBytes());

Key keys = new SecretKeySpec(key.getBytes(), “AES”);

Cipher cipher = Cipher.getInstance(“AES/CTR/PKCS5Padding”);

cipher.init(Cipher.DECRYPT_MODE, keys, ivSpec);

byte[] ret = cipher.doFinal(bytes);

decoded = new String(ret);

return decoded.trim();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (InvalidAlgorithmParameterException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}

return decoded;

}

/**

  • AES ECB加密

  • 填充为 “\0” noPadding

  • @param content 要加密的内容

  • @param key 加密文件的秘钥

  • @return 输出Base64的结果

**/

public static String encryptECB(byte[] content, String key) {

String encodeStr = “”;

try {

byte[] keyBytes = key.getBytes();

SecretKeySpec se
cretKeySpec = new SecretKeySpec(keyBytes, “AES”);

Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

//加密数据

int length = content.length;

int add = 16 - length % 16;

if (add == 16) {

add = 0;

}

byte[] contentNew = new byte[length + add];

//用’\0’加密

for (int i = 0; i < length + add; i++) {

if (i < length) {

contentNew[i] = content[i];

} else {

contentNew[i] = ‘\0’ ;

}

}

byte[] resultBytes = cipher.doFinal(contentNew);

//结果用Base64

encodeStr = new String(Base64Util.encode(resultBytes));

return encodeStr;

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

}

return encodeStr;

}

/**

  • AES ECB解密

  • @param encode 加密过后的文件

  • @param key 秘钥文件

*/

public static String decryptECB(String encode, String key) {

String decoded = “”;

byte[] decodeBase64 = Base64.decode(encode);

int length = decodeBase64.length;

int add = 16 - length % 16;

if (add == 16) {

add = 0;

}

byte[] decodeContentNew = new byte[length + add];

//填充’\0’

for (int i = 0; i < length + add; i++) {

if (i < length) {

decodeContentNew[i] = decodeBase64[i];

} else {

decodeContentNew[i] = ‘\0’ ;

}

}

try {

Key keys = new SecretKeySpec(key.getBytes(), “AES”);

Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”);

cipher.init(Cipher.DECRYPT_MODE, keys);

byte[] ret = cipher.doFinal(decodeContentNew);

decoded = new String(ret);

return decoded.trim();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (NoSuchPaddingException e) {

e.printStackTrace();

} catch (IllegalBlockSizeException e) {

e.printStackTrace();

} catch (BadPaddingException e) {

e.printStackTrace();

}
return decoded;
}

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值