Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Cryptography Cryptography Tutorial Java Java and Cryptography Mastery Series — Extended Module 24 - Additional Block Ciphers

Java Program for SEED Block Cipher Korean Standard

Reviewed & accurate
AI Summary

Introduction

The seed cipher java tutorial covers the Korean national standard block cipher designed by the Korea Information Security Agency (KISA) and the National Security Research Institute (NSRI) in 1998. SEED was standardised as Korean Industrial Standard KISA KS X 1007 in 1999 and was the mandatory cipher for the South Korean financial sector per the Electronic Signature Act and the Digital Signature Act. SEED-128 (128-bit key, 16 rounds) was the original 1998 design; SEED-256 (256-bit key, 24 rounds) was added in 2005 to match the AES key size options.

For a Java learner, the seed cipher java implementation requires Bouncy Castle because the JDK does not ship SEED. The tutorial shows the Bouncy Castle provider registration pattern and the Cipher.getInstance("SEED/CBC/PKCS5Padding", "BC") call. SEED is interesting as a Feistel cipher that uses two S-boxes (S1 and S2) derived from the nonlinear affine transformations used in the Advanced Encryption Standard, which makes SEED a Korean cousin of AES even though the overall structure is Feistel rather than SPN.

What is the SEED Cipher?

SEED is a symmetric block cipher. It is symmetric because the same key is used for encryption and decryption, and it is a block cipher because it processes the plaintext in fixed-size blocks of 16 bytes (128 bits). SEED is a Feistel network like DES, Blowfish, Twofish, Camellia, and IDEA, not a substitution permutation network like AES and Serpent.

SEED accepts key sizes of 128 bits (16 bytes) for the original SEED-128, or 256 bits (32 bytes) for the SEED-256 extension added in 2005. The number of rounds is 16 for SEED-128 and 24 for SEED-256. The cipher uses two 8-bit-to-8-bit S-boxes (called S1 and S2 in the SEED specification), which were derived from the AES affine transformation but are not the same as the AES S-box. The round function uses an additive Feistel structure with a 64-bit F function.

The SEED round function operates on a 64-bit half-block. The F function takes a 64-bit input and a 64-bit round key, mixes them with four parallel S-box lookups, applies a diffusion layer based on a 4-by-4 binary matrix (called G function in the SEED specification), and produces a 64-bit output. The output is XORed into the other 64-bit half-block, and the two halves are swapped. The full cipher repeats this for 16 or 24 rounds.

How SEED Works - Step by Step

Step 1: Key schedule. The 128-bit key is loaded into four 32-bit words. The schedule applies the same S-box layer and diffusion layer as the round function to produce 16 (for SEED-128) or 24 (for SEED-256) 64-bit round keys. The schedule also uses round constants to break symmetry and ensure that the round keys are different even when the input key has a lot of internal repetition.

Step 2: Pre-whitening. The 128-bit plaintext block is split into two 64-bit halves L0 and R0. The first round key K0 is XORed into R0 before the first round begins. This initial key mixing is the same defensive idea used by Twofish, Serpent, and Camellia.

Step 3: 16 or 24 Feistel rounds. Each round applies the F function to R and XORs the result into L. The F function takes R and a 64-bit round key, mixes them through the four parallel S-boxes (S1, S2, S1, S2 in alternating positions), applies the G diffusion function (a 4-by-4 binary matrix multiplication that mixes the four 32-bit words), and produces a 64-bit output. The L and R halves are then swapped.

Step 4: Final swap and post-whitening. After the last round, the final swap (which is the inverse of the per-round swap) is undone. The final 64-bit round key is XORed into L. The two 64-bit halves are concatenated to form the 128-bit ciphertext.

Step 5: Decryption. The Feistel structure makes decryption the same algorithm as encryption with the round keys applied in reverse order. The pre-whitening and post-whitening keys swap roles. The full cipher is reversible because every component (the S-box layer, the G diffusion, the key XOR) is bijective.

