[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创教程] [Perl]Crypt::CBC - 加密解密模块

本帖最后由 523066680 于 2017-12-21 11:18 编辑

示例:

      use Modern::Perl;
      use Data::Dump qw/dump/;
      use Crypt::CBC;
      my $cipher = Crypt::CBC->new(
                          -key    => 'password',
                          -cipher => 'Blowfish',
                          -header => 'randomiv',
                     );

      my $ciphertext = $cipher->encrypt("This data is hush hush");
      my $plaintext  = $cipher->decrypt($ciphertext);
      say unpack("H*", $ciphertext);
      say $plaintext;

Straberry Perl PDL 版本自带 Crypt 相关模块(Strawberry/perl/vendor/lib/crypt),
通常你可以直接使用 Crypt::RSA,Crypt::Blowfish 等模块,
但是这些模块需要手动对数据、密钥进行固定字节的对齐,而Crypt::CBC模块对这些细节做了自动处理。

-cipher 是加密/解密方案,支持 DSA,RSA,IDEA,DES,Blowfish,Twofish 等加密算法
-header 可以是 randomiv, salt, none 三种模式,默认情况下 salt 和 randomiv 都是随机取值。

在 $cipher 对象建立之后,可以重新设置 salt 和 randomiv,也可以通过 $cipher->salt() 的方式获取其值。

送一个示例,终端输入密码(显示星号),将脚本自身加密再解密:

      =info
          Author: 523066680
          Date: 2017-12-20
      =cut

      use Modern::Perl;
      use Win32::Console;
      use Crypt::CBC;
      use File::Slurp;
      use Time::HiRes qw/sleep/;
      use IO::Handle;
      STDOUT->autoflush(1);

      our $IN=Win32::Console->new(STD_INPUT_HANDLE);
      our $OUT=Win32::Console->new(STD_OUTPUT_HANDLE);
      our $IN_DEFAULT = $IN->Mode();
      $IN->Mode(ENABLE_MOUSE_INPUT);

      my $key = get_password();
      cipher("encrypt", $key, __FILE__ ,             __FILE__ ."_enc.txt", );
      cipher("decrypt", $key, __FILE__ ."_enc.txt" , __FILE__ ."_dec.txt", );
      pause();

      sub get_password
      {
          my @st;
          my $inp = "";
          print "Password:";
          while (1)
          {
              sleep 0.01;
              @st = $IN->Input();
              next if ($#st < 0);
              if ( $st[0] == 1 and $st[1] == 1 )
              {
                  if ( chr($st[5]) =~ /[\x20-\x7e]/ ) { print "*"; $inp .= chr($st[5]) }
                  elsif ( $st[5] == 27 ) { exit }
                  elsif ( $st[5] == 13 ) { print "\n"; last   }
                  elsif ( $st[5] == 8 )  { backspace( \$inp ) }
              }
          }
          return $inp;
      }

      sub backspace
      {
          my $s_ref = shift;
          if ( length($$s_ref) > 0 )
          {
              print "\b \b";
              $$s_ref =~ s/.$//;
          }
      }

      sub pause
      {
          print "Press any key to continue ...";
          my @st;
          while (1)
          {
              sleep 0.02;
              @st = $IN->Input();
              next if ($#st < 0);
              exit if ( $st[0] == 1 and $st[1] == 1 );
          }
      }

      sub cipher
      {
          my ($mode, $key, $src, $dst) = @_;
          say "${mode}ing";
          my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish' );
          my $stream = read_file( $src, binmode => ":raw" );
          my $result = $cipher->$mode( $stream );
          write_file( $dst, { binmode => ":raw" }, $result );
      }
[Finished in 0.4s]

返回列表