Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Oracle
 

Encryt and Decrypt Text messages


Code ResourceAuthor: jamesravid  

Difficulty Level:

Published: 6th Nov 2006   Read: 5958 times  

Filed in: Oracle
Add Comment


 


One of the oldest and simplest encrypting technique is the "Caesar cipher". This involves shifting each of the letters in a message by a set number of places along the alphabet (cycling back the beginning of the alphabet if necessary). For example, if we wanted a shift of 5 places then A becomes F, B becomes G, … , Y becomes D and Z becomes E. So, the word JAMES becomes OERJX. The key to decrypt this code is 5 (representing the shift).

Here are two function one to encrypt and other to decrypt any given code by using "Caesar cipher" algorithm.

Encrypt function accepts two parameters the first one is a plain text the one which you want to encrypt and the second parameter is the shift variable n (you wants to shift alphabets to 5 places then values of n is 5). you can use this function in any sql query or a pl/sql code.

Here is one simple example, select encrypt ('how are you?',5) from dual; --The above query should return mtb fwj dtz? Similarly, decrypt function accepts two parameters the first one is a encrypted text the one which you want to decrypt and the second parameter is the key value (for the above example the key values is 5). To decrypt the above code you can use the following query, select decrypt('mtb fwj dtz?',5) from dual; the query returns how are you?

 


Sample Code
  1.  
  2. -- function which encryts a plain text.
  3. CREATE OR REPLACE FUNCTION encrypt ( in_string VARCHAR2, shift NUMBER) RETURN VARCHAR2 IS
  4. lv_out_string VARCHAR2(32000);
  5. lv_in_string VARCHAR2(32000);
  6. lv_length NUMBER(5);
  7. lv_count NUMBER(5);
  8. lv_temp  VARCHAR2(1);
  9. lv_shift NUMBER(5);
  10. BEGIN
  11.    lv_shift := MOD(shift,26);
  12.    lv_in_string := LOWER(in_string);
  13.    SELECT LENGTH(lv_in_string ),1,'' INTO lv_length,lv_count,lv_out_string FROM dual;
  14.    FOR lv_count IN 1..lv_length LOOP
  15.       lv_temp := SUBSTR(lv_in_string ,lv_count,1);
  16.       IF ASCII(lv_temp) <97 OR ASCII(lv_temp) >122 THEN
  17.         lv_out_string := lv_out_string || lv_temp;
  18.       ELSE
  19.         IF ASCII(lv_temp)+lv_shift >122 THEN
  20.            lv_temp := CHR(ASCII(lv_temp)+lv_shift - 26);
  21.         ELSE
  22.            lv_temp := CHR(ASCII(lv_temp)+lv_shift);
  23.         END IF;
  24.        
  25.         lv_out_string := lv_out_string || lv_temp;
  26.       END IF;  
  27.    END LOOP;
  28.    RETURN lv_out_string;
  29. END encrypt;
  30.  
  31. ---------------------------------------------------------------------------------------------------
  32. -- function which decrypts a encryted text.
  33. CREATE OR REPLACE FUNCTION decrypt ( in_string VARCHAR2, shift NUMBER) RETURN VARCHAR2 IS
  34. lv_out_string VARCHAR2(32000);
  35. lv_length NUMBER(5);
  36. lv_count NUMBER(5);
  37. lv_temp  VARCHAR2(1);
  38. lv_shift NUMBER(5);
  39. BEGIN
  40.    lv_shift := MOD(shift,26);  
  41.    SELECT LENGTH(in_string),1,'' INTO lv_length,lv_count,lv_out_string FROM dual;
  42.    FOR lv_count IN 1..lv_length LOOP
  43.       lv_temp := SUBSTR(in_string,lv_count,1);
  44.       IF ASCII(lv_temp) <97 OR ASCII(lv_temp) >122 THEN
  45.         lv_out_string := lv_out_string || lv_temp;
  46.       ELSE
  47.         IF ASCII(lv_temp) - lv_shift < 97 THEN
  48.            lv_temp := CHR(ASCII(lv_temp) - lv_shift + 26);
  49.         ELSE
  50.            lv_temp := CHR(ASCII(lv_temp) - lv_shift);
  51.         END IF;
  52.        
  53.         lv_out_string := lv_out_string || lv_temp;
  54.       END IF;  
  55.     -- lv_out_string := lv_out_string || decode ( ascii(substr(in_string,lv_count,1)),32,' ',chr(ascii(substr(in_string,lv_count,1))+shift));
  56.    END LOOP;
  57.    RETURN lv_out_string;
  58. END decrypt;
  59.  
Copyright GeekInterview.com


Next Article: Compare data in tables of the local Oracle schema


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. jamesravid16254
2. Jim.Anderson8820
3. krishnaindia20078126
4. Mohit Sharma6745
5. Beena5366
6. jainsourabh24058
7. SriramKrishna1853
8. Satheesh1774

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape