Package cn.gmkit.zuc
Class ZUC
java.lang.Object
cn.gmkit.zuc.ZUC
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
FieldsModifier and TypeFieldDescriptionstatic final intZUC-128 IV length in bytes.static final intZUC-128 key length in bytes. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decrypt(byte[] key, byte[] iv, byte[] ciphertext) Decrypt bytes with ZUC-128.static StringdecryptBase64ToUtf8(String keyHex, String ivHex, String ciphertextBase64) Decrypt Base64 ciphertext and decode the plaintext as UTF-8.static StringdecryptHexToUtf8(String keyHex, String ivHex, String ciphertextHex) Decrypt hexadecimal ciphertext and decode the plaintext as UTF-8.static StringGenerate 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 StringGenerate the current project EIA3-compatible MAC value.static StringGenerate the standard EIA3 MAC value for a bit-length message prefix.static StringGenerate 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 StringencryptBase64(String keyHex, String ivHex, String plaintext) Encrypt a UTF-8 string and return Base64 ciphertext.static StringencryptHex(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 StringkeystreamHex(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 StringkeystreamWordsHex(String keyHex, String ivHex, int lengthWords) Generate ZUC-128 key stream words as hexadecimal bytes.
-
Field Details
-
KEY_LENGTH
public static final int KEY_LENGTHZUC-128 key length in bytes.- See Also:
-
IV_LENGTH
public static final int IV_LENGTHZUC-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 keyiv- 16-byte IVlengthBytes- requested byte length, may be 0- Returns:
- key stream bytes
-
keystreamHex
Generate ZUC-128 key stream bytes as hexadecimal.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVlengthBytes- 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 keyiv- 16-byte IVlengthWords- requested 32-bit word count, may be 0- Returns:
- unsigned 32-bit words stored in Java
ints
-
keystreamWordsHex
Generate ZUC-128 key stream words as hexadecimal bytes.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVlengthWords- 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 keyiv- 16-byte IVplaintext- 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 keyiv- 16-byte IVciphertext- ciphertext bytes, may be empty- Returns:
- plaintext bytes
-
encryptHex
Encrypt a UTF-8 string and return hexadecimal ciphertext.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVplaintext- UTF-8 text- Returns:
- hexadecimal ciphertext
-
encryptBase64
Encrypt a UTF-8 string and return Base64 ciphertext.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVplaintext- UTF-8 text- Returns:
- Base64 ciphertext
-
decryptHexToUtf8
Decrypt hexadecimal ciphertext and decode the plaintext as UTF-8.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVciphertextHex- hexadecimal ciphertext- Returns:
- plaintext text
-
decryptBase64ToUtf8
Decrypt Base64 ciphertext and decode the plaintext as UTF-8.- Parameters:
keyHex- 32-hex-character keyivHex- 32-hex-character IVciphertextBase64- Base64 ciphertext- Returns:
- plaintext text
-
eea3
Generate EEA3 key stream as hexadecimal bytes.The return value is word-aligned, matching the current TypeScript API.
- Parameters:
keyHex- 32-hex-character keycount- 32-bit counterbearer- 5-bit bearer identifier, 0 to 31direction- direction bit, 0 or 1bitLength- 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 或 1message- 待加密消息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 至 31direction- 方向位,只能为 0 或 1message- 消息字节;按全部字节计算有效位数- Returns:
- 与输入等长的密文字节
- Throws:
GmkitException- key、参数或消息为空/越界时抛出
-
eia3
Generate the current project EIA3-compatible MAC value.- Parameters:
keyHex- 32-hex-character keycount- 32-bit counterbearer- 5-bit bearer identifier, 0 to 31direction- direction bit, 0 or 1message- 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 keycount- 32-bit counterbearer- 5-bit bearer identifier, 0 to 31direction- direction bit, 0 or 1message- message bytesbitLength- authenticated bit length, may stop inside the final byte- Returns:
- 8-character hexadecimal MAC
- Throws:
GmkitException- key、bearer、direction、message 或 bitLength 不合法时抛出
-
eia3
Generate the current project EIA3-compatible MAC value for UTF-8 text.- Parameters:
keyHex- 32-hex-character keycount- 32-bit counterbearer- 5-bit bearer identifier, 0 to 31direction- direction bit, 0 or 1message- UTF-8 message text- Returns:
- 8-character hexadecimal MAC
-