Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.32 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. #convert a hex number to base64
  6. #base64: https://en.wikipedia.org/wiki/Base64
  7. #1.convert the hex into binary
  8. #1.a)split the hex into groups of 3 digits (24bits)
  9. #1.b)convert the 3 digit hex into binary
  10. #2.split it into groups of 6 bits (4 groups for each 24 bit binary)
  11. #3.convert each group into decimal (binary to decimal)
  12. #4. encode each decimal into base64 (store the encoding in a hash)
  13. #5.join the base64 characters
  14. my $hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
  15. #1.a)
  16. my @threeDigits = ( $hex =~ m/.../g );
  17. print "@threeDigits\n";
  18. #1.b)
  19. my @binThreeDigits;
  20. for my $threeDigits (@threeDigits) {
  21.     my $binThreeDigits = sprintf("%b", hex($threeDigits));
  22.     push(@binThreeDigits, $binThreeDigits);
  23. }
  24. print "@binThreeDigits\n";
  25. #2.
  26. my @sixDigits;
  27. for my $temp (@binThreeDigits) {
  28.     my @temp = ($temp =~ m/....../g);
  29.     push(@sixDigits,@temp);
  30. }
  31. print "@sixDigits\n";
  32. #3.
  33. my @decimals;
  34. for my $bin (@sixDigits) {
  35.     my $decvalue = oct( "0b$bin" );
  36.     push(@decimals, $decvalue);
  37. }
  38. print "@decimals\n";
  39. #4.
  40. my @baseTable =  ('A'..'Z', 'a'..'z', 0..9, '+', '/');
  41. my @base64;
  42. for my $dec (@decimals) {
  43.     push(@base64, $baseTable[$dec]);
  44. }
  45. print "@base64\n";
  46. #5.
  47. my $base64 = join("",@base64);
  48. print "$base64\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement