Twenty Year Anniversary

RC4 Simple FILE Encryption / Decryption

RC4 Simple FILE Encryption / Decryption
Posted Apr 5, 2018
Authored by Todor Donev

Simple script to perform RC4 encryption / decryption.

tags | tool, cracker
MD5 | 3a863b0b5164e05fa847e501c5eb915f

RC4 Simple FILE Encryption / Decryption

Change Mirror Download
#!/usr/bin/perl
#
# RC4 Simple FILE Encryption/Decryption
#
# Copyright 2018 (c) Todor Donev
# <todor.donev at gmail.com>
# https://ethical-hacker.org/
# https://facebook.com/ethicalhackerorg
#
# Description:
# RC4 generates a pseudorandom stream of bits
# (a keystream). As with any stream cipher,
# these can be used for encryption by combining
# it with the plaintext using bit-wise exclusive-or;
# decryption is performed the same way (since
# exclusive-or with given data is an involution).
# (This is similar to the Vernam cipher except
# that generated pseudorandom bits, rather than a
# prepared stream, are used.) To generate the
# keystream, the cipher makes use of a secret
# internal state which consists of two parts:
#
# o A permutation of all 256 possible bytes
# (denoted "S" below).
#
# o Two 8-bit index-pointers (denoted "i" and "j").
#
# The permutation is initialized with a variable
# length key, typically between 40 and 2048 bits,
# using the key-scheduling algorithm (KSA). Once
# this has been completed, the stream of bits is
# generated using the pseudo-random generation
# algorithm (PRGA).
#
# [todor@paladium ~]$ perl rc4file.pl ethack test.txt test.rc4
# [ RC4 Simple FILE Encryption/Decryption
# [ =======
# [ Author: Todor Donev <todor.donev at gmail.com>
# [ https://ethical-hacker.org/
# [ https://fb.com/ethicalhackerorg
# [ =======
# [ Opening test.txt
# [ Calculation..
# [ Ready and test.rc4 file is closed.
# [todor@paladium ~]$ perl rc4file.pl ethack test.rc4 test.rc4.encoded.txt && cat test.rc4.encoded.txt
# [ RC4 Simple FILE Encryption/Decryption
# [ =======
# [ Author: Todor Donev <todor.donev at gmail.com>
# [ https://ethical-hacker.org/
# [ https://fb.com/ethicalhackerorg
# [ =======
# [ Opening test.rc4
# [ Calculation..
# [ Ready and test.rc4.encoded.txt file is closed.
# Ethical Hacker Bulgaria 2018
# International Cybersecurity Association
# [todor@paladium ~]$
#
# Disclaimer:
# This or previous programs is for Educational
# purpose ONLY. Do not use it without permission.
# The usual disclaimer applies, especially the
# fact that Todor Donev is not liable for any
# damages caused by direct or indirect use of the
# information or functionality provided by these
# programs. The author or any Internet provider
# bears NO responsibility for content or misuse
# of these programs or any derivatives thereof.
# By using these programs you accept the fact
# that any damage (dataloss, system crash,
# system compromise, etc.) caused by the use
# of these programs is not Todor Donev's
# responsibility.
#
# Use them at your own risk!

use warnings;
use strict;
use open ':std', ':encoding(UTF-8)';

print "[ RC4 Simple FILE Encryption/Decryption\n";
print "[ =======\n";
print "[ Author: Todor Donev <todor.donev at gmail.com>\n";
print "[ https://ethical-hacker.org/\n";
print "[ https://fb.com/ethicalhackerorg\n";
print "[ =======\n";
die "[ Usage: $0 <passphrase> <input file> <output file>\n" if(@ARGV != 3);
our $MAX_CHUNK_SIZE = 1024 unless $MAX_CHUNK_SIZE;
my $passphrase = $ARGV[0];
print "[ Opening $ARGV[1]\n";
open(IN, $ARGV[1]) or die "[ Error: Can't read $ARGV[1]: $!\n";
die "[ Error: The file is empty" if (-z $ARGV[1]);
open(OUT, ">$ARGV[2]") or die "[ Error: Can't wrote $ARGV[2]: $!\n";
binmode(IN);
binmode(OUT);
print "[ Calculation..\n";
my $bytes = (stat $ARGV[1])[7];
while(sysread(IN,my $in,$bytes)){
print OUT rc4($passphrase, $in);
}
print "[ Ready and $ARGV[2] file is closed.\n";
close(IN);
close(OUT);

sub rc4 {
my $self;
my(@state, $x, $y);
if (ref $_[0]){
$self = shift;
@state = @{$self->{state}};
$x = $self->{x};
$y = $self->{y};
} else {
@state = init(shift);
$x = $y = 0;
}
my $message = shift;
my $num_pieces = do{
my $num = length($message) / $MAX_CHUNK_SIZE;
my $int = int $num;
$int == $num ? $int : $int+1;
};
for my $piece (0..$num_pieces-1){
my @message = unpack "C*", substr($message, $piece * $MAX_CHUNK_SIZE, $MAX_CHUNK_SIZE);
for ( @message ) {
$x = 0 if ++$x > 255;
$y -= 256 if ($y += $state[$x]) > 255;
@state[$x, $y] = @state[$y, $x];
$_ ^= $state[($state[$x]+$state[$y]) % 256];
}
substr($message, $piece * $MAX_CHUNK_SIZE, $MAX_CHUNK_SIZE) = pack "C*", @message;
}
if ($self) {
$self->{state} = \@state;
$self->{x} = $x;
$self->{y} = $y;
}
$message;
}

sub init {
my @k = unpack('C*',shift);
my @state = 0..255;
my $y = 0;
for my $x (0..255){
$y = ($k[$x % @k]+$state[$x]+$y) % 256;
@state[$x, $y] = @state[$y, $x];
}
wantarray ? @state : \@state;
}

Comments

RSS Feed Subscribe to this comment feed

No comments yet, be the first!

Login or Register to post a comment

File Archive:

September 2018

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Sep 1st
    1 Files
  • 2
    Sep 2nd
    3 Files
  • 3
    Sep 3rd
    15 Files
  • 4
    Sep 4th
    15 Files
  • 5
    Sep 5th
    18 Files
  • 6
    Sep 6th
    18 Files
  • 7
    Sep 7th
    15 Files
  • 8
    Sep 8th
    2 Files
  • 9
    Sep 9th
    2 Files
  • 10
    Sep 10th
    16 Files
  • 11
    Sep 11th
    17 Files
  • 12
    Sep 12th
    15 Files
  • 13
    Sep 13th
    29 Files
  • 14
    Sep 14th
    21 Files
  • 15
    Sep 15th
    3 Files
  • 16
    Sep 16th
    1 Files
  • 17
    Sep 17th
    15 Files
  • 18
    Sep 18th
    16 Files
  • 19
    Sep 19th
    29 Files
  • 20
    Sep 20th
    18 Files
  • 21
    Sep 21st
    0 Files
  • 22
    Sep 22nd
    0 Files
  • 23
    Sep 23rd
    0 Files
  • 24
    Sep 24th
    0 Files
  • 25
    Sep 25th
    0 Files
  • 26
    Sep 26th
    0 Files
  • 27
    Sep 27th
    0 Files
  • 28
    Sep 28th
    0 Files
  • 29
    Sep 29th
    0 Files
  • 30
    Sep 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2018 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close