orthogonal_text_scrambling_double.pl 804 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 29 July 2017
  5. # https://github.com/trizen
  6. # An interesting text scrambling algorithm, invented by the author in ~2008.
  7. use utf8;
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. sub scramble {
  12. my ($str) = @_;
  13. my $i = my $l = length($str);
  14. $str =~ s/(.{$i})(.)/$2$1/sg while (--$i > 0);
  15. $str =~ s/(.{$i})(.)/$2$1/sg while (++$i < $l);
  16. return $str;
  17. }
  18. sub unscramble {
  19. my ($str) = @_;
  20. my $i = my $l = length($str);
  21. $str =~ s/(.)(.{$i})/$2$1/sg while (--$i > 0);
  22. $str =~ s/(.)(.{$i})/$2$1/sg while (++$i < $l);
  23. return $str;
  24. }
  25. my $abc = "abcdefghijklmnopqrstuvwxyz";
  26. say scramble($abc); #=> "ckytmliqzrbjwuexhogpdsanvf"
  27. say unscramble(scramble($abc)); #=> "abcdefghijklmnopqrstuvwxyz"