blowfish

r r_member at pathlink.com
Thu Mar 23 09:46:03 PST 2006


i use and it seems to work, not very well tested so. i hope i interpreted the
license correctly.

r

/*
The Bouncy Castle License
Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
(http://www.bouncycastle.org)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software 
and associated documentation files (the "Software"), to deal in the Software
without restriction, 
including without limitation the rights to use, copy, modify, merge, publish,
distribute, sub license, 
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER
DEALINGS IN THE SOFTWARE.
*/

import std.stdio;
import std.string;

/**
* A class that provides Blowfish key encryption operations,
* such as encoding data and generating keys.
* All the algorithms herein are from Applied Cryptography
* and implement a simplified cryptography interface.
*/
public class BlowfishEngine
{
private const int[]
KP = [
cast(int) 0x243F6A88, cast(int) 0x85A308D3, cast(int) 0x13198A2E, cast(int)
0x03707344,
cast(int) 0xA4093822, cast(int) 0x299F31D0, cast(int) 0x082EFA98, cast(int)
0xEC4E6C89,
cast(int) 0x452821E6, cast(int) 0x38D01377, cast(int) 0xBE5466CF, cast(int)
0x34E90C6C,
cast(int) 0xC0AC29B7, cast(int) 0xC97C50DD, cast(int) 0x3F84D5B5, cast(int)
0xB5470917,
cast(int) 0x9216D5D9, cast(int) 0x8979FB1B
];

private const int[]
KS0 = [
cast(int) 0xD1310BA6, cast(int) 0x98DFB5AC, cast(int) 0x2FFD72DB, cast(int)
0xD01ADFB7,
cast(int) 0xB8E1AFED, cast(int) 0x6A267E96, cast(int) 0xBA7C9045, cast(int)
0xF12C7F99,
cast(int) 0x24A19947, cast(int) 0xB3916CF7, cast(int) 0x0801F2E2, cast(int)
0x858EFC16,
cast(int) 0x636920D8, cast(int) 0x71574E69, cast(int) 0xA458FEA3, cast(int)
0xF4933D7E,
cast(int) 0x0D95748F, cast(int) 0x728EB658, cast(int) 0x718BCD58, cast(int)
0x82154AEE,
cast(int) 0x7B54A41D, cast(int) 0xC25A59B5, cast(int) 0x9C30D539, cast(int)
0x2AF26013,
cast(int) 0xC5D1B023, cast(int) 0x286085F0, cast(int) 0xCA417918, cast(int)
0xB8DB38EF,
cast(int) 0x8E79DCB0, cast(int) 0x603A180E, cast(int) 0x6C9E0E8B, cast(int)
0xB01E8A3E,
cast(int) 0xD71577C1, cast(int) 0xBD314B27, cast(int) 0x78AF2FDA, cast(int)
0x55605C60,
cast(int) 0xE65525F3, cast(int) 0xAA55AB94, cast(int) 0x57489862, cast(int)
0x63E81440,
cast(int) 0x55CA396A, cast(int) 0x2AAB10B6, cast(int) 0xB4CC5C34, cast(int)
0x1141E8CE,
cast(int) 0xA15486AF, cast(int) 0x7C72E993, cast(int) 0xB3EE1411, cast(int)
0x636FBC2A,
cast(int) 0x2BA9C55D, cast(int) 0x741831F6, cast(int) 0xCE5C3E16, cast(int)
0x9B87931E,
cast(int) 0xAFD6BA33, cast(int) 0x6C24CF5C, cast(int) 0x7A325381, cast(int)
0x28958677,
cast(int) 0x3B8F4898, cast(int) 0x6B4BB9AF, cast(int) 0xC4BFE81B, cast(int)
0x66282193,
cast(int) 0x61D809CC, cast(int) 0xFB21A991, cast(int) 0x487CAC60, cast(int)
0x5DEC8032,
cast(int) 0xEF845D5D, cast(int) 0xE98575B1, cast(int) 0xDC262302, cast(int)
0xEB651B88,
cast(int) 0x23893E81, cast(int) 0xD396ACC5, cast(int) 0x0F6D6FF3, cast(int)
0x83F44239,
cast(int) 0x2E0B4482, cast(int) 0xA4842004, cast(int) 0x69C8F04A, cast(int)
0x9E1F9B5E,
cast(int) 0x21C66842, cast(int) 0xF6E96C9A, cast(int) 0x670C9C61, cast(int)
0xABD388F0,
cast(int) 0x6A51A0D2, cast(int) 0xD8542F68, cast(int) 0x960FA728, cast(int)
0xAB5133A3,
cast(int) 0x6EEF0B6C, cast(int) 0x137A3BE4, cast(int) 0xBA3BF050, cast(int)
0x7EFB2A98,
cast(int) 0xA1F1651D, cast(int) 0x39AF0176, cast(int) 0x66CA593E, cast(int)
0x82430E88,
cast(int) 0x8CEE8619, cast(int) 0x456F9FB4, cast(int) 0x7D84A5C3, cast(int)
0x3B8B5EBE,
cast(int) 0xE06F75D8, cast(int) 0x85C12073, cast(int) 0x401A449F, cast(int)
0x56C16AA6,
cast(int) 0x4ED3AA62, cast(int) 0x363F7706, cast(int) 0x1BFEDF72, cast(int)
0x429B023D,
cast(int) 0x37D0D724, cast(int) 0xD00A1248, cast(int) 0xDB0FEAD3, cast(int)
0x49F1C09B,
cast(int) 0x075372C9, cast(int) 0x80991B7B, cast(int) 0x25D479D8, cast(int)
0xF6E8DEF7,
cast(int) 0xE3FE501A, cast(int) 0xB6794C3B, cast(int) 0x976CE0BD, cast(int)
0x04C006BA,
cast(int) 0xC1A94FB6, cast(int) 0x409F60C4, cast(int) 0x5E5C9EC2, cast(int)
0x196A2463,
cast(int) 0x68FB6FAF, cast(int) 0x3E6C53B5, cast(int) 0x1339B2EB, cast(int)
0x3B52EC6F,
cast(int) 0x6DFC511F, cast(int) 0x9B30952C, cast(int) 0xCC814544, cast(int)
0xAF5EBD09,
cast(int) 0xBEE3D004, cast(int) 0xDE334AFD, cast(int) 0x660F2807, cast(int)
0x192E4BB3,
cast(int) 0xC0CBA857, cast(int) 0x45C8740F, cast(int) 0xD20B5F39, cast(int)
0xB9D3FBDB,
cast(int) 0x5579C0BD, cast(int) 0x1A60320A, cast(int) 0xD6A100C6, cast(int)
0x402C7279,
cast(int) 0x679F25FE, cast(int) 0xFB1FA3CC, cast(int) 0x8EA5E9F8, cast(int)
0xDB3222F8,
cast(int) 0x3C7516DF, cast(int) 0xFD616B15, cast(int) 0x2F501EC8, cast(int)
0xAD0552AB,
cast(int) 0x323DB5FA, cast(int) 0xFD238760, cast(int) 0x53317B48, cast(int)
0x3E00DF82,
cast(int) 0x9E5C57BB, cast(int) 0xCA6F8CA0, cast(int) 0x1A87562E, cast(int)
0xDF1769DB,
cast(int) 0xD542A8F6, cast(int) 0x287EFFC3, cast(int) 0xAC6732C6, cast(int)
0x8C4F5573,
cast(int) 0x695B27B0, cast(int) 0xBBCA58C8, cast(int) 0xE1FFA35D, cast(int)
0xB8F011A0,
cast(int) 0x10FA3D98, cast(int) 0xFD2183B8, cast(int) 0x4AFCB56C, cast(int)
0x2DD1D35B,
cast(int) 0x9A53E479, cast(int) 0xB6F84565, cast(int) 0xD28E49BC, cast(int)
0x4BFB9790,
cast(int) 0xE1DDF2DA, cast(int) 0xA4CB7E33, cast(int) 0x62FB1341, cast(int)
0xCEE4C6E8,
cast(int) 0xEF20CADA, cast(int) 0x36774C01, cast(int) 0xD07E9EFE, cast(int)
0x2BF11FB4,
cast(int) 0x95DBDA4D, cast(int) 0xAE909198, cast(int) 0xEAAD8E71, cast(int)
0x6B93D5A0,
cast(int) 0xD08ED1D0, cast(int) 0xAFC725E0, cast(int) 0x8E3C5B2F, cast(int)
0x8E7594B7,
cast(int) 0x8FF6E2FB, cast(int) 0xF2122B64, cast(int) 0x8888B812, cast(int)
0x900DF01C,
cast(int) 0x4FAD5EA0, cast(int) 0x688FC31C, cast(int) 0xD1CFF191, cast(int)
0xB3A8C1AD,
cast(int) 0x2F2F2218, cast(int) 0xBE0E1777, cast(int) 0xEA752DFE, cast(int)
0x8B021FA1,
cast(int) 0xE5A0CC0F, cast(int) 0xB56F74E8, cast(int) 0x18ACF3D6, cast(int)
0xCE89E299,
cast(int) 0xB4A84FE0, cast(int) 0xFD13E0B7, cast(int) 0x7CC43B81, cast(int)
0xD2ADA8D9,
cast(int) 0x165FA266, cast(int) 0x80957705, cast(int) 0x93CC7314, cast(int)
0x211A1477,
cast(int) 0xE6AD2065, cast(int) 0x77B5FA86, cast(int) 0xC75442F5, cast(int)
0xFB9D35CF,
cast(int) 0xEBCDAF0C, cast(int) 0x7B3E89A0, cast(int) 0xD6411BD3, cast(int)
0xAE1E7E49,
cast(int) 0x00250E2D, cast(int) 0x2071B35E, cast(int) 0x226800BB, cast(int)
0x57B8E0AF,
cast(int) 0x2464369B, cast(int) 0xF009B91E, cast(int) 0x5563911D, cast(int)
0x59DFA6AA,
cast(int) 0x78C14389, cast(int) 0xD95A537F, cast(int) 0x207D5BA2, cast(int)
0x02E5B9C5,
cast(int) 0x83260376, cast(int) 0x6295CFA9, cast(int) 0x11C81968, cast(int)
0x4E734A41,
cast(int) 0xB3472DCA, cast(int) 0x7B14A94A, cast(int) 0x1B510052, cast(int)
0x9A532915,
cast(int) 0xD60F573F, cast(int) 0xBC9BC6E4, cast(int) 0x2B60A476, cast(int)
0x81E67400,
cast(int) 0x08BA6FB5, cast(int) 0x571BE91F, cast(int) 0xF296EC6B, cast(int)
0x2A0DD915,
cast(int) 0xB6636521, cast(int) 0xE7B9F9B6, cast(int) 0xFF34052E, cast(int)
0xC5855664,
cast(int) 0x53B02D5D, cast(int) 0xA99F8FA1, cast(int) 0x08BA4799, cast(int)
0x6E85076A
];

private const int[]
KS1 = [
cast(int) 0x4B7A70E9, cast(int) 0xB5B32944, cast(int) 0xDB75092E, cast(int)
0xC4192623,
cast(int) 0xAD6EA6B0, cast(int) 0x49A7DF7D, cast(int) 0x9CEE60B8, cast(int)
0x8FEDB266,
cast(int) 0xECAA8C71, cast(int) 0x699A17FF, cast(int) 0x5664526C, cast(int)
0xC2B19EE1,
cast(int) 0x193602A5, cast(int) 0x75094C29, cast(int) 0xA0591340, cast(int)
0xE4183A3E,
cast(int) 0x3F54989A, cast(int) 0x5B429D65, cast(int) 0x6B8FE4D6, cast(int)
0x99F73FD6,
cast(int) 0xA1D29C07, cast(int) 0xEFE830F5, cast(int) 0x4D2D38E6, cast(int)
0xF0255DC1,
cast(int) 0x4CDD2086, cast(int) 0x8470EB26, cast(int) 0x6382E9C6, cast(int)
0x021ECC5E,
cast(int) 0x09686B3F, cast(int) 0x3EBAEFC9, cast(int) 0x3C971814, cast(int)
0x6B6A70A1,
cast(int) 0x687F3584, cast(int) 0x52A0E286, cast(int) 0xB79C5305, cast(int)
0xAA500737,
cast(int) 0x3E07841C, cast(int) 0x7FDEAE5C, cast(int) 0x8E7D44EC, cast(int)
0x5716F2B8,
cast(int) 0xB03ADA37, cast(int) 0xF0500C0D, cast(int) 0xF01C1F04, cast(int)
0x0200B3FF,
cast(int) 0xAE0CF51A, cast(int) 0x3CB574B2, cast(int) 0x25837A58, cast(int)
0xDC0921BD,
cast(int) 0xD19113F9, cast(int) 0x7CA92FF6, cast(int) 0x94324773, cast(int)
0x22F54701,
cast(int) 0x3AE5E581, cast(int) 0x37C2DADC, cast(int) 0xC8B57634, cast(int)
0x9AF3DDA7,
cast(int) 0xA9446146, cast(int) 0x0FD0030E, cast(int) 0xECC8C73E, cast(int)
0xA4751E41,
cast(int) 0xE238CD99, cast(int) 0x3BEA0E2F, cast(int) 0x3280BBA1, cast(int)
0x183EB331,
cast(int) 0x4E548B38, cast(int) 0x4F6DB908, cast(int) 0x6F420D03, cast(int)
0xF60A04BF,
cast(int) 0x2CB81290, cast(int) 0x24977C79, cast(int) 0x5679B072, cast(int)
0xBCAF89AF,
cast(int) 0xDE9A771F, cast(int) 0xD9930810, cast(int) 0xB38BAE12, cast(int)
0xDCCF3F2E,
cast(int) 0x5512721F, cast(int) 0x2E6B7124, cast(int) 0x501ADDE6, cast(int)
0x9F84CD87,
cast(int) 0x7A584718, cast(int) 0x7408DA17, cast(int) 0xBC9F9ABC, cast(int)
0xE94B7D8C,
cast(int) 0xEC7AEC3A, cast(int) 0xDB851DFA, cast(int) 0x63094366, cast(int)
0xC464C3D2,
cast(int) 0xEF1C1847, cast(int) 0x3215D908, cast(int) 0xDD433B37, cast(int)
0x24C2BA16,
cast(int) 0x12A14D43, cast(int) 0x2A65C451, cast(int) 0x50940002, cast(int)
0x133AE4DD,
cast(int) 0x71DFF89E, cast(int) 0x10314E55, cast(int) 0x81AC77D6, cast(int)
0x5F11199B,
cast(int) 0x043556F1, cast(int) 0xD7A3C76B, cast(int) 0x3C11183B, cast(int)
0x5924A509,
cast(int) 0xF28FE6ED, cast(int) 0x97F1FBFA, cast(int) 0x9EBABF2C, cast(int)
0x1E153C6E,
cast(int) 0x86E34570, cast(int) 0xEAE96FB1, cast(int) 0x860E5E0A, cast(int)
0x5A3E2AB3,
cast(int) 0x771FE71C, cast(int) 0x4E3D06FA, cast(int) 0x2965DCB9, cast(int)
0x99E71D0F,
cast(int) 0x803E89D6, cast(int) 0x5266C825, cast(int) 0x2E4CC978, cast(int)
0x9C10B36A,
cast(int) 0xC6150EBA, cast(int) 0x94E2EA78, cast(int) 0xA5FC3C53, cast(int)
0x1E0A2DF4,
cast(int) 0xF2F74EA7, cast(int) 0x361D2B3D, cast(int) 0x1939260F, cast(int)
0x19C27960,
cast(int) 0x5223A708, cast(int) 0xF71312B6, cast(int) 0xEBADFE6E, cast(int)
0xEAC31F66,
cast(int) 0xE3BC4595, cast(int) 0xA67BC883, cast(int) 0xB17F37D1, cast(int)
0x018CFF28,
cast(int) 0xC332DDEF, cast(int) 0xBE6C5AA5, cast(int) 0x65582185, cast(int)
0x68AB9802,
cast(int) 0xEECEA50F, cast(int) 0xDB2F953B, cast(int) 0x2AEF7DAD, cast(int)
0x5B6E2F84,
cast(int) 0x1521B628, cast(int) 0x29076170, cast(int) 0xECDD4775, cast(int)
0x619F1510,
cast(int) 0x13CCA830, cast(int) 0xEB61BD96, cast(int) 0x0334FE1E, cast(int)
0xAA0363CF,
cast(int) 0xB5735C90, cast(int) 0x4C70A239, cast(int) 0xD59E9E0B, cast(int)
0xCBAADE14,
cast(int) 0xEECC86BC, cast(int) 0x60622CA7, cast(int) 0x9CAB5CAB, cast(int)
0xB2F3846E,
cast(int) 0x648B1EAF, cast(int) 0x19BDF0CA, cast(int) 0xA02369B9, cast(int)
0x655ABB50,
cast(int) 0x40685A32, cast(int) 0x3C2AB4B3, cast(int) 0x319EE9D5, cast(int)
0xC021B8F7,
cast(int) 0x9B540B19, cast(int) 0x875FA099, cast(int) 0x95F7997E, cast(int)
0x623D7DA8,
cast(int) 0xF837889A, cast(int) 0x97E32D77, cast(int) 0x11ED935F, cast(int)
0x16681281,
cast(int) 0x0E358829, cast(int) 0xC7E61FD6, cast(int) 0x96DEDFA1, cast(int)
0x7858BA99,
cast(int) 0x57F584A5, cast(int) 0x1B227263, cast(int) 0x9B83C3FF, cast(int)
0x1AC24696,
cast(int) 0xCDB30AEB, cast(int) 0x532E3054, cast(int) 0x8FD948E4, cast(int)
0x6DBC3128,
cast(int) 0x58EBF2EF, cast(int) 0x34C6FFEA, cast(int) 0xFE28ED61, cast(int)
0xEE7C3C73,
cast(int) 0x5D4A14D9, cast(int) 0xE864B7E3, cast(int) 0x42105D14, cast(int)
0x203E13E0,
cast(int) 0x45EEE2B6, cast(int) 0xA3AAABEA, cast(int) 0xDB6C4F15, cast(int)
0xFACB4FD0,
cast(int) 0xC742F442, cast(int) 0xEF6ABBB5, cast(int) 0x654F3B1D, cast(int)
0x41CD2105,
cast(int) 0xD81E799E, cast(int) 0x86854DC7, cast(int) 0xE44B476A, cast(int)
0x3D816250,
cast(int) 0xCF62A1F2, cast(int) 0x5B8D2646, cast(int) 0xFC8883A0, cast(int)
0xC1C7B6A3,
cast(int) 0x7F1524C3, cast(int) 0x69CB7492, cast(int) 0x47848A0B, cast(int)
0x5692B285,
cast(int) 0x095BBF00, cast(int) 0xAD19489D, cast(int) 0x1462B174, cast(int)
0x23820E00,
cast(int) 0x58428D2A, cast(int) 0x0C55F5EA, cast(int) 0x1DADF43E, cast(int)
0x233F7061,
cast(int) 0x3372F092, cast(int) 0x8D937E41, cast(int) 0xD65FECF1, cast(int)
0x6C223BDB,
cast(int) 0x7CDE3759, cast(int) 0xCBEE7460, cast(int) 0x4085F2A7, cast(int)
0xCE77326E,
cast(int) 0xA6078084, cast(int) 0x19F8509E, cast(int) 0xE8EFD855, cast(int)
0x61D99735,
cast(int) 0xA969A7AA, cast(int) 0xC50C06C2, cast(int) 0x5A04ABFC, cast(int)
0x800BCADC,
cast(int) 0x9E447A2E, cast(int) 0xC3453484, cast(int) 0xFDD56705, cast(int)
0x0E1E9EC9,
cast(int) 0xDB73DBD3, cast(int) 0x105588CD, cast(int) 0x675FDA79, cast(int)
0xE3674340,
cast(int) 0xC5C43465, cast(int) 0x713E38D8, cast(int) 0x3D28F89E, cast(int)
0xF16DFF20,
cast(int) 0x153E21E7, cast(int) 0x8FB03D4A, cast(int) 0xE6E39F2B, cast(int)
0xDB83ADF7
];

private const int[]
KS2 = [
cast(int) 0xE93D5A68, cast(int) 0x948140F7, cast(int) 0xF64C261C, cast(int)
0x94692934,
cast(int) 0x411520F7, cast(int) 0x7602D4F7, cast(int) 0xBCF46B2E, cast(int)
0xD4A20068,
cast(int) 0xD4082471, cast(int) 0x3320F46A, cast(int) 0x43B7D4B7, cast(int)
0x500061AF,
cast(int) 0x1E39F62E, cast(int) 0x97244546, cast(int) 0x14214F74, cast(int)
0xBF8B8840,
cast(int) 0x4D95FC1D, cast(int) 0x96B591AF, cast(int) 0x70F4DDD3, cast(int)
0x66A02F45,
cast(int) 0xBFBC09EC, cast(int) 0x03BD9785, cast(int) 0x7FAC6DD0, cast(int)
0x31CB8504,
cast(int) 0x96EB27B3, cast(int) 0x55FD3941, cast(int) 0xDA2547E6, cast(int)
0xABCA0A9A,
cast(int) 0x28507825, cast(int) 0x530429F4, cast(int) 0x0A2C86DA, cast(int)
0xE9B66DFB,
cast(int) 0x68DC1462, cast(int) 0xD7486900, cast(int) 0x680EC0A4, cast(int)
0x27A18DEE,
cast(int) 0x4F3FFEA2, cast(int) 0xE887AD8C, cast(int) 0xB58CE006, cast(int)
0x7AF4D6B6,
cast(int) 0xAACE1E7C, cast(int) 0xD3375FEC, cast(int) 0xCE78A399, cast(int)
0x406B2A42,
cast(int) 0x20FE9E35, cast(int) 0xD9F385B9, cast(int) 0xEE39D7AB, cast(int)
0x3B124E8B,
cast(int) 0x1DC9FAF7, cast(int) 0x4B6D1856, cast(int) 0x26A36631, cast(int)
0xEAE397B2,
cast(int) 0x3A6EFA74, cast(int) 0xDD5B4332, cast(int) 0x6841E7F7, cast(int)
0xCA7820FB,
cast(int) 0xFB0AF54E, cast(int) 0xD8FEB397, cast(int) 0x454056AC, cast(int)
0xBA489527,
cast(int) 0x55533A3A, cast(int) 0x20838D87, cast(int) 0xFE6BA9B7, cast(int)
0xD096954B,
cast(int) 0x55A867BC, cast(int) 0xA1159A58, cast(int) 0xCCA92963, cast(int)
0x99E1DB33,
cast(int) 0xA62A4A56, cast(int) 0x3F3125F9, cast(int) 0x5EF47E1C, cast(int)
0x9029317C,
cast(int) 0xFDF8E802, cast(int) 0x04272F70, cast(int) 0x80BB155C, cast(int)
0x05282CE3,
cast(int) 0x95C11548, cast(int) 0xE4C66D22, cast(int) 0x48C1133F, cast(int)
0xC70F86DC,
cast(int) 0x07F9C9EE, cast(int) 0x41041F0F, cast(int) 0x404779A4, cast(int)
0x5D886E17,
cast(int) 0x325F51EB, cast(int) 0xD59BC0D1, cast(int) 0xF2BCC18F, cast(int)
0x41113564,
cast(int) 0x257B7834, cast(int) 0x602A9C60, cast(int) 0xDFF8E8A3, cast(int)
0x1F636C1B,
cast(int) 0x0E12B4C2, cast(int) 0x02E1329E, cast(int) 0xAF664FD1, cast(int)
0xCAD18115,
cast(int) 0x6B2395E0, cast(int) 0x333E92E1, cast(int) 0x3B240B62, cast(int)
0xEEBEB922,
cast(int) 0x85B2A20E, cast(int) 0xE6BA0D99, cast(int) 0xDE720C8C, cast(int)
0x2DA2F728,
cast(int) 0xD0127845, cast(int) 0x95B794FD, cast(int) 0x647D0862, cast(int)
0xE7CCF5F0,
cast(int) 0x5449A36F, cast(int) 0x877D48FA, cast(int) 0xC39DFD27, cast(int)
0xF33E8D1E,
cast(int) 0x0A476341, cast(int) 0x992EFF74, cast(int) 0x3A6F6EAB, cast(int)
0xF4F8FD37,
cast(int) 0xA812DC60, cast(int) 0xA1EBDDF8, cast(int) 0x991BE14C, cast(int)
0xDB6E6B0D,
cast(int) 0xC67B5510, cast(int) 0x6D672C37, cast(int) 0x2765D43B, cast(int)
0xDCD0E804,
cast(int) 0xF1290DC7, cast(int) 0xCC00FFA3, cast(int) 0xB5390F92, cast(int)
0x690FED0B,
cast(int) 0x667B9FFB, cast(int) 0xCEDB7D9C, cast(int) 0xA091CF0B, cast(int)
0xD9155EA3,
cast(int) 0xBB132F88, cast(int) 0x515BAD24, cast(int) 0x7B9479BF, cast(int)
0x763BD6EB,
cast(int) 0x37392EB3, cast(int) 0xCC115979, cast(int) 0x8026E297, cast(int)
0xF42E312D,
cast(int) 0x6842ADA7, cast(int) 0xC66A2B3B, cast(int) 0x12754CCC, cast(int)
0x782EF11C,
cast(int) 0x6A124237, cast(int) 0xB79251E7, cast(int) 0x06A1BBE6, cast(int)
0x4BFB6350,
cast(int) 0x1A6B1018, cast(int) 0x11CAEDFA, cast(int) 0x3D25BDD8, cast(int)
0xE2E1C3C9,
cast(int) 0x44421659, cast(int) 0x0A121386, cast(int) 0xD90CEC6E, cast(int)
0xD5ABEA2A,
cast(int) 0x64AF674E, cast(int) 0xDA86A85F, cast(int) 0xBEBFE988, cast(int)
0x64E4C3FE,
cast(int) 0x9DBC8057, cast(int) 0xF0F7C086, cast(int) 0x60787BF8, cast(int)
0x6003604D,
cast(int) 0xD1FD8346, cast(int) 0xF6381FB0, cast(int) 0x7745AE04, cast(int)
0xD736FCCC,
cast(int) 0x83426B33, cast(int) 0xF01EAB71, cast(int) 0xB0804187, cast(int)
0x3C005E5F,
cast(int) 0x77A057BE, cast(int) 0xBDE8AE24, cast(int) 0x55464299, cast(int)
0xBF582E61,
cast(int) 0x4E58F48F, cast(int) 0xF2DDFDA2, cast(int) 0xF474EF38, cast(int)
0x8789BDC2,
cast(int) 0x5366F9C3, cast(int) 0xC8B38E74, cast(int) 0xB475F255, cast(int)
0x46FCD9B9,
cast(int) 0x7AEB2661, cast(int) 0x8B1DDF84, cast(int) 0x846A0E79, cast(int)
0x915F95E2,
cast(int) 0x466E598E, cast(int) 0x20B45770, cast(int) 0x8CD55591, cast(int)
0xC902DE4C,
cast(int) 0xB90BACE1, cast(int) 0xBB8205D0, cast(int) 0x11A86248, cast(int)
0x7574A99E,
cast(int) 0xB77F19B6, cast(int) 0xE0A9DC09, cast(int) 0x662D09A1, cast(int)
0xC4324633,
cast(int) 0xE85A1F02, cast(int) 0x09F0BE8C, cast(int) 0x4A99A025, cast(int)
0x1D6EFE10,
cast(int) 0x1AB93D1D, cast(int) 0x0BA5A4DF, cast(int) 0xA186F20F, cast(int)
0x2868F169,
cast(int) 0xDCB7DA83, cast(int) 0x573906FE, cast(int) 0xA1E2CE9B, cast(int)
0x4FCD7F52,
cast(int) 0x50115E01, cast(int) 0xA70683FA, cast(int) 0xA002B5C4, cast(int)
0x0DE6D027,
cast(int) 0x9AF88C27, cast(int) 0x773F8641, cast(int) 0xC3604C06, cast(int)
0x61A806B5,
cast(int) 0xF0177A28, cast(int) 0xC0F586E0, cast(int) 0x006058AA, cast(int)
0x30DC7D62,
cast(int) 0x11E69ED7, cast(int) 0x2338EA63, cast(int) 0x53C2DD94, cast(int)
0xC2C21634,
cast(int) 0xBBCBEE56, cast(int) 0x90BCB6DE, cast(int) 0xEBFC7DA1, cast(int)
0xCE591D76,
cast(int) 0x6F05E409, cast(int) 0x4B7C0188, cast(int) 0x39720A3D, cast(int)
0x7C927C24,
cast(int) 0x86E3725F, cast(int) 0x724D9DB9, cast(int) 0x1AC15BB4, cast(int)
0xD39EB8FC,
cast(int) 0xED545578, cast(int) 0x08FCA5B5, cast(int) 0xD83D7CD3, cast(int)
0x4DAD0FC4,
cast(int) 0x1E50EF5E, cast(int) 0xB161E6F8, cast(int) 0xA28514D9, cast(int)
0x6C51133C,
cast(int) 0x6FD5C7E7, cast(int) 0x56E14EC4, cast(int) 0x362ABFCE, cast(int)
0xDDC6C837,
cast(int) 0xD79A3234, cast(int) 0x92638212, cast(int) 0x670EFA8E, cast(int)
0x406000E0
];

private const int[]
KS3 = [
cast(int) 0x3A39CE37, cast(int) 0xD3FAF5CF, cast(int) 0xABC27737, cast(int)
0x5AC52D1B,
cast(int) 0x5CB0679E, cast(int) 0x4FA33742, cast(int) 0xD3822740, cast(int)
0x99BC9BBE,
cast(int) 0xD5118E9D, cast(int) 0xBF0F7315, cast(int) 0xD62D1C7E, cast(int)
0xC700C47B,
cast(int) 0xB78C1B6B, cast(int) 0x21A19045, cast(int) 0xB26EB1BE, cast(int)
0x6A366EB4,
cast(int) 0x5748AB2F, cast(int) 0xBC946E79, cast(int) 0xC6A376D2, cast(int)
0x6549C2C8,
cast(int) 0x530FF8EE, cast(int) 0x468DDE7D, cast(int) 0xD5730A1D, cast(int)
0x4CD04DC6,
cast(int) 0x2939BBDB, cast(int) 0xA9BA4650, cast(int) 0xAC9526E8, cast(int)
0xBE5EE304,
cast(int) 0xA1FAD5F0, cast(int) 0x6A2D519A, cast(int) 0x63EF8CE2, cast(int)
0x9A86EE22,
cast(int) 0xC089C2B8, cast(int) 0x43242EF6, cast(int) 0xA51E03AA, cast(int)
0x9CF2D0A4,
cast(int) 0x83C061BA, cast(int) 0x9BE96A4D, cast(int) 0x8FE51550, cast(int)
0xBA645BD6,
cast(int) 0x2826A2F9, cast(int) 0xA73A3AE1, cast(int) 0x4BA99586, cast(int)
0xEF5562E9,
cast(int) 0xC72FEFD3, cast(int) 0xF752F7DA, cast(int) 0x3F046F69, cast(int)
0x77FA0A59,
cast(int) 0x80E4A915, cast(int) 0x87B08601, cast(int) 0x9B09E6AD, cast(int)
0x3B3EE593,
cast(int) 0xE990FD5A, cast(int) 0x9E34D797, cast(int) 0x2CF0B7D9, cast(int)
0x022B8B51,
cast(int) 0x96D5AC3A, cast(int) 0x017DA67D, cast(int) 0xD1CF3ED6, cast(int)
0x7C7D2D28,
cast(int) 0x1F9F25CF, cast(int) 0xADF2B89B, cast(int) 0x5AD6B472, cast(int)
0x5A88F54C,
cast(int) 0xE029AC71, cast(int) 0xE019A5E6, cast(int) 0x47B0ACFD, cast(int)
0xED93FA9B,
cast(int) 0xE8D3C48D, cast(int) 0x283B57CC, cast(int) 0xF8D56629, cast(int)
0x79132E28,
cast(int) 0x785F0191, cast(int) 0xED756055, cast(int) 0xF7960E44, cast(int)
0xE3D35E8C,
cast(int) 0x15056DD4, cast(int) 0x88F46DBA, cast(int) 0x03A16125, cast(int)
0x0564F0BD,
cast(int) 0xC3EB9E15, cast(int) 0x3C9057A2, cast(int) 0x97271AEC, cast(int)
0xA93A072A,
cast(int) 0x1B3F6D9B, cast(int) 0x1E6321F5, cast(int) 0xF59C66FB, cast(int)
0x26DCF319,
cast(int) 0x7533D928, cast(int) 0xB155FDF5, cast(int) 0x03563482, cast(int)
0x8ABA3CBB,
cast(int) 0x28517711, cast(int) 0xC20AD9F8, cast(int) 0xABCC5167, cast(int)
0xCCAD925F,
cast(int) 0x4DE81751, cast(int) 0x3830DC8E, cast(int) 0x379D5862, cast(int)
0x9320F991,
cast(int) 0xEA7A90C2, cast(int) 0xFB3E7BCE, cast(int) 0x5121CE64, cast(int)
0x774FBE32,
cast(int) 0xA8B6E37E, cast(int) 0xC3293D46, cast(int) 0x48DE5369, cast(int)
0x6413E680,
cast(int) 0xA2AE0810, cast(int) 0xDD6DB224, cast(int) 0x69852DFD, cast(int)
0x09072166,
cast(int) 0xB39A460A, cast(int) 0x6445C0DD, cast(int) 0x586CDECF, cast(int)
0x1C20C8AE,
cast(int) 0x5BBEF7DD, cast(int) 0x1B588D40, cast(int) 0xCCD2017F, cast(int)
0x6BB4E3BB,
cast(int) 0xDDA26A7E, cast(int) 0x3A59FF45, cast(int) 0x3E350A44, cast(int)
0xBCB4CDD5,
cast(int) 0x72EACEA8, cast(int) 0xFA6484BB, cast(int) 0x8D6612AE, cast(int)
0xBF3C6F47,
cast(int) 0xD29BE463, cast(int) 0x542F5D9E, cast(int) 0xAEC2771B, cast(int)
0xF64E6370,
cast(int) 0x740E0D8D, cast(int) 0xE75B1357, cast(int) 0xF8721671, cast(int)
0xAF537D5D,
cast(int) 0x4040CB08, cast(int) 0x4EB4E2CC, cast(int) 0x34D2466A, cast(int)
0x0115AF84,
cast(int) 0xE1B00428, cast(int) 0x95983A1D, cast(int) 0x06B89FB4, cast(int)
0xCE6EA048,
cast(int) 0x6F3F3B82, cast(int) 0x3520AB82, cast(int) 0x011A1D4B, cast(int)
0x277227F8,
cast(int) 0x611560B1, cast(int) 0xE7933FDC, cast(int) 0xBB3A792B, cast(int)
0x344525BD,
cast(int) 0xA08839E1, cast(int) 0x51CE794B, cast(int) 0x2F32C9B7, cast(int)
0xA01FBAC9,
cast(int) 0xE01CC87E, cast(int) 0xBCC7D1F6, cast(int) 0xCF0111C3, cast(int)
0xA1E8AAC7,
cast(int) 0x1A908749, cast(int) 0xD44FBD9A, cast(int) 0xD0DADECB, cast(int)
0xD50ADA38,
cast(int) 0x0339C32A, cast(int) 0xC6913667, cast(int) 0x8DF9317C, cast(int)
0xE0B12B4F,
cast(int) 0xF79E59B7, cast(int) 0x43F5BB3A, cast(int) 0xF2D519FF, cast(int)
0x27D9459C,
cast(int) 0xBF97222C, cast(int) 0x15E6FC2A, cast(int) 0x0F91FC71, cast(int)
0x9B941525,
cast(int) 0xFAE59361, cast(int) 0xCEB69CEB, cast(int) 0xC2A86459, cast(int)
0x12BAA8D1,
cast(int) 0xB6C1075E, cast(int) 0xE3056A0C, cast(int) 0x10D25065, cast(int)
0xCB03A442,
cast(int) 0xE0EC6E0E, cast(int) 0x1698DB3B, cast(int) 0x4C98A0BE, cast(int)
0x3278E964,
cast(int) 0x9F1F9532, cast(int) 0xE0D392DF, cast(int) 0xD3A0342B, cast(int)
0x8971F21E,
cast(int) 0x1B0A7441, cast(int) 0x4BA3348C, cast(int) 0xC5BE7120, cast(int)
0xC37632D8,
cast(int) 0xDF359F8D, cast(int) 0x9B992F2E, cast(int) 0xE60B6F47, cast(int)
0x0FE3F11D,
cast(int) 0xE54CDA54, cast(int) 0x1EDAD891, cast(int) 0xCE6279CF, cast(int)
0xCD3E7E6F,
cast(int) 0x1618B166, cast(int) 0xFD2C1D05, cast(int) 0x848FD2C5, cast(int)
0xF6FB2299,
cast(int) 0xF523F357, cast(int) 0xA6327623, cast(int) 0x93A83531, cast(int)
0x56CCCD02,
cast(int) 0xACF08162, cast(int) 0x5A75EBB5, cast(int) 0x6E163697, cast(int)
0x88D273CC,
cast(int) 0xDE966292, cast(int) 0x81B949D0, cast(int) 0x4C50901B, cast(int)
0x71C65614,
cast(int) 0xE6C6C7BD, cast(int) 0x327A140A, cast(int) 0x45E1D006, cast(int)
0xC3F27B9A,
cast(int) 0xC9AA53FD, cast(int) 0x62A80F00, cast(int) 0xBB25BFE2, cast(int)
0x35BDD2F6,
cast(int) 0x71126905, cast(int) 0xB2040222, cast(int) 0xB6CBCF7C, cast(int)
0xCD769C2B,
cast(int) 0x53113EC0, cast(int) 0x1640E3D3, cast(int) 0x38ABBD60, cast(int)
0x2547ADF0,
cast(int) 0xBA38209C, cast(int) 0xF746CE76, cast(int) 0x77AFA1C5, cast(int)
0x20756060,
cast(int) 0x85CBFE4E, cast(int) 0x8AE88DD8, cast(int) 0x7AAAF9B0, cast(int)
0x4CF9AA7E,
cast(int) 0x1948C25C, cast(int) 0x02FB8A8C, cast(int) 0x01C36AE4, cast(int)
0xD6EBE1F9,
cast(int) 0x90D4F869, cast(int) 0xA65CDEA0, cast(int) 0x3F09252D, cast(int)
0xC208E69F,
cast(int) 0xB74E6132, cast(int) 0xCE77E25B, cast(int) 0x578FDFE3, cast(int)
0x3AC372E6
];

//====================================
// Useful constants
//====================================
private static const int    ROUNDS = 16;
private static const int    BLOCK_SIZE = 8;  // bytes = 64 bits
private static const int    SBOX_SK = 256;
private static const int    P_SZ = ROUNDS+2;
private const int[] S0, S1, S2, S3;     // the s-boxes
private const int[] P;                  // the p-array
private bool encrypting = false;
private ubyte[] workingKey = null;

public this()
{
S0 = new int[SBOX_SK];
S1 = new int[SBOX_SK];
S2 = new int[SBOX_SK];
S3 = new int[SBOX_SK];
P = new int[P_SZ];
}

public this(bool encrypting, ubyte[] key)
{
S0 = new int[SBOX_SK];
S1 = new int[SBOX_SK];
S2 = new int[SBOX_SK];
S3 = new int[SBOX_SK];
P = new int[P_SZ];
init(encrypting, key);
}

/**
* initialise a Blowfish cipher.
*
* @param forEncryption whether or not we are for encryption.
* @param params the parameters required to set up the cipher.
* @exception IllegalArgumentException if the params argument is
* inappropriate.
*/
public void init(bool encrypting, ubyte[] key=null)
{
if (key != null)
{
this.encrypting = encrypting;
this.workingKey = key;
setKey(this.workingKey);
}
else
throw new Exception("invalid parameter passed to Blowfish init - key");
}

public char[] getAlgorithmName()
{
return "Blowfish";
}

public int processBlock(ubyte[] inBytes, int inOff, inout ubyte[] outBytes, int
outOff)
{
if (workingKey == null) {
throw new Exception("Blowfish not initialised");
}
if(inBytes.length == 0) {
throw new Exception("input buffer too short");
}
if ((inOff + BLOCK_SIZE) > inBytes.length) {
throw new Exception("input buffer too short");
}
if ((outOff + BLOCK_SIZE) > outBytes.length) {
outBytes.length = (outOff + BLOCK_SIZE);
}
if (encrypting) {
encryptBlock(inBytes, inOff, outBytes, outOff);
}
else {
decryptBlock(inBytes, inOff, outBytes, outOff);
}
return BLOCK_SIZE;
}

public int processBlock(ubyte[] inBytes, inout ubyte[] outBytes)
{
int pad = 0; int inOff = 0;
if(workingKey == null) {
throw new Exception("Blowfish not initialised");
}
if(inBytes.length == 0) {
throw new Exception("input buffer too short");
}
pad = inBytes.length % BLOCK_SIZE;
if(pad != 0) {
pad = BLOCK_SIZE - pad;
while(inOff < pad) {
inBytes ~= 0x0;
inOff++;
}
}
outBytes.length = inBytes.length;
inOff = 0;
while(inOff < inBytes.length) {
if(encrypting) {
encryptBlock(inBytes, inOff, outBytes, inOff);
}
else {
decryptBlock(inBytes, inOff, outBytes, inOff);
}
inOff += BLOCK_SIZE;
}
return inBytes.length;
}

public void reset()
{
}

public int getBlockSize()
{
return BLOCK_SIZE;
}

//==================================
// Private Implementation
//==================================

private int F(int x)
{
return (((S0[(cast(uint) x >> 24)] + S1[(cast(uint) x >> 16) & 0xff]) ^
S2[(cast(uint) x >> 8) & 0xff]) + S3[x & 0xff]);
}

/**
* apply the encryption cycle to each value pair in the table.
*/
private void processTable(int xl, int xr, int[] table)
{
int size = table.length;
for (int s = 0; s < size; s += 2)
{
xl ^= P[0];
for (int i = 1; i < ROUNDS; i += 2)
{
xr ^= F(xl) ^ P[i];
xl ^= F(xr) ^ P[i + 1];
}
xr ^= P[ROUNDS + 1];
table[s] = xr;
table[s + 1] = xl;
xr = xl;            // end of cycle swap
xl = table[s];
}
}

private void setKey(ubyte[] key)
{
/*
* - comments are from _Applied Crypto_, Schneier, p338
* please be careful comparing the two, AC numbers the
* arrays from 1, the enclosed code from 0.
*
* (1)
* Initialise the S-boxes and the P-array, with a fixed string
* This string contains the hexadecimal digits of pi (3.141...)
*/
//Array.Copy(KS0, 0, S0, 0, SBOX_SK);
S0[0..SBOX_SK] = KS0;
//Array.Copy(KS1, 0, S1, 0, SBOX_SK);
S1[0..SBOX_SK] = KS1;
//Array.Copy(KS2, 0, S2, 0, SBOX_SK);
S2[0..SBOX_SK] = KS2;
//Array.Copy(KS3, 0, S3, 0, SBOX_SK);
S3[0..SBOX_SK] = KS3;

//Array.Copy(KP, 0, P, 0, P_SZ);
P[0..P_SZ] = KP;

/*
* (2)
* Now, XOR P[0] with the first 32 bits of the key, XOR P[1] with the
* second 32-bits of the key, and so on for all bits of the key
* (up to P[17]).  Repeatedly cycle through the key bits until the
* entire P-array has been XOR-ed with the key bits
*/
int keyLength = key.length;
int keyIndex = 0;

for (int i=0; i < P_SZ; i++)
{
// get the 32 bits of the key, in 4 * 8 bit chunks
int data = cast(int) 0x0000000;
for (int j=0; j < 4; j++)
{
// create a 32 bit block
data = (data << 8) | (key[keyIndex++] & 0xff);

// wrap when we get to the end of the key
if (keyIndex >= keyLength)
{
keyIndex = 0;
}
}
// XOR the newly created 32 bit chunk onto the P-array
P[i] ^= data;
}

/*
* (3)
* Encrypt the all-zero string with the Blowfish algorithm, using
* the subkeys described in (1) and (2)
*
* (4)
* Replace P1 and P2 with the output of step (3)
*
* (5)
* Encrypt the output of step(3) using the Blowfish algorithm,
* with the modified subkeys.
*
* (6)
* Replace P3 and P4 with the output of step (5)
*
* (7)
* Continue the process, replacing all elements of the P-array
* and then all four S-boxes in order, with the output of the
* continuously changing Blowfish algorithm
*/
processTable(0, 0, P);
processTable(P[P_SZ - 2], P[P_SZ - 1], S0);
processTable(S0[SBOX_SK - 2], S0[SBOX_SK - 1], S1);
processTable(S1[SBOX_SK - 2], S1[SBOX_SK - 1], S2);
processTable(S2[SBOX_SK - 2], S2[SBOX_SK - 1], S3);
}

/**
* Encrypt the given input starting at the given offset and place
* the result in the provided buffer starting at the given offset.
* The input will be an exact multiple of our blocksize.
*/
private void encryptBlock(ubyte[] src, int srcIndex, ubyte[] dst, int dstIndex)
{
int xl = BytesTo32bits(src, srcIndex);
int xr = BytesTo32bits(src, srcIndex+4);
xl ^= P[0];
for (int i = 1; i < ROUNDS; i += 2)
{
xr ^= F(xl) ^ P[i];
xl ^= F(xr) ^ P[i + 1];
}
xr ^= P[ROUNDS + 1];
Bits32ToBytes(xr, dst, dstIndex);
Bits32ToBytes(xl, dst, dstIndex + 4);
}

/**
* Decrypt the given input starting at the given offset and place
* the result in the provided buffer starting at the given offset.
* The input will be an exact multiple of our blocksize.
*/
private void decryptBlock(ubyte[] src, int srcIndex, ubyte[] dst, int dstIndex)
{
int xl = BytesTo32bits(src, srcIndex);
int xr = BytesTo32bits(src, srcIndex + 4);
xl ^= P[ROUNDS + 1];
for (int i = ROUNDS; i > 0 ; i -= 2)
{
xr ^= F(xl) ^ P[i];
xl ^= F(xr) ^ P[i - 1];
}
xr ^= P[0];
Bits32ToBytes(xr, dst, dstIndex);
Bits32ToBytes(xl, dst, dstIndex+4);
}

private int BytesTo32bits(ubyte[] b, int i)
{
return ((b[i]   & 0xff) << 24) |
((b[i+1] & 0xff) << 16) |
((b[i+2] & 0xff) << 8) |
((b[i+3] & 0xff));
}

private void Bits32ToBytes(int inData,  ubyte[] b, int offset)
{
b[offset + 3] = cast(ubyte)inData;
b[offset + 2] = cast(ubyte)(inData >> 8);
b[offset + 1] = cast(ubyte)(inData >> 16);
b[offset]     = cast(ubyte)(inData >> 24);
}
}

int main() {
ubyte[] bkey;
ubyte[] ins;
ubyte[] outs;
ubyte[] xx;
bkey.length = 8; ins.length = 17; outs.length = 32;   xx.length = 32;
bkey = cast(ubyte[])"xxxxxxxx";
ins = cast(ubyte[])"ttttttttttttttttz";

/*  working
BlowfishEngine bl;
bl = new BlowfishEngine();
bl.init(true, bkey);
bl.processBlock(ins, outs);
printf(">|  %s\n", toStringz(cast(char[])outs));

BlowfishEngine bl1;
bl1 = new BlowfishEngine();
bl1.init(false, bkey);
bl1.processBlock(outs, xx);
printf(">|  %s\n", toStringz(cast(char[])xx));

return 1;
}





More information about the Digitalmars-d-announce mailing list