java源码-Byte

Byte有一个cache数组,存了-128--127

public static Byte valueOf(byte b)
这个方法直接返回chache里的对象,这样可以提高效率,节省空间


private static class ByteCache {
    private ByteCache(){}

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

AES加密是一种常用的对称加密算法,Java中也提供了AES加密的实现。以下是一个简单的AES加密Java源码示例: ``` import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class AESUtil { // 生成AES秘钥 public static byte[] initKey() throws Exception { KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128); // 生成128位的AES秘钥 SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } // AES加密 public static byte[] encrypt(byte[] data, byte[] key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return cipher.doFinal(data); } // AES解密 public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return cipher.doFinal(encryptedData); } } ``` 这段代码实现了AES秘钥的生成、加密和解密功能。其中,`initKey()`方法用于生成128位的AES秘钥,`encrypt()`方法用于对数据进行AES加密,`decrypt()`方法用于对加密后的数据进行解密。在使用时,需要先调用`initKey()`方法生成秘钥,然后使用`encrypt()`方法进行加密,再使用`decrypt()`方法进行解密。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值