본문 바로가기

bouncy castle

[bouncy castle] SEED CBC Example

Seed CBC Exmaple

 

이 예제는 Bouncy castle을 이용하여  SEED CBC모드로 암복호화 하는 예제이다.

 

Block Size: 128 bit (16 byte)

Key Size: 128 bit (16 byte)

 

패딩이 필요없는 키와 블럭 크기가 맞는 경우의 예제이다.

public byte[] seedCbcEncrypt(byte[] key, byte[] text, byte[] iv) throws InvalidCipherTextException {
    if (iv == null) {
        iv = new byte[16];
    }

    //create Seed Cipher
    BlockCipher engine = new SEEDEngine();
    BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(engine));

    // cipher encryption init with key
    cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));

    byte[] outBuff = new byte[cipher.getOutputSize(text.length)];
    int offset = cipher.processBytes(text, 0, text.length, outBuff, 0);
    cipher.doFinal(outBuff, offset);

    return outBuff;
}

public byte[] seedCbcDecrypt(byte[] key, byte[] text, byte[] iv) throws InvalidCipherTextException {
    if (iv == null) {
        iv = new byte[16];
    }

    //create Seed Cipher
    BlockCipher engine = new SEEDEngine();
    BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(engine));

    //cipher decryption init with key
    cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));

    byte[] outBuff = new byte[cipher.getOutputSize(text.length)];
    int offset = cipher.processBytes(text, 0, text.length, outBuff, 0);
    cipher.doFinal(outBuff, offset);

    return outBuff;
}

@Test
public void seed_cbc_encrypt_test_001() throws InvalidCipherTextException {
    byte[] txt = { 
        (byte)0x12, (byte)0x34, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x80, 
        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 
    };
    byte[] anw = { 
        (byte)0x6c, (byte)0x71, (byte)0xe6, (byte)0x0d, (byte)0xef, (byte)0x88, (byte)0x4c, (byte)0x34, 
        (byte)0xc8, (byte)0x10, (byte)0x90, (byte)0x42, (byte)0x97, (byte)0xb4, (byte)0x4f, (byte)0x3c 
    };
    byte[] key = { 
        (byte)0x40, (byte)0x41, (byte)0x42, (byte)0x43, (byte)0x44, (byte)0x45, (byte)0x46, (byte)0x47, 
        (byte)0x48, (byte)0x49, (byte)0x4a, (byte)0x4b, (byte)0x4c, (byte)0x4d, (byte)0x4e, (byte)0x4f 
    };

    Seed seed = new Seed();
    byte[] ret = seed.seedCbcEncrypt(key, txt);

    assertEquals("6C71E60DEF884C34C810904297B44F3C", toHex(ret));
}


@Test
public void seed_cbc_decrypt_test_001() throws InvalidCipherTextException {
    byte[] txt = { 
        (byte)0x12, (byte)0x34, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x80, 
        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 
    };
    byte[] anw = { 
        (byte)0x6c, (byte)0x71, (byte)0xe6, (byte)0x0d, (byte)0xef, (byte)0x88, (byte)0x4c, (byte)0x34, 
        (byte)0xc8, (byte)0x10, (byte)0x90, (byte)0x42, (byte)0x97, (byte)0xb4, (byte)0x4f, (byte)0x3c 
    };
    byte[] key = { 
        (byte)0x40, (byte)0x41, (byte)0x42, (byte)0x43, (byte)0x44, (byte)0x45, (byte)0x46, (byte)0x47, 
        (byte)0x48, (byte)0x49, (byte)0x4a, (byte)0x4b, (byte)0x4c, (byte)0x4d, (byte)0x4e, (byte)0x4f 
    };

    Seed seed = new Seed();
    byte[] ret = seed.seedCbcDecrypt(key, anw);

    assertEquals("12340000010000800000000000000000", toHex(ret));
}

 

 

코드위치

https://github.com/coolbong/BouncyCastleExample/blob/master/src/main/java/io/github/coolbong/Seed.java