| 1 | package Crypt::Mode::ECB; | ||
| 2 | |||
| 3 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | ||
| 4 | |||
| 5 | 25 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 25 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 25 | our $VERSION = '0.091'; | |
| 8 | |||
| 9 | 25 | ▶ | use Crypt::Cipher; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 66 | ▶ | sub encrypt { |
|
Subroutine
encrypt: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | 66 | my ($self, $pt) = (shift, shift); | |
| 13 | 66 | local $SIG{__DIE__} = \&CryptX::_croak; | |
| 14 | 66 | $self->start_encrypt(@_)->add($pt) . $self->finish; | |
| 15 | } | ||
| 16 | |||
| 17 | 66 | ▶ | sub decrypt { |
|
Subroutine
decrypt: called CC=1 SCAR=0.0
Pod
documented
| |||
| 18 | 66 | my ($self, $ct) = (shift, shift); | |
| 19 | 66 | local $SIG{__DIE__} = \&CryptX::_croak; | |
| 20 | 66 | $self->start_decrypt(@_)->add($ct) . $self->finish; | |
| 21 | } | ||
| 22 | |||
| 23 | 0 | ▶ | sub CLONE_SKIP { 1 } # prevent cloning |
|
Subroutine
CLONE_SKIP: not called CC=1 SCAR=6.9
| |||
| 24 | |||
| 25 | 25 | 1; | |
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | Crypt::Mode::ECB - Block cipher mode ECB [Electronic codebook] | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | use Crypt::Mode::ECB; | ||
| 36 | my $m = Crypt::Mode::ECB->new('AES'); | ||
| 37 | my $key = '1234567890123456'; | ||
| 38 | my $plaintext = 'example plaintext'; | ||
| 39 | my $chunk1 = 'example '; | ||
| 40 | my $chunk2 = 'plaintext'; | ||
| 41 | |||
| 42 | # encrypt or decrypt in one call | ||
| 43 | my $single_ciphertext = $m->encrypt($plaintext, $key); | ||
| 44 | my $single_plaintext = $m->decrypt($single_ciphertext, $key); | ||
| 45 | |||
| 46 | # encrypt more chunks | ||
| 47 | $m->start_encrypt($key); | ||
| 48 | my $chunked_ciphertext = ''; | ||
| 49 | $chunked_ciphertext .= $m->add($chunk1); | ||
| 50 | $chunked_ciphertext .= $m->add($chunk2); | ||
| 51 | $chunked_ciphertext .= $m->finish; | ||
| 52 | |||
| 53 | # decrypt more chunks | ||
| 54 | $m->start_decrypt($key); | ||
| 55 | my $chunked_plaintext = ''; | ||
| 56 | $chunked_plaintext .= $m->add($chunked_ciphertext); | ||
| 57 | $chunked_plaintext .= $m->finish; | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | This module implements ECB cipher mode. B<Note:> It works only with ciphers from L<CryptX> (Crypt::Cipher::NNNN). | ||
| 62 | B<BEWARE: ECB is insecure by design>, if you are not sure go for L<Crypt::Mode::CBC>! | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | Unless noted otherwise, assume C<$m> is an existing mode object created via | ||
| 67 | C<new>, for example: | ||
| 68 | |||
| 69 | my $m = Crypt::Mode::ECB->new('AES'); | ||
| 70 | |||
| 71 | =head2 new | ||
| 72 | |||
| 73 | my $m = Crypt::Mode::ECB->new($name); | ||
| 74 | #or | ||
| 75 | my $m = Crypt::Mode::ECB->new($name, $padding); | ||
| 76 | #or | ||
| 77 | my $m = Crypt::Mode::ECB->new($name, $padding, $cipher_rounds); | ||
| 78 | |||
| 79 | # $name ....... [string] one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE', | ||
| 80 | # 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6', | ||
| 81 | # 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64', | ||
| 82 | # 'SEED', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent' | ||
| 83 | # or any <NAME> for which there is a Crypt::Cipher::<NAME> module | ||
| 84 | # $padding .... [integer] 0 no padding (plaintext size has to be multiple of block length) | ||
| 85 | # 1 PKCS5 padding, Crypt::CBC's "standard" - DEFAULT | ||
| 86 | # 2 Crypt::CBC's "oneandzeroes" | ||
| 87 | # 3 ANSI X.923 padding | ||
| 88 | # 4 zero padding | ||
| 89 | # 5 zero padding (+a block of zeros if the output length is divisible by the blocksize) | ||
| 90 | # $cipher_rounds ... [integer] optional, number of rounds for given cipher | ||
| 91 | |||
| 92 | =head2 encrypt | ||
| 93 | |||
| 94 | Encrypts the plaintext in a single call. Returns the ciphertext as a binary string. | ||
| 95 | The plaintext scalar is converted to bytes using Perl's usual scalar | ||
| 96 | stringification. Defined scalars, including numbers and string-overloaded | ||
| 97 | objects, are accepted. C<undef> is treated as an empty string and may emit | ||
| 98 | Perl's usual "uninitialized value" warning. | ||
| 99 | |||
| 100 | my $ciphertext = $m->encrypt($plaintext, $key); | ||
| 101 | |||
| 102 | =head2 decrypt | ||
| 103 | |||
| 104 | Decrypts the ciphertext in a single call. Returns the plaintext as a binary string. | ||
| 105 | The ciphertext scalar is converted to bytes using Perl's usual scalar | ||
| 106 | stringification. Defined scalars, including numbers and string-overloaded | ||
| 107 | objects, are accepted. C<undef> is treated as an empty string and may emit | ||
| 108 | Perl's usual "uninitialized value" warning. | ||
| 109 | |||
| 110 | my $plaintext = $m->decrypt($ciphertext, $key); | ||
| 111 | |||
| 112 | =head2 start_encrypt | ||
| 113 | |||
| 114 | Initializes encryption mode. Returns the object itself. | ||
| 115 | |||
| 116 | $m->start_encrypt($key); | ||
| 117 | |||
| 118 | =head2 start_decrypt | ||
| 119 | |||
| 120 | Initializes decryption mode. Returns the object itself. | ||
| 121 | |||
| 122 | $m->start_decrypt($key); | ||
| 123 | |||
| 124 | =head2 add | ||
| 125 | |||
| 126 | Feeds data to the encryption or decryption stream. Returns a binary string. | ||
| 127 | |||
| 128 | Each argument is converted to bytes using Perl's usual scalar stringification. | ||
| 129 | Defined scalars, including numbers and string-overloaded objects, are | ||
| 130 | accepted. C<undef> is treated as an empty string and may emit Perl's usual | ||
| 131 | "uninitialized value" warning. | ||
| 132 | |||
| 133 | # in encrypt mode | ||
| 134 | my $ciphertext = $m->add($plaintext); | ||
| 135 | |||
| 136 | # in decrypt mode | ||
| 137 | my $plaintext = $m->add($ciphertext); | ||
| 138 | |||
| 139 | =head2 finish | ||
| 140 | |||
| 141 | Finalizes the stream. In encryption mode, pads and encrypts the final block; | ||
| 142 | in decryption mode, decrypts and removes padding from the final block. | ||
| 143 | Returns the result as a binary string (may be empty if there is no data to flush). | ||
| 144 | |||
| 145 | # encrypt more chunks | ||
| 146 | $m->start_encrypt($key); | ||
| 147 | my $chunk1 = 'example '; | ||
| 148 | my $chunk2 = 'plaintext'; | ||
| 149 | my $ciphertext = ''; | ||
| 150 | $ciphertext .= $m->add($chunk1); | ||
| 151 | $ciphertext .= $m->add($chunk2); | ||
| 152 | $ciphertext .= $m->finish; | ||
| 153 | |||
| 154 | # decrypt more chunks | ||
| 155 | $m->start_decrypt($key); | ||
| 156 | my $plaintext = ''; | ||
| 157 | $plaintext .= $m->add($ciphertext); | ||
| 158 | $plaintext .= $m->finish; | ||
| 159 | |||
| 160 | =head1 SEE ALSO | ||
| 161 | |||
| 162 | =over | ||
| 163 | |||
| 164 | =item * L<CryptX>, L<Crypt::Cipher> | ||
| 165 | |||
| 166 | =item * L<Crypt::Cipher::AES>, L<Crypt::Cipher::Blowfish>, ... | ||
| 167 | |||
| 168 | =item * L<https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_codebook_.28ECB.29> | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =cut | ||