?
Solved

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

Posted on 2003-04-01
12
Medium Priority
?
212 Views
Last Modified: 2010-03-05
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
Comment
Question by:ibwan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
12 Comments
 
LVL 6

Expert Comment

by:holli
ID: 8247183
looks correct. what exactly is your problem?
0
 

Author Comment

by:ibwan
ID: 8250595
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
 
LVL 6

Expert Comment

by:holli
ID: 8251746
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
Technology Partners: 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!

 

Author Comment

by:ibwan
ID: 8503107
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
 
LVL 6

Expert Comment

by:holli
ID: 8503239
will i have to wait another 5 weeks if i respond to the new question?
0
 

Author Comment

by:ibwan
ID: 8503256
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
 

Author Comment

by:ibwan
ID: 8503285
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
 

Author Comment

by:ibwan
ID: 8507748
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
 
LVL 6

Accepted Solution

by:
holli earned 2000 total points
ID: 8510713
post the code you have, i will have a look.
0
 

Author Comment

by:ibwan
ID: 8515039
#---- 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
 
LVL 20

Expert Comment

by:jmcg
ID: 9708165
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question