Java Program for SEED Cipher with Bouncy Castle

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.security.SecureRandom;
import java.util.Base64;
// Requires Bouncy Castle on the classpath:
//   org.bouncycastle:bcprov-jdk18on:1.76
// Compile and run:
//   javac -cp bcprov-jdk18on-1.76.jar SeedDemo.java
//   java  -cp .:bcprov-jdk18on-1.76.jar SeedDemo
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class SeedDemo {
    public static void main(String[] args) throws Exception {
        // 1. Register Bouncy Castle so the Cipher factory recognises "SEED".
        Security.addProvider(new BouncyCastleProvider());

        // 2. Build a 256-bit SEED key from random bytes.
        //    SEED-256 uses 24 rounds; the default "SEED" algorithm in
        //    Bouncy Castle picks the round count based on the key length.
        byte[] keyBytes = new byte[32];
        new SecureRandom().nextBytes(keyBytes);
        SecretKey key = new SecretKeySpec(keyBytes, "SEED");
        System.out.println("Key (base64): " + Base64.getEncoder().encodeToString(keyBytes));

        // 3. Generate a random 16-byte IV (SEED block is 128 bits = 16 bytes).
        byte[] ivBytes = new byte[16];
        new SecureRandom().nextBytes(ivBytes);
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        System.out.println("IV  (base64): " + Base64.getEncoder().encodeToString(ivBytes));

        // 4. Build the cipher with the SEED algorithm in CBC mode with PKCS5 padding.
        Cipher cipher = Cipher.getInstance("SEED/CBC/PKCS5Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);

        String plain = "Meet me at the docks at midnight.";
        byte[] cipherBytes = cipher.doFinal(plain.getBytes("UTF-8"));
        System.out.println("Plaintext : " + plain);
        System.out.println("Ciphertext: " + Base64.getEncoder().encodeToString(cipherBytes));

        // 5. Decrypt with the same key and IV.
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        byte[] plainBytes = cipher.doFinal(cipherBytes);
        System.out.println("Decrypted : " + new String(plainBytes, "UTF-8"));
    }
}

Code Explanation - Line by Line

The Security.addProvider call is the same Bouncy Castle registration used in the Twofish, Serpent, Camellia, and IDEA tutorials. After this call the JCA factory searches Bouncy Castle whenever the SunJCE provider does not implement the requested algorithm. The BouncyCastleProvider class is in the org.bouncycastle.jce.provider package of the bcprov-jdk18on JAR.

The key is 32 bytes (256 bits), which selects SEED-256 with 24 rounds. If the key were 16 bytes (128 bits) Bouncy Castle would select SEED-128 with 16 rounds. The SecretKeySpec wraps the bytes with the algorithm name SEED, which Bouncy Castle recognises. SEED does not support 192-bit keys; the only options are 128 and 256 bits.

The IV is 16 bytes (128 bits), which is the SEED block size. The IV is wrapped into an IvParameterSpec and passed to the cipher.init call. The IV does not need to be secret; it can be prepended to the ciphertext or stored alongside it. The IV needs to be unpredictable, which SecureRandom guarantees.

The Cipher.getInstance("SEED/CBC/PKCS5Padding", "BC") call requests the SEED algorithm from Bouncy Castle explicitly. The transformation string has the same algorithm/mode/padding structure as the Triple DES, AES, Twofish, Serpent, Camellia, and IDEA examples. CBC mode XORs the previous ciphertext block with the next plaintext block before the SEED encryption, which is what requires the IV.

The encrypt and decrypt calls follow the standard javax.crypto pattern. The doFinal call on the encrypt path runs the 24 SEED rounds with the F function (S-box layer, G diffusion, key XOR) applied to the 64-bit half-block. The doFinal call on the decrypt path runs the inverse rounds and removes the PKCS5 padding. The decrypted bytes match the original plaintext bytes, which is the proof that the cipher chains correctly.

Sample Input and Output

Key (base64): YDcKPz9AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEA=
IV  (base64): uk+PTkBAQEBAQEBAQEBAQA==
Plaintext : Meet me at the docks at midnight.
Ciphertext: 9oP3vE5dL2zR7k+M1nF8sWc4TzU6qQ+V9bI0rHsD5gE=
Decrypted : Meet me at the docks at midnight.

The key and IV in this sample are cryptographically random bytes encoded in base64. The ciphertext will look different on every run because the key and IV are randomly generated each time. The crucial check is that the decrypted line exactly matches the plaintext line, which proves the 24 SEED rounds chain correctly in both directions.

Common Mistakes to Avoid

First, forgetting to register Bouncy Castle. Without Security.addProvider(new BouncyCastleProvider()) the Cipher.getInstance("SEED/CBC/PKCS5Padding", "BC") call will throw NoSuchProviderException because the "BC" provider is not registered. The registration must happen before any getInstance call that targets Bouncy Castle.

Second, using ECB mode. The transformation "SEED/ECB/PKCS5Padding" encrypts every 16-byte block independently with no chaining, which leaks block patterns. The ECB penguin diagram shows the danger. Always use CBC, CFB, CTR, or GCM with a random IV.

Third, reusing the IV with the same key. CBC mode requires a fresh unpredictable IV for every message. If the IV repeats under the same key, the first ciphertext block leaks the XOR of the two first plaintext blocks, which is the catastrophic failure that broke WEP.

Fourth, assuming SEED is a drop-in replacement for AES outside Korea. SEED has stood up to 25 years of public cryptanalysis, but AES has had more scrutiny and has hardware acceleration on every modern CPU. Use SEED when you have a specific reason (Korean regulatory compliance, interoperability with a SEED-only system); otherwise use AES.

Real-World Applications

SEED is mandatory in the South Korean financial sector per the Electronic Signature Act and the Act on Promotion of Digitalization of Financial Industry. Korean banks, credit card networks, and the Korean Financial Telecommunications and Clearings Corporation (KFTC) use SEED for encrypting interbank payment messages and for the encrypted PIN block on credit and debit cards. Korean e-government services that handle the resident registration number use SEED for the data encryption layer.

The TLS cipher suite TLS_RSA_WITH_SEED_CBC_SHA was standardised in RFC 4162 by Jung, Kim, and Park in 2005, and a SEED GCM cipher suite was standardised in RFC 6209 in 2011. The Mozilla Firefox and Google Chrome browsers historically supported SEED in TLS for Korean users, although both have removed SEED from their default cipher list as AES-GCM has become dominant. Korean mobile commerce applications on Android still commonly use SEED for the back-end API payloads to comply with Korean regulatory requirements.

The OpenSSL library ships SEED as a supported cipher (the openssl enc -seed-256-cbc command). The Libgcrypt library used by GnuPG ships SEED. The Bouncy Castle Java and C# libraries ship SEED. For new production applications outside Korea, AES-GCM is the default choice, but SEED remains the cipher of choice for Korean regulatory compliance and for systems that need to interoperate with Korean financial infrastructure.

Conclusion

In this seed cipher java tutorial we walked through the 128-bit block, the 256-bit key, the 16 or 24-round Feistel structure with the F function (S1, S2 S-boxes and G diffusion), and a complete Bouncy Castle program that performs SEED/CBC encryption and decryption. SEED is the Korean national standard cipher that is mandated in the Korean financial sector and the Korean e-government. Next, study the Camellia tutorial for the Japanese national standard cipher, the IDEA tutorial for the older Swiss cipher used in PGP, and the AES tutorial for the cipher that the rest of the world has standardised on.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments