Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- #convert a hex number to base64
- #base64: https://en.wikipedia.org/wiki/Base64
- #1.convert the hex into binary
- #1.a)split the hex into groups of 3 digits (24bits)
- #1.b)convert the 3 digit hex into binary
- #2.split it into groups of 6 bits (4 groups for each 24 bit binary)
- #3.convert each group into decimal (binary to decimal)
- #4. encode each decimal into base64 (store the encoding in a hash)
- #5.join the base64 characters
- my $hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
- #1.a)
- my @threeDigits = ( $hex =~ m/.../g );
- print "@threeDigits\n";
- #1.b)
- my @binThreeDigits;
- for my $threeDigits (@threeDigits) {
- my $binThreeDigits = sprintf("%b", hex($threeDigits));
- push(@binThreeDigits, $binThreeDigits);
- }
- print "@binThreeDigits\n";
- #2.
- my @sixDigits;
- for my $temp (@binThreeDigits) {
- my @temp = ($temp =~ m/....../g);
- push(@sixDigits,@temp);
- }
- print "@sixDigits\n";
- #3.
- my @decimals;
- for my $bin (@sixDigits) {
- my $decvalue = oct( "0b$bin" );
- push(@decimals, $decvalue);
- }
- print "@decimals\n";
- #4.
- my @baseTable = ('A'..'Z', 'a'..'z', 0..9, '+', '/');
- my @base64;
- for my $dec (@decimals) {
- push(@base64, $baseTable[$dec]);
- }
- print "@base64\n";
- #5.
- my $base64 = join("",@base64);
- print "$base64\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement