본문 바로가기

bouncy castle

[bouncy castle] SEED ECB Example

SEED ECB Example

 

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

 

Block Size: 128 bit (16 byte)

Key Size: 128 bit (16 byte)

 

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

public byte[] seedEcbEncrypt(byte[] key, byte[] text) throws InvalidCipherTextException {
    //create Seed Cipher
    BlockCipher engine = new SEEDEngine();
    BufferedBlockCipher cipher = new BufferedBlockCipher(engine);

    //init for encrypt(set key)
    cipher.init(true, new KeyParameter(key));


    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[] seedEcbDecrypt(byte[] key, byte[] text) throws InvalidCipherTextException {
    //create Seed Cipher
    BlockCipher engine = new SEEDEngine();
    BufferedBlockCipher cipher = new BufferedBlockCipher(engine);

    //init for decrypt (set key)
    cipher.init(false, new KeyParameter(key));

    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_ecb_encrypt_test_001() throws InvalidCipherTextException {
    byte[] txt = {
        (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07,
        (byte)0x08, (byte)0x09, (byte)0x0a, (byte)0x0b, (byte)0x0c, (byte)0x0d, (byte)0x0e, (byte)0x0f
    };
    byte[] key = {
        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
    };

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

    assertEquals("5EBAC6E0054E166819AFF1CC6D346CDB", toHex(ret));
}

@Test
public void seed_ecb_decrypt_test_001() throws InvalidCipherTextException {
    byte[] key = {
            (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
            (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
    };

    byte[] anw = {
            (byte)0x5e, (byte)0xba, (byte)0xc6, (byte)0xe0, (byte)0x05, (byte)0x4e, (byte)0x16, (byte)0x68,
            (byte)0x19, (byte)0xaf, (byte)0xf1, (byte)0xcc, (byte)0x6d, (byte)0x34, (byte)0x6c, (byte)0xdb
    };

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

    assertEquals("000102030405060708090A0B0C0D0E0F", toHex(ret));
}

 

코드위치

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