com.supermap.data

类 SM4Cipher

  • java.lang.Object
    • com.supermap.data.InternalHandle
      • com.supermap.data.InternalHandleDisposable
        • com.supermap.data.SM4Cipher
  • 所有已实现的接口:
    IDisposable


    public class SM4Cipher
    extends com.supermap.data.InternalHandleDisposable
    SM4(国密4)加解密工具, 目前支持CTR和CBC模式
    从以下版本开始:
    11.2.0
    示范代码:
    示范如何使用SM4(国密4)加解密工具
    // 密钥key 长度必须是 16 byte即128 bit
    String key = "12345678abcdefgh";
    // 初始向量iv 长度必须是 16 byte即128 bit
    String iv = "hgfedcba87654321";
    
    String msg = "Hello the world!";
    SM4Cipher cipher = SM4Cipher.getInstance("CTR", key.getBytes(), iv.getBytes());
    
    byte[] encrypted = cipher.encrypt(msg.getBytes());
    
    byte[] result = cipher.decrypt(encrypted);
    
    String resultString = new String(result);
    
    • 构造器详细资料

      • SM4Cipher

        public SM4Cipher()
    • 方法详细资料

      • encryptGCM

        public SM4Cipher.GCMEncryptResult encryptGCM(byte[] input,
                                                     byte[] aad,
                                                     int tagLength)
        GCM加密操作
        参数:
        input - 输入的缓冲区
        aad - 附加消息(AAD,Additional Authenticated Data)附加消息是可选的输入,用于提供额外的上下文信息,最终被包含在消息认证码(MAC)计算中。如果不需要,可以设置为null。
        tagLength - 需要传出的认证标签长度。不能超过16字节。如果不需要,可以设置为0。
        返回:
        加密结果对象,包含加密结果内容和认证标签
        抛出:
        IllegalStateException - 对象已经被dispose
        IllegalArgumentException - 输入的缓冲区为空
        IllegalArgumentException - 输入的缓冲区长度必须为16的倍数
        IllegalArgumentException - 输入的认证标签长度小于0或者大于16
        从以下版本开始:
        11.2.0
        示范代码:
        示范如何使用SM4(国密4)加解密工具GCM模式加解密
        // 密钥key 长度必须是 16 byte即128 bit
        String key = "12345678abcdefgh";
        // 初始向量iv 长度必须是 16 byte即128 bit
        String iv = "hgfedcba87654321";
        
        // aad-附加消息长度和内容任意,这个内容不会被加密,但是会用于计算认证标签,所以需要加解密输入相同内容
        String aad = "000111222333444555666777";
        
        // 输入加解密的数据长度必须是16 byte的整数倍
        String msg = "Hello the world!";
        SM4Cipher cipher = SM4Cipher.getInstance("GCM", key.getBytes(), iv.getBytes());
        
        int tagLength = 16;
        SM4Cipher.GCMEncryptResult gcmEncryptResult = cipher.encryptGCM(msg.getBytes(), aad.getBytes(), tagLength);
        
        byte[] encrypted = gcmEncryptResult.getResult();
        // 还需要从加密结果中获取到认证标签,传入解密函数中才能正确解密
        byte[] tag = gcmEncryptResult.getTag();
        
        byte[] result = cipher.decryptGCM(encrypted, aad.getBytes(), tag);
        
        String resultString = new String(result);
        
      • decryptGCM

        public byte[] decryptGCM(byte[] input,
                                 byte[] aad,
                                 byte[] tag)
        GCM解密操作
        参数:
        input - 输入的缓冲区
        aad - 附加消息(AAD,Additional Authenticated Data)附加消息是可选的输入,用于提供额外的上下文信息,最终被包含在消息认证码(MAC)计算中。如果不需要,可以设置为null。
        tag - GCM模式加密过程计算出的认证标签。用于验证加密数据的完整性和认证数据的真实性。如果不需要,可以设置为null。
        返回:
        加密结果对象,包含加密结果内容和认证标签 查看示例 SM4Cipher.encryptGCM(byte[], byte[], int)
        抛出:
        IllegalStateException - 对象已经被dispose
        IllegalArgumentException - 输入的缓冲区为空
        IllegalArgumentException - 输入的缓冲区长度必须为16的倍数
        IllegalArgumentException - 输入的认证标签长度必须小于16
        从以下版本开始:
        11.2.0
      • dispose

        public void dispose()
        释放资源

Copyright © 2021–2024 SuperMap. All rights reserved.