Crypt-ECB-1.45/0000755000175600010010000000000011037362353012045 5ustar cappelKeinCrypt-ECB-1.45/CHANGES0000755000175600010010000000113711037360035013040 0ustar cappelKeinv1.45, July 2008 - fixed test.pl so it will not stumble over an already installed Crypt::CBC - fixed bug in t/70-funcstyle.t which could in some cases let tests wrongly fail - test scripts: changed indentation, updated list of known ciphers v1.40, January 2005 - the '0' block problem still had survived in functions encrypt and decrypt... fixed - some code cleanup - changed versioning scheme to be more CPAN-like v1.3, January 2005 - no code changes, just some minor documentation update v1.2, January 2005 - fixed problems with blocks consisting of a '0' v1.1, December 2000 - first release Crypt-ECB-1.45/COPYING0000755000175600010010000003560610167502722013114 0ustar cappelKein GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONSCrypt-ECB-1.45/ECB.pm0000755000175600010010000004511011037357734013007 0ustar cappelKeinpackage Crypt::ECB; # Copyright (C) 2000, 2005, 2008 Christoph Appel (Christoph.Appel@t-systems.com) # see documentation for details ######################################## # general module startup things ######################################## use strict; use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(PADDING_NONE PADDING_AUTO); @EXPORT_OK = qw(encrypt decrypt encrypt_hex decrypt_hex); $VERSION = '1.45'; use constant PADDING_NONE => 0; use constant PADDING_AUTO => 1; ######################################## # public methods ######################################## # # constructor, initialization of vars # sub new ($;$$$) { my $class = shift; my $crypt = { # the following could be read by another program Caching => '1', # caching is on by default Padding => '0', # padding off by default Mode => '', Key => '', Cipher => '', Module => '', Keysize => '', Blocksize => '', Errstring => '', # the following should not be read by someone else buffer => '', # internal buffer used by crypt() and finish() # only used by _getcipher cipherobj => '', # contains the cipher object if caching oldKey => '', oldCipher => '', }; bless $crypt, $class; # the following is for compatibility with Crypt::CBC if (@_) { $crypt->key(shift); $crypt->cipher(shift || 'DES'); } return $crypt; } # # sets key if argument given, returns key # sub key (\$;$) { my $crypt = shift; $crypt->{Key} = shift if @_; return $crypt->{Key}; } # # sets caching, returns caching mode # sub caching (\$;$) { my $crypt = shift; $crypt->{Caching} = shift if @_; return $crypt->{Caching}; } # # sets padding mode, returns mode # sub padding (\$;$) { my $crypt = shift; $crypt->{Padding} = shift if @_; return $crypt->{Padding}; } # # sets and loads crypting module if argument given # returns module name # sub cipher (\$;$) { my $crypt = shift; if (my $cipher = shift) { # for compatibility with Crypt::CBC cipher modules # can be specified with the 'Crypt::' in front my $module = $cipher=~/^Crypt/ ? $cipher : "Crypt::$cipher"; eval "require $module"; if ($@) { $crypt->{Errstring} = "Couldn't load $module: $@" . "Are you sure '$cipher' is correct? If so," . " install $module in the proper path or" . " choose some other cipher.\n"; return 0; } # some packages like Crypt::DES and Crypt::IDEA behave # strange in the way that their methods do not belong to # Crypt::DES or Crypt::IDEA but only DES or IDEA instead unless ($module->can('blocksize')) { $module=$cipher } unless ($module->can('blocksize')) { $crypt->{Errstring} = "Can't work because Crypt::$cipher doesn't report" . " blocksize. Are you sure $cipher is a valid" . " crypting module?\n"; return 0; } $crypt->{Blocksize} = $module->blocksize; # In opposition to the blocksize, the keysize need not be # known by me, but by the one who provides the key. This # is because some modules (Crypt::Blowfish) report keysize # is 0, in other cases several keysizes are admitted, so # reporting just one number would anyway be to narrow $crypt->{Keysize} = $module->can('keysize') ? $module->keysize : ''; $crypt->{Cipher} = $cipher; $crypt->{Module} = $module; } $crypt->{Errstring} = ''; return $crypt->{Cipher}; } # # sets mode if argument given, either en- or decrypt # checks, whether all required vars are set # returns mode # sub start (\$$) { my $crypt = shift; my $mode = shift; unless ($mode=~/^[de]/i) { $crypt->{Errstring} = "Mode has to be either (e)ncrypt or (d)ecrypt.\n"; return 0; } $crypt->{Mode} = ($mode=~/^d/i) ? "decrypt" : "encrypt"; # checks whether $mode starts with either d or e, # only first character matters, rest of $mode is ignored unless ($crypt->{Key}) { $crypt->{Errstring} = "Key not set. Use '\$your_obj->key" . "('some_key'). The key length is probably specified" . " by the algorithm (for example the Crypt::Blowfish" . " module needs an eight byte key).\n"; return 0; } unless ($crypt->{Module}) { $crypt->{Errstring} = "Cipher not set." . " Use '\$your_obj->cipher(\$cipher)', \$cipher being" . " some algorithm like for example 'DES', 'IDEA' or" . " 'Blowfish'. The corresponding module 'Crypt::" . "\$cipher' has to be installed.\n"; return 0; } if ($crypt->{buffer}) { $crypt->{Errstring} = "Not yet finished existing crypting" . " process. Call finish() before calling start() anew.\n"; return 0; } $crypt->{Errstring} = ''; return $crypt->{Mode}; } # # calls the crypting module # returns the en-/decrypted data # sub crypt (\$;$) { my $crypt = shift; my $data = shift; $data = ($_ || '') unless length($data); my $errmsg = $crypt->{Errstring}; my $bs = $crypt->{Blocksize}; my $mode = $crypt->{Mode}; die $errmsg if $errmsg; unless ($mode) { die "You tried to use crypt() without calling start()" . " before. Use '\$your_obj->start(\$mode)' first," . " \$mode being one of 'decrypt' or 'encrypt'.\n"; } $data = $crypt->{buffer}.$data; # data is split into blocks of proper size which is reported # by the cipher module my @blocks = $data=~/(.{1,$bs})/gs; # last block goes into buffer $crypt->{buffer} = pop @blocks; my ($cipher, $text) = ($crypt->_getcipher, ''); $text .= $cipher->$mode($_) foreach (@blocks); return $text; } # # # sub finish (\$) { my $crypt = shift; my $errmsg = $crypt->{Errstring}; my $bs = $crypt->{Blocksize}; my $mode = $crypt->{Mode}; my $data = $crypt->{buffer}; my $result = ''; die $errmsg if $errmsg; unless ($mode) { die "You tried to use crypt() without calling start()" . " before. Use '\$your_obj->start(\$mode)' first," . " \$mode being one of 'decrypt' or 'encrypt'.\n"; } # cleanup: forget mode, purge buffer $crypt->{Mode} = ''; $crypt->{buffer} = ''; return '' unless defined($data); my $cipher = $crypt->_getcipher; # now we have to distinguish between en- and decrypting # when decrypting, data has to be truncated to correct size # when encrypting, data has to be padded up to blocksize if ($mode =~ /^d/i) { # pad data with binary 0 up to blocksize # in fact, this should not be necessary because correctly # encrypted data is always a multiple of the blocksize $data = pack("a$bs",$data); $result = $cipher->$mode($data); $result = $crypt->_truncate($result); } else { # if length is smaller than blocksize, just pad the block if (length($data) < $bs) { $data = $crypt->_pad($data); $result = $cipher->$mode($data); } # else append (if necessary) a full block else { $result = $cipher->$mode($data); $crypt->_pad('') && ($result .= $cipher->$mode($crypt->_pad(''))); } } return $result; } # # returns mode # sub mode (\$) { my $crypt = shift; return $crypt->{Mode}; } # # returns errstring # sub errstring (\$) { my $crypt = shift; return $crypt->{Errstring}; } ######################################## # private methods ######################################## # # truncates result to correct length # sub _truncate (\$$) { my $crypt = shift; my $result = shift; my $padstyle = $crypt->{Padding}; if ($padstyle == PADDING_NONE) { # no action } # PADDING_AUTO means padding as in Crypt::CBC elsif ($padstyle == PADDING_AUTO) { substr($result,-unpack("C",substr($result,-1))) = ''; } # some more for the future? else { die "Padding style '$padstyle' not defined.\n"; } return $result; } # # pad block to blocksize # sub _pad (\$$) { my $crypt = shift; my $data = shift; my $bs = $crypt->{Blocksize}; my $padstyle = $crypt->{Padding}; if ($padstyle == PADDING_NONE) { if (length($data) % $bs) { die "Your message length is not a multiple of" . " $crypt->{Cipher}'s blocksize ($bs bytes)." . " Correct this by hand or tell me to handle" . " padding.\n"; } } # PADDING_AUTO means padding (resp. truncating) as in Crypt::CBC elsif ($padstyle == PADDING_AUTO) { $data .= pack("C*",($bs-length($data))x($bs-length($data))); } # some more for the future? else { die "Padding style '$padstyle' not defined.\n"; } return $data; } # # returns a cipher object, handles caching # sub _getcipher (\$) { my $crypt = shift; if ($crypt->{Caching}) { # create a new cipher object if necessary unless ($crypt->{cipherobj} && $crypt->{oldKey} eq $crypt->{Key} && $crypt->{oldCipher} eq $crypt->{Cipher}) { $crypt->{cipherobj} = $crypt->{Module}->new($crypt->{Key}); $crypt->{oldKey} = $crypt->{Key}; $crypt->{oldCipher} = $crypt->{Cipher}; } return $crypt->{cipherobj}; } else { $crypt->{cipherobj} = ''; return $crypt->{Module}->new($crypt->{Key}); } } ######################################## # convenience functions/methods ######################################## # # magic decrypt/encrypt function/method # sub _xcrypt { my ($mode, $crypt, $key, $cipher, $data, $padstyle); if (ref($_[1]) =~ /^Crypt/) { ($mode, $crypt, $data) = @_; } else { ($mode, $key, $cipher, $data, $padstyle) = @_; $crypt = Crypt::ECB->new($key); $crypt->padding($padstyle || 0); $crypt->cipher($cipher) || die $crypt->errstring; $data = $_ unless length($data); } $crypt->start($mode) || die $crypt->errstring; my $text = $crypt->crypt($data) . $crypt->finish; return $text; } # # convenience encrypt and decrypt functions/methods # sub encrypt ($$;$$) { _xcrypt('encrypt', @_) } sub decrypt ($$;$$) { _xcrypt('decrypt', @_) } # # calls encrypt, returns hex packed data # sub encrypt_hex ($$;$$) { if (ref($_[0]) =~ /^Crypt/) { my $crypt = shift; join('',unpack('H*',$crypt->encrypt(shift))); } else { join('',unpack('H*',encrypt($_[0], $_[1], $_[2], $_[3]))); } } # # calls decrypt, expected input is hex packed # sub decrypt_hex ($$;$$) { if (ref($_[0]) =~ /^Crypt/) { my $crypt = shift; $crypt->decrypt(pack('H*',shift)); } else { decrypt($_[0], $_[1], pack('H*',$_[2]), $_[3]); } } ######################################## # finally, to satisfy require ######################################## 1; __END__ =head1 NAME Crypt::ECB - Encrypt Data using ECB Mode =head1 SYNOPSIS Use Crypt::ECB OO style use Crypt::ECB; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); $crypt->cipher('Blowfish') || die $crypt->errstring; $crypt->key('some_key'); $enc = $crypt->encrypt("Some data."); print $crypt->decrypt($enc); or use the function style interface use Crypt::ECB qw(encrypt decrypt encrypt_hex decrypt_hex); $ciphertext = encrypt($key, 'Blowfish', "Some data", PADDING_AUTO); $plaintext = decrypt($key, 'Blowfish', $ciphertext, PADDING_AUTO); $hexcode = encrypt_hex($key, $cipher, $plaintext); $plain = decrypt_hex($key, $cipher, $hexcode); =head1 DESCRIPTION This module is a Perl-only implementation of the ECB mode. In combination with a block cipher such as DES, IDEA or Blowfish, you can encrypt and decrypt messages of arbitrarily long length. Though for security reasons other modes than ECB such as CBC should be preferred. See textbooks on cryptography if you want to know why. The functionality of the module can be accessed via OO methods or via standard function calls. Remember that some crypting module like for example Blowfish has to be installed. The syntax follows that of Crypt::CBC. =head1 METHODS =head2 new(), key(), cipher(), padding() $crypt = Crypt::ECB->new; $crypt->key('Some_key'); $crypt->cipher('Blowfish') || die $crypt->errstring; $crypt->padding(PADDING_AUTO); print $crypt->key; print $crypt->cipher; print $crypt->padding; $crypt = Crypt::ECB->new('Some_key','Blowfish'); $crypt->cipher || die "'Blowfish' wasn't loaded for some reason."; B initializes the variables it uses. Optional parameters are key and cipher. If called without parameters you have to call B and B before you can start crypting. If called with key but without cipher, for compatibility with Crypt::CBC 'DES' is assumed. B sets the key if given a parameter. It always returns the key. Note that some crypting modules require keys of definite length. For example the Crypt::Blowfish module expects an eight byte key. B sets the block cipher to be used if given a parameter. It tries to load the corresponding module. If an error occurs, it returns 0 and sets $crypt->{Errstring}. Otherwise it returns the cipher name. Free packages available for Perl are for example Blowfish, DES or IDEA. If called without parameter it just returns the name of the cipher. B sets the way how data is padded up to a multiple of the cipher's blocksize. Until now two ways are implemented: When set to PADDING_NONE, no padding is done. You then have to take care of correct padding (and truncating) yourself. When set to PADDING_AUTO, the ECB module handles padding (and truncating when decrypting) the same way Crypt::CBC does. By default the padding style is set to PADDING_NONE. This means if you don't bother and your data has not the correct length, the module will complain and therefore force you to think about what you really want. =head2 start(), mode(), crypt(), finish() $crypt->start('encrypt') || die $crypt->errstring; $enc .= $crypt->crypt($_) foreach (@lines); $enc .= $crypt->finish; $crypt->start('decrypt'); print $crypt->mode; B sets the crypting mode and checks if all required variables like key and cipher are set. Allowed parameters are any words starting either with 'e' or 'd'. The Method returns the mode which is set or 0 if an error occurred. B is called without parameters and just returns the mode which is set. B processes the data given as argument. If called without argument $_ is processed. The method returns the processed data. Cipher and key have to be set in order to be able to process data. If some of these are missing or B was not called before, the method dies. After having sent all data to be processed to B you have to call B in order to flush data that's left in the buffer. =head2 caching() $crypt->caching(1); # caching on $crypt->caching(0); # caching off print $crypt->caching; The caching mode is returned. If given an argument caching mode is set. Caching is on if B evaluates true, otherwise caching is off. By default caching is on. What is this caching? The Crypt::ECB module communicates with the cipher module via some object. Creating the cipher object takes some time for the cipher module has to do some initialization. Now caching means that the same cipher object is used until caching is turned off or the key or the cipher module are changed. If caching is off, a new cipher object is created is created each time B or B are called and destroyed at the end of these methods. Crypting using caching is B faster than without caching. =head2 encrypt(), decrypt(), encrypt_hex(), decrypt_hex() $enc = $crypt->encrypt($data); print $crypt->decrypt($enc); $hexenc = $crypt->encrypt_hex($data); print $crypt->decrypt_hex($hexenc); B and B are convenience methods which call B, B and B for you. B and B are convenience functions that operate on ciphertext in a hexadecimal representation. They are exactly equivalent to $hexenc = join('',unpack('H*',$crypt->encrypt($data))); print $crypt->decrypt(pack('H*',$hexenc)); These functions can be useful if, for example, you wish to place the encrypted information into an e-mail message, Web page or URL. =head2 errstring() print $crypt->errstring; Some methods like B or B return 0 if an error occurs. You can then retrieve a more detailed error message by calling $crypt->errstring. =head1 VARIABLES Variables which could be of interest to the outside world are: $crypt->{Key}, $crypt->{Cipher}, $crypt->{Module}, $crypt->{Keysize}, $crypt->{Blocksize}, $crypt->{Mode}, $crypt->{Caching}, $crypt->{Padding}, $crypt->{Errstring}. The variables should not be set directly, use instead the above described methods. Reading should not pose a problem. =head1 CONSTANTS The two constants naming the padding styles are exported by default: PADDING_NONE => 0 PADDING_AUTO => 1 =head1 FUNCTIONS For convenience en- or decrypting can also be done by calling ordinary functions. The functions are: B, B, B, B. The module is smart enough to recognize whether these functions are called in an OO context or not. =head2 encrypt(), decrypt(), encrypt_hex(), decrypt_hex() $ciphertext = encrypt($key, $cipher, $plaintext, PADDING_AUTO); $plaintext = decrypt($key, $cipher, $ciphertext, PADDING_AUTO); $ciphertext = encrypt_hex($key, $cipher, $plaintext, PADDING_AUTO); $plaintext = decrypt_hex($key, $cipher, $ciphertext, PADDING_AUTO); B and B process the provided text and return either the corresponding ciphertext (encrypt) or plaintext (decrypt). Data and padstyle are optional, but remember that by default no padding is done. If data is omitted, $_ is assumed. B and B operate on ciphertext in a hexadecimal representation. Otherwise usage is the same as for B and B. =head1 BUGS None that I know of. =head1 TODO The other block cipher modes CBC, CFB and OFB could be implemented. Convenience encrypt and decrypt functions utilizing base64 encoding could be added. =head1 COPYING This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. =head1 AUTHOR Christoph Appel (see ECB.pm for email address) =head1 SEE ALSO perl(1), Crypt::DES(3), Crypt::IDEA(3), Crypt::CBC(3) =cut Crypt-ECB-1.45/Makefile.PL0000755000175600010010000000016110167502722014017 0ustar cappelKeinuse ExtUtils::MakeMaker; WriteMakefile( NAME => 'Crypt::ECB', VERSION_FROM => 'ECB.pm', ); Crypt-ECB-1.45/MANIFEST0000644000175600010010000000034611037357771013211 0ustar cappelKeinCHANGES COPYING ECB.pm Makefile.PL MANIFEST META.yml README t/10-encryption.t t/20-decryption.t t/30-padding.t t/40-substr.t t/50-caching.t t/60-nullblock-1.t t/65-nullblock-2.t t/70-funcstyle.t t/75-funcstyle-nullblock.t test.pl Crypt-ECB-1.45/META.yml0000644000175600010010000000044411037362353013320 0ustar cappelKein# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Crypt-ECB version: 1.45 version_from: ECB.pm installdirs: site requires: distribution_type: module generated_by: ExtUtils::MakeMaker version 6.30 Crypt-ECB-1.45/README0000755000175600010010000000321510167502722012730 0ustar cappelKeinModule Crypt::ECB ----------------- DESCRIPTION This module is a Perl-only implementation of the ECB mode. In combination with a block cipher such as DES, IDEA or Blowfish, you can encrypt and decrypt messages of arbitrarily long length. Though for security reasons other modes than ECB such as CBC should be preferred. See textbooks on cryptography if you want to know why. The functionality of the module can be accessed via OO methods or via standard function calls. Remember that some crypting module like for example Blowfish has to be installed. The syntax follows that of Crypt::CBC. INSTALLATION To install, just type perl Makefile.PL make make test make install If you are on a MS system, you possibly won't have make. But don't worry, for there is nothing to be compiled, installation is trivial: Just make a directory 'Crypt' in Perl's libpath if not existing. Then copy 'ECB.pm' to that directory, that's all. Perl's libpath will be something like 'c:\perl\site\lib'. COPYING This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Crypt-ECB-1.45/t/0000755000175600010010000000000011037362353012310 5ustar cappelKeinCrypt-ECB-1.45/t/10-encryption.t0000755000175600010010000000332611037362341015111 0ustar cappelKein#!/usr/bin/perl -w my %data = ( 'Blowfish', '0bc9ff2cfa46ba9f783a4cb66e2fea9b0b34a5d7cc73d74542db1a7afa9eb300', 'Blowfish_PP', '52a037af4a2aea2d10cc09183b433f1a12c5ce734067d597da040861fed3ae61', 'Camellia', '37adb2c1ba5a6be79c7b886cdd432853bd6dfa6eac8a02cd8a85174ecd17ed12', 'Camellia_PP', '37adb2c1ba5a6be79c7b886cdd432853bd6dfa6eac8a02cd8a85174ecd17ed12', 'CAST5', '811a469a643c4f1e9c0236ab1a76682bb918a95c33c7663203fb163df0eb264f', 'CAST5_PP', '811a469a643c4f1e9c0236ab1a76682bb918a95c33c7663203fb163df0eb264f', 'DES', 'a47b1b2c90fb3b7a7367c1844d3d07e620b943fdc6728a05e5cf69afe49da6e8', 'DES_PP', 'a47b1b2c90fb3b7a7367c1844d3d07e620b943fdc6728a05e5cf69afe49da6e8', 'IDEA', '58678df1889afedbd336fe64a6fb39ab08156a201f832e9a8a2fd460251ebe24', 'Rijndael', 'a7acc570d3d8fc33e215e369fbc3d6552cfb2c2bf39b5064d5310c0d32eedeb2', 'Rijndael_PP', 'a7acc570d3d8fc33e215e369fbc3d6552cfb2c2bf39b5064d5310c0d32eedeb2', # Twofish2 taken out for the moment, 'cause I'm not sure about the test data. # At least on some machines the test script reports an error. # 'Twofish2', '0958c674179aefaf13de8b25a613174dc40a90b80918bce55d314c86ecd3db45', ); print "1..", scalar(keys %data), "\n"; use Crypt::ECB; my ($crypt, $cipher, $ks, $enc, $test); my $text = "This is just some dummy text!\n"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (sort keys %data) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $enc = $crypt->encrypt_hex($text); print "not " unless $enc eq $data{$cipher}; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/20-decryption.t0000755000175600010010000000332611037362341015100 0ustar cappelKein#!/usr/bin/perl -w my %data = ( 'Blowfish', '0bc9ff2cfa46ba9f783a4cb66e2fea9b0b34a5d7cc73d74542db1a7afa9eb300', 'Blowfish_PP', '52a037af4a2aea2d10cc09183b433f1a12c5ce734067d597da040861fed3ae61', 'Camellia', '37adb2c1ba5a6be79c7b886cdd432853bd6dfa6eac8a02cd8a85174ecd17ed12', 'Camellia_PP', '37adb2c1ba5a6be79c7b886cdd432853bd6dfa6eac8a02cd8a85174ecd17ed12', 'CAST5', '811a469a643c4f1e9c0236ab1a76682bb918a95c33c7663203fb163df0eb264f', 'CAST5_PP', '811a469a643c4f1e9c0236ab1a76682bb918a95c33c7663203fb163df0eb264f', 'DES', 'a47b1b2c90fb3b7a7367c1844d3d07e620b943fdc6728a05e5cf69afe49da6e8', 'DES_PP', 'a47b1b2c90fb3b7a7367c1844d3d07e620b943fdc6728a05e5cf69afe49da6e8', 'IDEA', '58678df1889afedbd336fe64a6fb39ab08156a201f832e9a8a2fd460251ebe24', 'Rijndael', 'a7acc570d3d8fc33e215e369fbc3d6552cfb2c2bf39b5064d5310c0d32eedeb2', 'Rijndael_PP', 'a7acc570d3d8fc33e215e369fbc3d6552cfb2c2bf39b5064d5310c0d32eedeb2', # Twofish2 taken out for the moment, 'cause I'm not sure about the test data. # At least on some machines the test script reports an error. # 'Twofish2', '0958c674179aefaf13de8b25a613174dc40a90b80918bce55d314c86ecd3db45', ); print "1..", scalar(keys %data), "\n"; use Crypt::ECB; my ($crypt, $cipher, $ks, $dec, $test); my $text = "This is just some dummy text!\n"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (sort keys %data) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $dec = $crypt->decrypt_hex($data{$cipher}); print "not " unless $dec eq $text; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/30-padding.t0000755000175600010010000000163211037362341014325 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $ks, $len, $nok, $enc, $dec, $test); my $text = "This is just some dummy text!\n"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $nok = 0; $crypt->padding(PADDING_AUTO); $enc = $crypt->encrypt($text); $nok++ unless $crypt->decrypt($enc) eq $text; $crypt->padding(PADDING_NONE); $nok++ unless $crypt->decrypt($enc) eq $text."\x02\x02"; print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/40-substr.t0000755000175600010010000000162311037362341014242 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $ks, $len, $nok, $enc, $dec, $test); my $text = "This is just some dummy text!\n"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $nok = 0; foreach $len (0..length($text)) { $enc = $crypt->encrypt(substr($text,0,$len)); $dec = $crypt->decrypt($enc); $nok++ unless substr($text,0,$len) eq $dec; } print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/50-caching.t0000755000175600010010000000203211037362341014310 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $ks, $len, $nok, $enc, $dec, $test); my $text = "This is just some dummy text!\n"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $nok = 0; foreach (1..3) { $crypt->caching( 1-($crypt->caching) ); # toggle caching $enc = $crypt->encrypt($text); # if caching off, cipherobj must be empty, and vice versa $nok++ if $crypt->caching ^ ($crypt->{cipherobj} ne ''); $dec = $crypt->decrypt($enc); $nok++ unless $text eq $dec; } print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/60-nullblock-1.t0000755000175600010010000000144111037362341015043 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $ks, $len, $nok, $enc, $dec, $test); my $text = "0"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $nok = 0; $enc = $crypt->encrypt($text); $nok++ unless $crypt->decrypt($enc) eq $text; print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/65-nullblock-2.t0000755000175600010010000000145111037362341015052 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $ks, $len, $nok, $enc, $dec, $test); my $text = "123456780"; my $key = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $crypt->key(substr($key,0,$ks)); $nok = 0; $enc = $crypt->encrypt($text); $nok++ unless $crypt->decrypt($enc) eq $text; print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/70-funcstyle.t0000755000175600010010000000211211037362341014731 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB qw(encrypt decrypt PADDING_AUTO); my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $key, $ks, $len, $nok, $enc1, $enc2, $dec1, $dec2, $test); my $text = "This is just some dummy text!\n"; my $xkey = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $key = substr($xkey, 0, $ks); $crypt->key($key); $nok = 0; $enc1 = $crypt->encrypt($text); $enc2 = encrypt($key, $cipher, $text, PADDING_AUTO); $nok++ unless ($enc1 eq $enc2); $dec1 = $crypt->decrypt($enc1); $dec2 = decrypt($key, $cipher, $enc2, PADDING_AUTO); $nok++ unless ($dec1 eq $text); $nok++ unless ($dec2 eq $text); print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/t/75-funcstyle-nullblock.t0000755000175600010010000000205411037362341016726 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB qw(encrypt decrypt PADDING_AUTO); my @ciphers = ( 'Blowfish', 'Blowfish_PP', 'Camellia', 'Camellia_PP', 'CAST5', 'CAST5_PP', 'DES', 'DES_PP', 'IDEA', 'Rijndael', 'Rijndael_PP', 'Twofish2', ); print "1..", $#ciphers+1, "\n"; my ($crypt, $cipher, $key, $ks, $len, $nok, $enc1, $enc2, $dec1, $dec2, $test); my $text = "0"; my $xkey = "This is an at least 56 Byte long test key!!! It really is."; $crypt = Crypt::ECB->new; $crypt->padding(PADDING_AUTO); foreach $cipher (@ciphers) { unless ($crypt->cipher($cipher)) { print "ok ".(++$test)." # skip, $cipher not installed\n"; next; } $ks = ($crypt->{Keysize} or 8); $key = substr($xkey, 0, $ks); $crypt->key($key); $nok = 0; $enc1 = $crypt->encrypt($text); $enc2 = encrypt($key, $cipher, $text, PADDING_AUTO); $nok++ unless ($enc1 eq $enc2); $dec1 = $crypt->decrypt($enc1); $dec2 = decrypt($key, $cipher, $enc2, PADDING_AUTO); $nok++ unless ($dec1 eq $text); $nok++ unless ($dec2 eq $text); print "not " if $nok; print "ok ".(++$test)."\n"; } Crypt-ECB-1.45/test.pl0000755000175600010010000000103411037352761013364 0ustar cappelKein#!/usr/bin/perl -w use Crypt::ECB; my ($path, $retval, $ok); my $crypt = Crypt::ECB->new; print "Checking your perl installation for cipher modules:\n"; foreach $path (@INC) { while (<$path/Crypt/*.pm>) { s|^.*Crypt/||; s|\.pm$||; $retval = 0; eval { $retval = $crypt->cipher($_) }; print " Found $_.\n" if $retval and ++$ok; } } unless ($ok) { print "There are no crypt modules installed that I know of." . " Attention: Crypt::ECB will not be of any use to you" . " unless you install some cipher module(s).\n"; }