Guest User

Untitled

a guest
Sep 11th, 2024
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.97 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. #
  4. # pine-passfile-decode -- encode or decode a decrypted, obfuscated (al)pine
  5. # passfile
  6. #
  7. # The encode/decode routines are lifted verbatim from alpine/alpine/imap.c
  8. #
  9. # Since alpine-2.10(ish?) the passfile is encrypted with an x509 cert; use
  10. # openssl smime to encrypt/decrypt.
  11. #
  12. # Decrypt like so:
  13. # openssl smime -decrypt -inkey ~/.alpine-smime/.pwd/MasterPassword.key -in ~/.alpine.pwd -inform pem | pine-passfile-decode -m decrypt
  14. #
  15. # Encrypt like so:
  16. # pine-passfile-decode -m encrypt | openssl smime -decrypt -inkey .alpine-smime/.pwd/MasterPassword.key -in .alpine.pwd -inform pem
  17. #
  18.  
  19. use strict;
  20. use Getopt::Std;
  21.  
  22. our $FIRSTCH=32;
  23. our $LASTCH=126;
  24. our $TABSZ=($LASTCH - $FIRSTCH + 1);
  25. our $xlate_key=0;
  26.  
  27. sub xlate_in($){
  28.     my $c=ord($_[0]);
  29.     my $eti;
  30.     $eti = $xlate_key;
  31.     if(($c >= $FIRSTCH) && ($c <= $LASTCH)){
  32.         $eti += ($c - $FIRSTCH);
  33.         $eti -= ($eti >= 2*$TABSZ) ? 2*$TABSZ : ($eti >= $TABSZ) ? $TABSZ : 0;
  34.         return chr((($xlate_key = $eti) + $FIRSTCH));
  35.     } else {
  36.         return chr($c);
  37.     }
  38. }
  39.  
  40. sub xlate_out($){
  41.     my $c=ord($_[0]);
  42.     my $dti;
  43.     my $xch;
  44.     if(($c >= $FIRSTCH) && ($c <= $LASTCH)){
  45.         $xch = $c - ($dti = $xlate_key);
  46.         $xch += ($xch < $FIRSTCH-$TABSZ) ? 2*$TABSZ : ($xch < $FIRSTCH) ? $TABSZ : 0;
  47.         $dti = ($xch - $FIRSTCH) + $dti;
  48.         $dti -= ($dti >= 2*$TABSZ) ? 2*$TABSZ : ($dti >= $TABSZ) ? $TABSZ : 0;
  49.         $xlate_key = $dti;
  50.         return chr($xch);
  51.     } else {
  52.         return chr($c);
  53.     }
  54. }
  55.  
  56. my %opts;
  57. getopts("m:", \%opts);
  58. my $mode=$opts{"m"} || "decrypt";
  59.  
  60. my $linenum=0;
  61. while (my $line=<>){
  62.     $xlate_key=$linenum;
  63.     $line =~ s/\r?\n$//;
  64.     my @words=split(/\t/,$line);
  65.     for (my $i=0;$i <= $#words; $i++){
  66.         my $word=$words[$i];
  67.         my @chars=split(//,$word);
  68.         for my $c (@chars){
  69.             if ($mode eq "decrypt"){
  70.                 print xlate_out($c);
  71.             } elsif ($mode eq "encrypt"){
  72.                 print xlate_in($c);
  73.             } else {
  74.                 warn "Bad mode: $mode\n";
  75.             }
  76.         }
  77.         print "\t" unless ($i == $#words);
  78.     }
  79.     print "\r\n";
  80.     $linenum++;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment