加密里面的padding

在使用加密模块的时候,比如javax.crypto.Cipher,会碰到padding不同的形式。在java里面规定了如下几种padding:

NoPadding: No padding.

 

OAEPWith<digest></digest>And<mgf></mgf>Padding: Optimal Asymmetric Encryption Padding scheme defined in PKCS #1, where <digest></digest>should be replaced by the message digest and <mgf></mgf>by the mask generation function. Example: OAEPWithMD5AndMGF1Padding.

 

PKCS5Padding: The padding scheme described in: RSA Laboratories, "PKCS #5: Password-Based Encryption Standard," version 1.5, November 1993.

 

SSL3Padding: The padding scheme defined in the SSL Protocol Version 3.0, November 18, 1996, section 5.2.3.2 (CBC block cipher)

通过google查到这篇描述padding的文章讲的比较清楚:

http://www.di-mgt.com.au/cryptopad.html

不同的padding方式如下:

  1. Pad with bytes all of the same value as the number of padding bytes (pkcs#5)
  2. Pad with 0x80 followed by zero bytes
  3. Pad with zeroes except make the last byte equal to the number of padding bytes
  4. Pad with zero (null) characters
  5. Pad with space characters
<think>好的,我现在需要帮助用户找到一个关于AES加密工具类的实现示例代码。首先,我得回忆一下用户提到的引用内容,看看里面有没有相关的示例。根据用户提供的引用[1]、[3]、[4]、[5]都有提到不同的AES工具类实现。 用户可能需要一个完整的Java代码示例,最好是包含加密和解密方法,并且有使用示例。引用[1]提到了一个EncryptionUtils类,引用[3]则是一个具体的AESTool类,引用[4]是Android平台的,而引用[5]则强调了注意事项和密钥管理的问题。 我需要综合这些信息,给出一个符合标准的工具类。首先确定使用AES/CBC/PKCS5Padding模式,因为引用[5]提到要明确指定模式和填充。然后需要生成一个随机的IV,并把它和加密后的数据一起返回,可能在加密结果中包含IV,这样解密时才能正确提取。 密钥的处理也很重要,用户提供的密钥可能需要经过处理,比如使用SHA-256哈希来生成合适的AES密钥长度。引用[5]提到不要硬编码密钥,所以工具类应该接受密钥字符串作为输入,并进行适当的转换。 然后,加密方法中需要生成IV,并初始化Cipher实例。加密后的数据需要将IV和密文结合,可能用Base64编码。解密的时候,需要从加密后的数据中分离出IV,然后进行解密。 代码的结构应该包括encrypt和decrypt两个静态方法,处理异常,并且有使用示例。同时,需要注意字符编码,比如使用UTF-8来处理字符串和字节数组的转换。 还要考虑跨平台兼容性,比如使用Base64编码,这样在Android和Java之间都能正确解析。另外,异常处理需要适当,可能在方法中抛出异常,让调用者处理。 现在,把这些思路整理成一个工具类,确保代码正确,符合AES的最佳实践。然后编写使用示例,展示如何调用加密和解密方法。同时,在注意事项里提到密钥管理、IV的使用、字符编码等要点,引用相关的引用内容作为支持。 最后,生成相关问题,可能涉及加密模式的选择、密钥存储、异常处理优化、跨平台问题等,帮助用户进一步了解AES的实现细节。</think>以下是一个基于Java实现的AES加密工具类示例,整合了安全实践和跨平台兼容性: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.Base64; public class AesUtils { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; private static final int IV_LENGTH = 16; // 加密方法 public static String encrypt(String plainText, String secretKey) throws Exception { byte[] keyBytes = generateKey(secretKey); SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES"); // 生成随机IV byte[] ivBytes = new byte[IV_LENGTH]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(ivBytes); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec); byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); // 组合IV和密文 byte[] combined = new byte[ivBytes.length + encrypted.length]; System.arraycopy(ivBytes, 0, combined, 0, ivBytes.length); System.arraycopy(encrypted, 0, combined, ivBytes.length, encrypted.length); return Base64.getEncoder().encodeToString(combined); } // 解密方法 public static String decrypt(String encryptedText, String secretKey) throws Exception { byte[] combined = Base64.getDecoder().decode(encryptedText); byte[] ivBytes = new byte[IV_LENGTH]; System.arraycopy(combined, 0, ivBytes, 0, IV_LENGTH); byte[] encrypted = new byte[combined.length - IV_LENGTH]; System.arraycopy(combined, IV_LENGTH, encrypted, 0, encrypted.length); SecretKeySpec secretKeySpec = new SecretKeySpec(generateKey(secretKey), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec); return new String(cipher.doFinal(encrypted), StandardCharsets.UTF_8); } // 生成256位密钥 private static byte[] generateKey(String secretKey) throws Exception { MessageDigest sha = MessageDigest.getInstance("SHA-256"); return sha.digest(secretKey.getBytes(StandardCharsets.UTF_8)); } // 使用示例 public static void main(String[] args) throws Exception { String originalText = "Hello World! 你好世界!"; String secretKey = "mySecureKey123"; String encrypted = encrypt(originalText, secretKey); System.out.println("加密结果: " + encrypted); String decrypted = decrypt(encrypted, secretKey); System.out.println("解密结果: " + decrypted); } } ``` 关键实现细节说明: 1. 采用`AES/CBC/PKCS5Padding`模式,明确指定加密模式和填充方案[^5] 2. 使用SHA-256生成256位密钥,避免直接使用短密码带来的安全隐患 3. 每次加密生成随机IV(初始化向量),并与密文合并存储,提升安全性 4. 使用Base64编码处理二进制数据,确保跨平台兼容性[^4] 5. 强制使用UTF-8字符编码,避免编解码不一致问题 注意事项: 1. 密钥管理:建议使用密钥管理系统(KMS)存储密钥,避免硬编码[^5] 2. IV处理:虽然IV不需要保密,但必须保证每次加密都使用不同的随机IV[^5] 3. 异常处理:生产环境需要更完善的异常处理机制 4. 性能优化:可缓存SecretKeySpec对象提升重复加密效率
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值