Package cn.gmkit.zuc

Class ZUC

java.lang.Object
cn.gmkit.zuc.ZUC

public final class ZUC extends Object
ZUC-128 stream cipher utilities.

The API accepts 16-byte keys and 16-byte IVs. Encryption and decryption are the same XOR operation against the generated key stream.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    ZUC-128 IV length in bytes.
    static final int
    ZUC-128 key length in bytes.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    decrypt(byte[] key, byte[] iv, byte[] ciphertext)
    Decrypt bytes with ZUC-128.
    static String
    decryptBase64ToUtf8(String keyHex, String ivHex, String ciphertextBase64)
    Decrypt Base64 ciphertext and decode the plaintext as UTF-8.
    static String
    decryptHexToUtf8(String keyHex, String ivHex, String ciphertextHex)
    Decrypt hexadecimal ciphertext and decode the plaintext as UTF-8.
    static String
    eea3(String keyHex, int count, int bearer, int direction, int bitLength)
    Generate EEA3 key stream as hexadecimal bytes.
    static byte[]
    eea3Encrypt(String keyHex, int count, int bearer, int direction, byte[] message)
    按完整消息字节长度执行 3GPP EEA3 加密。
    static byte[]
    eea3Encrypt(String keyHex, int count, int bearer, int direction, byte[] message, int bitLength)
    按 3GPP EEA3 规范加密消息;旧 eea3(java.lang.String, int, int, int, int) 继续返回字对齐密钥流以保持兼容。
    static String
    eia3(String keyHex, int count, int bearer, int direction, byte[] message)
    Generate the current project EIA3-compatible MAC value.
    static String
    eia3(String keyHex, int count, int bearer, int direction, byte[] message, int bitLength)
    Generate the standard EIA3 MAC value for a bit-length message prefix.
    static String
    eia3(String keyHex, int count, int bearer, int direction, String message)
    Generate the current project EIA3-compatible MAC value for UTF-8 text.
    static byte[]
    encrypt(byte[] key, byte[] iv, byte[] plaintext)
    Encrypt bytes with ZUC-128.
    static String
    encryptBase64(String keyHex, String ivHex, String plaintext)
    Encrypt a UTF-8 string and return Base64 ciphertext.
    static String
    encryptHex(String keyHex, String ivHex, String plaintext)
    Encrypt a UTF-8 string and return hexadecimal ciphertext.
    static byte[]
    keystream(byte[] key, byte[] iv, int lengthBytes)
    Generate ZUC-128 key stream bytes.
    static String
    keystreamHex(String keyHex, String ivHex, int lengthBytes)
    Generate ZUC-128 key stream bytes as hexadecimal.
    static int[]
    keystreamWords(byte[] key, byte[] iv, int lengthWords)
    Generate ZUC-128 key stream words.
    static String
    keystreamWordsHex(String keyHex, String ivHex, int lengthWords)
    Generate ZUC-128 key stream words as hexadecimal bytes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • KEY_LENGTH

      public static final int KEY_LENGTH
      ZUC-128 key length in bytes.
      See Also:
    • IV_LENGTH

      public static final int IV_LENGTH
      ZUC-128 IV length in bytes.
      See Also:
  • Method Details

    • keystream

      public static byte[] keystream(byte[] key, byte[] iv, int lengthBytes)
      Generate ZUC-128 key stream bytes.
      Parameters:
      key - 16-byte key
      iv - 16-byte IV
      lengthBytes - requested byte length, may be 0
      Returns:
      key stream bytes
    • keystreamHex

      public static String keystreamHex(String keyHex, String ivHex, int lengthBytes)
      Generate ZUC-128 key stream bytes as hexadecimal.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      lengthBytes - requested byte length, may be 0
      Returns:
      hexadecimal key stream
    • keystreamWords

      public static int[] keystreamWords(byte[] key, byte[] iv, int lengthWords)
      Generate ZUC-128 key stream words.
      Parameters:
      key - 16-byte key
      iv - 16-byte IV
      lengthWords - requested 32-bit word count, may be 0
      Returns:
      unsigned 32-bit words stored in Java ints
    • keystreamWordsHex

      public static String keystreamWordsHex(String keyHex, String ivHex, int lengthWords)
      Generate ZUC-128 key stream words as hexadecimal bytes.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      lengthWords - requested 32-bit word count, may be 0
      Returns:
      hexadecimal key stream
    • encrypt

      public static byte[] encrypt(byte[] key, byte[] iv, byte[] plaintext)
      Encrypt bytes with ZUC-128.
      Parameters:
      key - 16-byte key
      iv - 16-byte IV
      plaintext - plaintext bytes, may be empty
      Returns:
      ciphertext bytes
    • decrypt

      public static byte[] decrypt(byte[] key, byte[] iv, byte[] ciphertext)
      Decrypt bytes with ZUC-128.
      Parameters:
      key - 16-byte key
      iv - 16-byte IV
      ciphertext - ciphertext bytes, may be empty
      Returns:
      plaintext bytes
    • encryptHex

      public static String encryptHex(String keyHex, String ivHex, String plaintext)
      Encrypt a UTF-8 string and return hexadecimal ciphertext.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      plaintext - UTF-8 text
      Returns:
      hexadecimal ciphertext
    • encryptBase64

      public static String encryptBase64(String keyHex, String ivHex, String plaintext)
      Encrypt a UTF-8 string and return Base64 ciphertext.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      plaintext - UTF-8 text
      Returns:
      Base64 ciphertext
    • decryptHexToUtf8

      public static String decryptHexToUtf8(String keyHex, String ivHex, String ciphertextHex)
      Decrypt hexadecimal ciphertext and decode the plaintext as UTF-8.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      ciphertextHex - hexadecimal ciphertext
      Returns:
      plaintext text
    • decryptBase64ToUtf8

      public static String decryptBase64ToUtf8(String keyHex, String ivHex, String ciphertextBase64)
      Decrypt Base64 ciphertext and decode the plaintext as UTF-8.
      Parameters:
      keyHex - 32-hex-character key
      ivHex - 32-hex-character IV
      ciphertextBase64 - Base64 ciphertext
      Returns:
      plaintext text
    • eea3

      public static String eea3(String keyHex, int count, int bearer, int direction, int bitLength)
      Generate EEA3 key stream as hexadecimal bytes.

      The return value is word-aligned, matching the current TypeScript API.

      Parameters:
      keyHex - 32-hex-character key
      count - 32-bit counter
      bearer - 5-bit bearer identifier, 0 to 31
      direction - direction bit, 0 or 1
      bitLength - requested bit length, may be 0
      Returns:
      word-aligned hexadecimal key stream
    • eea3Encrypt

      public static byte[] eea3Encrypt(String keyHex, int count, int bearer, int direction, byte[] message, int bitLength)
      按 3GPP EEA3 规范加密消息;旧 eea3(java.lang.String, int, int, int, int) 继续返回字对齐密钥流以保持兼容。
      Parameters:
      keyHex - 32 位十六进制字符密钥
      count - 32 位计数器
      bearer - 5 位承载标识
      direction - 方向位,0 或 1
      message - 待加密消息
      bitLength - 有效消息比特数
      Returns:
      密文字节,末字节未使用的低位清零
    • eea3Encrypt

      public static byte[] eea3Encrypt(String keyHex, int count, int bearer, int direction, byte[] message)
      按完整消息字节长度执行 3GPP EEA3 加密。
      Parameters:
      keyHex - 32 个十六进制字符的 128 位密钥
      count - 32 位计数器
      bearer - 5 位承载标识,范围 0 至 31
      direction - 方向位,只能为 0 或 1
      message - 消息字节;按全部字节计算有效位数
      Returns:
      与输入等长的密文字节
      Throws:
      GmkitException - key、参数或消息为空/越界时抛出
    • eia3

      public static String eia3(String keyHex, int count, int bearer, int direction, byte[] message)
      Generate the current project EIA3-compatible MAC value.
      Parameters:
      keyHex - 32-hex-character key
      count - 32-bit counter
      bearer - 5-bit bearer identifier, 0 to 31
      direction - direction bit, 0 or 1
      message - message bytes
      Returns:
      8-character hexadecimal MAC
    • eia3

      public static String eia3(String keyHex, int count, int bearer, int direction, byte[] message, int bitLength)
      Generate the standard EIA3 MAC value for a bit-length message prefix.
      Parameters:
      keyHex - 32-hex-character key
      count - 32-bit counter
      bearer - 5-bit bearer identifier, 0 to 31
      direction - direction bit, 0 or 1
      message - message bytes
      bitLength - authenticated bit length, may stop inside the final byte
      Returns:
      8-character hexadecimal MAC
      Throws:
      GmkitException - key、bearer、direction、message 或 bitLength 不合法时抛出
    • eia3

      public static String eia3(String keyHex, int count, int bearer, int direction, String message)
      Generate the current project EIA3-compatible MAC value for UTF-8 text.
      Parameters:
      keyHex - 32-hex-character key
      count - 32-bit counter
      bearer - 5-bit bearer identifier, 0 to 31
      direction - direction bit, 0 or 1
      message - UTF-8 message text
      Returns:
      8-character hexadecimal MAC