Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Could some one help to check either the script MIME::Base64 (encode and decode) below in the right format or not

use MIME::Base64;
use Crypt::CBC;
use vars '$CIPHER';
my $secret = "cryp1234";

&des64_encrypt('testing123'); # encrypt
#or
&des64_decrypt('testing123'); # decrypt

sub des64_decrypt
{
my $data = shift;
$CIPHER = Crypt::CBC->new({'key' => $secret,'cipher' =>'DES','regenerate_key' => 0, 'prepend_iv' => 0});
return $CIPHER->decrypt(decode_base64($data));
}

sub des64_encrypt
{
my $data = shift;
$CIPHER = Crypt::CBC->new({'key' => $secret,'cipher' =>'DES','regenerate_key' => 0, 'prepend_iv' => 0});
my $value = $CIPHER->encrypt($data);
return encode_base64($value);
}

0
ibwan
Asked:
ibwan
  • 6
  • 4
1 Solution
 
holliCommented:
looks correct. what exactly is your problem?
0
 
ibwanAuthor Commented:
the problem as below:

this is the string that suppose to encoded
passwd before encryption = ebiz12345
after encryption = MOzA5zgDZKSxIKAMVXKyMA==

but when i run the encoded engine the result as below and it keep changing every time it running.

7yyGzcdIOyoAymooHW3hjg==
q5lCpl4SwEZZTjN/ffFZJw==
Pu6jSZU+tE6ZTCk4y/AENA==
ASwc+ISvVgODih933J6XRQ==
deLnWLWz03gqUncBSUAANQ==

ok, let say because of the $secret parameter. But when i decode "7yyGzcdIOyoAymooHW3hjg==" the result as follow: "ëòc~*5"
(supposely "ebiz12345")

could some explained detail or if you could give some example script on how to do encryption as above
0
 
holliCommented:
use strict;
use MIME::Base64;
use Crypt::CBC;

my ($secret, $test) = ("cryp1234","testing123");
my $cipher = Crypt::CBC->new({'key' => $secret,'cipher' =>'DES','regenerate_key' => 0, 'prepend_iv' => 0});

my $encription = &des64_encrypt('testing123'); # encrypt
print "encrypting and encoding $test: $encription\n";

my $decription = &des64_decrypt($encription); # decrypt
print "decrypting and decoding $encription: $decription\n";

sub des64_decrypt { return $cipher->decrypt(decode_base64($_[0])); }
sub des64_encrypt { return encode_base64($cipher->encrypt($_[0])); }



this produces the following output:

encrypting and encoding testing123: VEC03enjjjxRBYtlNXX7Yg==

decrypting and decoding VEC03enjjjxRBYtlNXX7Yg==
: testing123
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ibwanAuthor Commented:
thanks for your good example,

but this program you running in one time. how about we save the encryt output in to file. later if we wanna view, we just open the file and decrypt the content.

I've try it but it's not working. What should i do?

Acctually i want to crypt my images and save it into file. later i open it and view it. Or is there any idea instead using this idea. i need your help
0
 
holliCommented:
will i have to wait another 5 weeks if i respond to the new question?
0
 
ibwanAuthor Commented:
bro don't say like that..

actually we are in the midst of tranfering our new equipment to the new building. and we got problem with the internet connection. Just couple of day we resolve the problem.

By the way, thanks for your response and i'm still need your suggestion and positive answer. sorry for your Inconvenient
0
 
ibwanAuthor Commented:
bro don't say like that..

actually we are in the midst of tranfering our new equipment to the new building. and we got problem with the internet connection. Just couple of day we resolve the problem.

By the way, thanks for your response and i'm still need your suggestion and positive answer. sorry for your Inconvenient
0
 
ibwanAuthor Commented:
dear holli,

i got 3 different situation to determine the problem on MIME::Base64 i've used. Could you help me please..

1. encode and strightly decode in the same programs
output : working fine

2. encode and store it into file. then read the file and encode. all this thing run in the same time in one program.
output : working fine

3. encode and store it into file. there's other program to open and decode the text.
output : not working

could you help me to xplain what was happening actually.
0
 
holliCommented:
post the code you have, i will have a look.
0
 
ibwanAuthor Commented:
#---- base64.pl --------------
# this is main program to do encrypt or decrypt

use strict;
use MIME::Base64;
use Crypt::CBC;

my $secret = "fuji6381";
my $cipher = Crypt::CBC->new({'key' => $secret,'cipher' =>'DES','regenerate_key' => 0, 'prepend_iv' => 0});

sub base64_decrypt{
        my $value = shift;
        return $cipher->decrypt(decode_base64($value));
}

sub base64_encrypt{
        my $value = shift;
        return encode_base64($cipher->encrypt($value));

}

#---------------------------------
#------ encrypt.pl ---------------
# once this program run it will encrypt and save it
# into file, then it will read back the file and
# decrypt the content and print out the output
# in the same program.

#!/usr/bin/perl

require "base64.pl";

my $file = "base64.dat";

#-------- encode text and save into file ------
#----------------------------------------------

my $text = "ntahapeapentahkaninabolula";
my $encode = &base64_encrypt($text);
&fdbsave($encode,$file);

#-------- read file and decode text  ------
#----------------------------------------------

my $data = &filedbread($file);
chomp $data;
$decode = &base64_decrypt($data);

#-------- print out ---------------

print "content-type:text/html\n\n";
print $text." ->".$encode;
print $data." ->".$decode;

#---------------------------------

sub fdbsave {
        my $file = pop;

        open (DBSAVE, ">$file") || goto ERR;
        flock (DBSAVE, 2);
        print DBSAVE @_;
        close (DBSAVE);

        undef @_;
        return 1;

ERR:
        undef @_;
        return 0;
}

sub filedbread {
        my $file          = shift;

        if (!-e $file) {
                return "Database not found";
        }

        local *DBREAD;

        ## Whole db
        open (DBREAD, "<$file") || goto ERR;
        flock (DBREAD, 1) || goto ERR;
        my $alldata;
        while(<DBREAD>) { $alldata.= $_; }
        close DBREAD;
        return $alldata;

ERR:
        close DBREAD;
        print "$file cannot open ";
        return undef;
}

#---------------------------------
#------ decrypt.pl ---------------
# once this program run it will read the file and
# decrypt the content and print out the output
# in the same program.

#!/usr/bin/perl

require "lib/files/base64.pl";

print "content-type:text/html\n\n";

my $file = "base64.dat";
my $data = &filedbread($file);
chomp $data;

$decrypt = &base64_decrypt($data);
print $data." ->".$decrypt;

sub filedbread {
        my $file          = shift;

        if (!-e $file) {
                return "Database not found";
        }

        local *DBREAD;

        ## Whole db
        open (DBREAD, "<$file") || goto ERR;
        flock (DBREAD, 1) || goto ERR;
        my $alldata;
        while(<DBREAD>) { $alldata.= $_; }
        close DBREAD;
        return $alldata;

ERR:
        close DBREAD;
        print "$file cannot open ";
        return undef;
}
0
 
jmcgOwnerCommented:
Nothing has happened on this question in over 5 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by holli.

Please post any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now