| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Crypt::TripleDES::CBC; | ||||||||||
| 5 | |||||||||||
| 6 | # PODNAME: Crypt::TripleDES::CBC | ||||||||||
| 7 | # ABSTRACT: Triple DES in CBC mode Pure implementation | ||||||||||
| 8 | # | ||||||||||
| 9 | # This file is part of Crypt-TripleDES-CBC | ||||||||||
| 10 | # | ||||||||||
| 11 | # This software is copyright (c) 2016 by Shantanu Bhadoria. | ||||||||||
| 12 | # | ||||||||||
| 13 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 14 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 15 | # | ||||||||||
| 16 | 2 | our $VERSION = '0.006'; # VERSION | |||||||||
| 17 | |||||||||||
| 18 | # Dependencies | ||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use Crypt::DES; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 2 | has cipher1 => ( | |||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | lazy_build => 1, | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 4 | ▶ | sub _build_cipher1 { | ||||||||
|
Subroutine
_build_cipher1: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 4 | my ($self) = @_; | |||||||||
| 32 | 4 | my $cipher = new Crypt::DES( substr( $self->key, 0, 8 ) ); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 2 | has cipher2 => ( | |||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | lazy_build => 1, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 4 | ▶ | sub _build_cipher2 { | ||||||||
|
Subroutine
_build_cipher2: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 4 | my ($self) = @_; | |||||||||
| 43 | 4 | my $cipher = new Crypt::DES( substr( $self->key, 8 ) ); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | 2 | has key => ( | |||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | required => 1, | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 2 | has iv => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | required => 1, | ||||||||||
| 56 | default => pack( "H*", "0000000000000000" ), | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | 2 | ▶ | sub encrypt { | ||||||||
|
Subroutine
encrypt: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | 2 | my ( $self, $cleartext ) = @_; | |||||||||
| 62 | 2 | my $length = length($cleartext); | |||||||||
| 63 | 2 | my $result = ''; | |||||||||
| 64 | 2 | my $iv = $self->iv; | |||||||||
| 65 | 2 | ▶ | while ( $length > 8 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 36 | my $block = substr( $cleartext, 0, 8 ); | |||||||||
| 67 | 36 | $cleartext = substr( $cleartext, 8 ); | |||||||||
| 68 | 36 | my $ciphertext = $self->_encrypt_3des( $block ^ $iv ); | |||||||||
| 69 | 36 | $result .= $ciphertext; | |||||||||
| 70 | 36 | $iv = $ciphertext; | |||||||||
| 71 | 36 | $length = length($cleartext); | |||||||||
| 72 | } | ||||||||||
| 73 | 2 | my $ciphertext = $self->_encrypt_3des( $cleartext ^ $iv ); | |||||||||
| 74 | 2 | $result .= $ciphertext; | |||||||||
| 75 | 2 | return $result; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | 2 | ▶ | sub decrypt { | ||||||||
|
Subroutine
decrypt: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 80 | 2 | my ( $self, $ciphertext ) = @_; | |||||||||
| 81 | 2 | my $length = length($ciphertext); | |||||||||
| 82 | 2 | my $result = ''; | |||||||||
| 83 | 2 | my $iv = $self->iv; | |||||||||
| 84 | 2 | ▶ | while ( $length > 8 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 36 | my $block = substr( $ciphertext, 0, 8 ); | |||||||||
| 86 | 36 | $ciphertext = substr( $ciphertext, 8 ); | |||||||||
| 87 | 36 | my $cleartext = $self->_decrypt_3des($block); | |||||||||
| 88 | 36 | $result .= $cleartext ^ $iv; | |||||||||
| 89 | 36 | $iv = $block; | |||||||||
| 90 | 36 | $length = length($ciphertext); | |||||||||
| 91 | } | ||||||||||
| 92 | 2 | my $cleartext = $self->_decrypt_3des($ciphertext); | |||||||||
| 93 | 2 | $result .= $cleartext ^ $iv; | |||||||||
| 94 | 2 | return $result; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 38 | ▶ | sub _encrypt_3des { | ||||||||
|
Subroutine
_encrypt_3des: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 38 | my ( $self, $plaintext ) = @_; | |||||||||
| 99 | 38 | return $self->cipher1->encrypt( | |||||||||
| 100 | $self->cipher2->decrypt( $self->cipher1->encrypt($plaintext) ) ); | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 38 | ▶ | sub _decrypt_3des { | ||||||||
|
Subroutine
_decrypt_3des: called CC=1 SCAR=0.0
| |||||||||||
| 104 | 38 | my ( $self, $ciphertext ) = @_; | |||||||||
| 105 | 38 | return $self->cipher1->decrypt( | |||||||||
| 106 | $self->cipher2->encrypt( $self->cipher1->decrypt($ciphertext) ) ); | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 2 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||