Files
selima-perl/lib/perl5/Selima/Encrypt.pm
2026-03-10 21:31:43 +08:00

63 lines
1.7 KiB
Perl

# Selima Website Content Management System
# Encrypt.pm: The simple symmetric encrypter/decrypter.
# Copyright (c) 2004-2018 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: imacat <imacat@mail.imacat.idv.tw>
# First written: 2004-09-26
package Selima::Encrypt;
use 5.008;
use strict;
use warnings;
use base qw(Exporter);
use vars qw(@EXPORT @EXPORT_OK);
BEGIN {
@EXPORT = qw(encrypt decrypt);
@EXPORT_OK = @EXPORT;
# Prototype declaration
sub encrypt($);
sub decrypt($);
}
use Crypt::Rijndael qw();
use MIME::Base64 qw(encode_base64 decode_base64);
use vars qw($cipher);
$cipher = new Crypt::Rijndael(decode_base64("MDkxcTc3dGNvMkRZTTYxM0dqRmZ2R2xvQmNLcUZkNVo="));
# This use symmetric encryption/decryption. It is not safe.
# Nothing is exported. Use it by the full package name.
# encrypt: Encrypt
sub encrypt($) {
local ($_, %_);
$_ = $_[0];
$_ = length($_) . " " . $_;
# Pad with US-ASCII printable characters
$_ .= chr(32 + int rand 95) while length($_) % 16 != 0;
return encode_base64($cipher->encrypt($_));
}
# decrypt: Decrypt
sub decrypt($) {
local ($_, %_);
$_ = $cipher->decrypt(decode_base64($_[0]));
s/^(\d+) //;
return substr $_, 0, $1;
}
return 1;