bouncy castle (18) 썸네일형 리스트형 [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 Buffer.. [bouncy castle] File Hash(SHA256) 값 구하기 File Hash Example 릴리즈한 결과물의 변경이 었는지 확인 할수 있게 Hash 값을 같이 제공한다. public byte[] sha256(File file) { Digest digest = new SHA256Digest(); int hashSize = digest.getDigestSize(); byte[] outBuff = new byte[hashSize]; byte[] buf = new byte[4*1024]; // 4KB FileInputStream fis = null; try { fis = new FileInputStream(file); int len; while ((len = fis.read(buf)) > 0) { digest.update(buf, 0, len); } } catch (I.. [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).. [bouncy castle] HMAC-MD5, HMAC-SHA1 example HMAC MD5 Example public byte[] hmacMd5(byte[] key, byte[] text) { HMac mac = new HMac(new MD5Digest()); mac.init(new KeyParameter(key)); int macSize = mac.getMacSize(); byte[] output = new byte[macSize]; mac.update(text, 0, text.length); mac.doFinal(output, 0); return output; } public void hmac_md5_rfc_2202_test_001_01() { //test_case = 1 //key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b //key_len = 16.. [bouncy castle] AES CMAC 128 example AES CMAC Example AES CMAC 128에서 128의 의미는 CMAC Block Size를 bit로 표기한 것으로 16 byte key 길이를 의미한다. public byte[] aesCmac128(byte[] key, byte[] text) { //Create CMAC / AES / 128 bit CMac mac = new CMac(new AESEngine(), 128); mac.init(new KeyParameter(key)); byte[] output = new byte[mac.getMacSize()]; mac.update(text, 0, text.length); mac.doFinal(output, 0); return output; } public void aes_cmac_test_00.. [bouncy castle] Random generator Random generator example public byte[] rand(int length) { // create random generator (sha1 digest) RandomGenerator generator = new DigestRandomGenerator(new SHA1Digest()); // add seed generator.addSeedMaterial(System.nanoTime()); byte[] buf = new byte[length]; // generate generator.nextBytes(buf); return buf; } public void random_test_001() { Random rnd = new Random(); byte[] random1 = rnd.rand(.. [bouncy castle] hash SHA1, SHA256, SHA512 SHA1 hash example public byte[] sha1(byte[] text) { // create sha1 digest Digest digest = new SHA1Digest(); //get block size for output buffer int blockSize = digest.getDigestSize(); byte[] buf = new byte[blockSize]; digest.update(text, 0, text.length); digest.doFinal(buf, 0); return buf; } public void hash_sha1_test_001() { byte[] text = "Hello world".getBytes(StandardCharsets.UTF_8); Hash hash.. [bouncy castle] hash md5 md5 hash example public byte[] md5(byte[] text) { // create MD5 digest Digest digest = new MD5Digest(); // get block size for output buffer int blockSize = digest.getDigestSize(); byte[] buf = new byte[blockSize]; digest.update(text, 0, text.length); digest.doFinal(buf, 0); return buf; } public void hash_md5_test_001() { byte[] text = "Hello world".getBytes(StandardCharsets.UTF_8); Hash hash = n.. [bouncy castle] AES CTR (128, 192, 256) with padding example AES ECB Example 128, 192, 256의 의미는 키 길이의 bit수를 의미한다. 128 bit은 16 byte key 길이, 192 bit은 24 byte key 길이, 192 bit은 32 byte key 길이를 의미한다. 32 byte block을 암호화 / 복호화하는 예제 static byte[] aes_128bit_16byte = "ABCDEFGHIJKLMNOP".getBytes(StandardCharsets.UTF_8); static byte[] aes_192bit_24byte = "ABCDEFGHIJKLMNOPQRSTUVWX".getBytes(StandardCharsets.UTF_8); static byte[] aes_256bit_32byte = "ABCDEFGHIJKLMNOPQ.. [bouncy castle] AES CBC (128, 192, 256) with padding example AES CBC with padding Example 128, 192, 256의 의미는 키 길이의 bit수를 의미한다. 128 bit은 16 byte key 길이, 192 bit은 24 byte key 길이, 192 bit은 32 byte key 길이를 의미한다. block size에 맞게 padding하여 암호화 / 복호화하는 예제 참고사항) Bouncy castle에서 Padding 방식을 지정하지 않을 경우 default padding 방식은 PKCS7 방식을 사용한다. static byte[] aes_128bit_16byte = "ABCDEFGHIJKLMNOP".getBytes(StandardCharsets.UTF_8); static byte[] aes_192bit_24byte = "ABCDEFGH.. 이전 1 2 다음