Skip to main content

Cipher engine in ruby

Recently i have started to learn ruby because it's the core language for the rails framework.
Here the cipher engine which uses the old caesar cipher algorithm,
i have used file concepts to read and write the both
encrypted and decrypted outputs to files.
We can also use other methods separately,

launch your irb then

load 'encryptor.rb'

make an instance of the Encryptor class by

e = Encryptor.new

to check all the available methods

e.methods

Here is the source

class Encryptor

    def cipher(rotation)
   chars = (' '..'z').to_a 
   rotated_chars = chars.rotate(rotation)
   Hash[chars.zip(rotated_chars)]
    end

    def encrypt_letter(char,rotation)
    cipher_for_rotate = cipher(rotation)
    cipher_for_rotate[char]
    end

    def decrypt_letter(char,rotation)
    decypher_rotate = cipher(-rotation)
    decypher_rotate[char]
    end

    #encryption method

    def encrypt(string,rotation)

    #split the string to letters
    letters = string.split("")

    #encrypt each letters and store it in array
    result = letters.collect do |letter|
        enc_char = encrypt_letter(letter,rotation)
    end

    #join the characters 
    result.join
    end

    #encrypt a file

    def encrypt_file(filename,rotation)
    #1. create the file handle to the input file
    f = File.open(filename, "r")

    #2. read the text of the input file
    text = f.read

    #3 encrypt the text
    encrypted = encrypt(text,rotation)

    #4 create a name for output file
    output = "#{filename}.encrypted"

    #5. create an output file handle
    out = File.open(output,"w")
    
    #6. write out the text
    out.write(encrypted)
    #7. close the file
    f.close
    out.close
    end

    def decrypt(string,rotation)

    #split the string to letters
    letters = string.split("")

    #decrypt each letters
    result = letters.collect do |letter|
        dec_char = decrypt_letter(letter,rotation)
    end
    #join
    result.join
    end

    def decrypt_file(filename,rotation)

    #create filehandler to read
    f = File.open(filename,"r")

    #read the cipher
    cipher = f.read

    #decrypt the cipher
    decrypted_msg = decrypt(cipher,rotation)

    #output filename
    output_filename = filename.gsub("encrypted","decrypted")

    #file handler to write
    out = File.open(output_filename,"w")

    #write contents
    out.write(decrypted_msg)

    #close
    out.close
    end
end



Comments

Popular posts from this blog

ARP/RARP full simulation program

server.c #include "stdio.h" #include "stdlib.h" #include "string.h" #include "sys/types.h" #include "sys/socket.h" #include "arpa/inet.h" #include "netinet/in.h" #define SA struct sockaddr struct IPmac { char ip[100]; char mac[100]; }; int main() { int sockfd,len,i; struct sockaddr_in servaddr; char buff[30],temp[30],ip[30],mac[30];

Configure opendns in ubuntu 12.04

Today i have changed my dns server name to opendns. which is alternative to default dns and it provide more secure, faster browsing experience. What is opendns? OpenDNS is the leading provider of Internet security and DNS services Industry leading malware and botnet protection Award winning Web filtering Faster, reliable, more secure DNS Ultra-reliable, globally distributed cloud network Simple, Smart, Easy How to configure opendns in ubuntu 12.04: open terminal and type sudo nano /etc/resolv.conf enter your password and change these lines   nameserver 208.67.222.222  nameserver 208.67.220.220 to use google public dns use 8.8.8.8, 8.8.4.4 instead of 208.67.222.222, 208.67.220.220 simple isn't..! now see your facebook load faster than before. opendns comes with parental control, so never worry about unwanted websites, and your childrens are safe now. to check your dns changed successfully goto welcome.opendns.com

Given a string, reverse only vowels in it; leaving rest of the string as it is

/* Given a string, reverse only vowels in it; leaving rest of the string as it is. Input : abcdef Output : ebcdaf */ import java.io.*; import java.util.*; public class VowelReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); String vowels = ""; String ans = ""; int arr[] = new int[str.length()]; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='a' || str.charAt(i)=='e' || str.charAt(i)=='i' || str.charAt(i)=='o' || str.charAt(i)=='u') { vowels+=str.charAt(i); arr[i]=1; } } String revVowels = new StringBuffer(vowels).reverse().toString(); int j=0; for(int i=0;i<str.length();i++) { if(arr[i]!=1) { ans+=str.charAt(i); } else { ans+=revVowels.charAt(j); j++; } } System.out.println(ans); } }