JS AES256 OFB 模式 16进制

直接上代码

AES256 加密

/*
    功能: 加密

    参数:
    data: 16进制字符串
    iv: 16进制初始向量, 一般为8字节  
    key: 16进制字符串, AES256 对应 32 字节  
*/
const encrypt = (data, iv, key) => {
    //将16字符串,转为js word
    let hexData = CryptoJS.enc.Hex.parse(data);
    let hexKey = CryptoJS.enc.Hex.parse(key);
    let hexIv = CryptoJS.enc.Hex.parse(iv);

    let encrypted = CryptoJS.AES.encrypt(hexData, hexKey, {
        iv: hexIv,
        mode: CryptoJS.mode.OFB,
        padding: CryptoJS.pad.NoPadding,
      });

    
    //返回16进制字符串
    return encrypted.ciphertext.toString().toUpperCase();
}

/*
    功能: 解密
    data: 需要解密 16进制字符串
    iv: 16进制初始向量, 一般为8字节  
    key: 16进制字符串, AES256 对应 32 字节  
*/
const decrypt = (data, iv, key) => {
     //将16字符串,转为js word
    let hexData = CryptoJS.enc.Hex.parse(data);
    let hexKey = CryptoJS.enc.Hex.parse(key);
    let hexIv = CryptoJS.enc.Hex.parse(iv);

    let decrypted = CryptoJS.AES.decrypt(
    {
        ciphertext: hexData,
    }
    , hexKey, {
        iv: hexIv,
        mode: CryptoJS.mode.OFB,
        padding: CryptoJS.pad.NoPadding,
      });

    
    //返回16进制字符串
    return decrypted.toString();
}

/**
 *         ┏┓   ┏┓+ +
 *        ┏┛┻━━━┛┻┓ + +
 *        ┃       ┃
 *        ┃   ━   ┃ ++ + + +
 *        ████━████ ┃+
 *        ┃       ┃ +
 *        ┃   ┻   ┃
 *        ┃       ┃ + +
 *        ┗━┓   ┏━┛
 *          ┃   ┃
 *          ┃   ┃ + + + +
 *          ┃   ┃    Code is far away from bug with the animal protecting
 *          ┃   ┃ +     神兽保佑,代码无bug
 *          ┃   ┃
 *          ┃   ┃  +
 *          ┃    ┗━━━┓ + +
 *          ┃        ┣┓
 *          ┃        ┏┛
 *          ┗┓┓┏━┳┓┏┛ + + + +
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛+ + + +
 *
 * @author chenxi
 * @date 2022年3月14日15:19:19
 */
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是AES 256 OFB模式的Java实现示例: ```java import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AESOFBExample { public static void main(String[] args) throws Exception { String plainText = "This is a secret message"; System.out.println("Plain Text: " + plainText); // Generate a 256-bit AES key KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); SecretKey secretKey = keyGen.generateKey(); byte[] keyBytes = secretKey.getEncoded(); // Generate a random initialization vector SecureRandom random = new SecureRandom(); byte[] ivBytes = new byte[16]; random.nextBytes(ivBytes); // Create the Cipher object and configure it for OFB mode with AES encryption Cipher cipher = Cipher.getInstance("AES/OFB/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // Encrypt the plain text byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); System.out.println("Encrypted Text: " + new String(encryptedBytes)); // Decrypt the encrypted text cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); System.out.println("Decrypted Text: " + new String(decryptedBytes)); } } ``` 上述代码中,我们首先使用Java的`KeyGenerator`类生成一个256位的AES密钥。然后,我们使用`SecureRandom`类生成一个随机的16字节的初始化向量。 接下来,我们创建一个`Cipher`对象,并将其配置为使用AES加密的OFB模式。然后,我们使用生成的密钥和初始化向量初始化`Cipher`对象,并使用`doFinal`方法对明文进行加密。 最后,我们再次使用相同的密钥和初始化向量初始化`Cipher`对象,并使用`doFinal`方法对密文进行解密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值