Data Encryption


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Data Encryption
# 1  
Old 09-02-2015
Data Encryption

Using awk or sed command how can i encrypt the characters for a particular column.For every character it should replace the third charter of alphabets.Example replace "A" with "C" and "B" with "D"..like this it should replace for all characters in particular column.

Using below command i am able to replace the values in particular column.but not sure to replace like explain above.

Code:
awk -F, -v OFS=','  '$3="xxxxx" substr($3,6)' file


Last edited by Corona688; 09-02-2015 at 02:55 PM.. Reason: removed unnecessary quote
# 2  
Old 09-02-2015
Hello katakamvivek,

Following code may help you in same. Let's say we have an example file named file_1.
Code:
cat file_1 
R.Singh test abcdefgh

awk '{split("abcdefghijklmnopqrstuvwxyz", A,"");while(j++<=26){if(j==25 || j==26){D[A[j]]=A[j];} else {D[A[j]]=A[j+2]}};while(i++<=length($3)){S=S D[substr($3,i,1)]};$3=S;print}' file_1

Output will be as follows.
Code:
R.Singh test cdefghij

EDIT: Adding a non one-liner form for solution now.
Code:
cat file_1.ksh 
awk '{
    split("abcdefghijklmnopqrstuvwxyz", A,"");
    while(j++<=26){        
            if(j==25 || j==26)
                { 
                    D[A[j]]=A[j]
                }
            else
                {
                    D[A[j]]=A[j+2]
                }
              };
    while(i++<=length($3)){
                S=S D[substr($3,i,1)]
                  };
    $3=S;
    print
      }' file_1

EDIT2: Here I want to mention fact that I have put logic here in code so when letter y or z comes in column 3rd then those letters will be remained as it is, if you need them too changed to, you can try it with a minor change in if section, where I am setting array D's value.

EDIT3: Previous solution was having only small letters logic, adding small and block letters in solution now.
Code:
awk '{
        split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", A,"");
        while(j++<=52){
                        if(j==25 || j==26 || j==51 || j==52)
                                {
                                        D[A[j]]=A[j]
                                }
                        else
                                {
                                        D[A[j]]=A[j+2]
                                }
                      };
        while(i++<=length($3)){
                                S=S D[substr($3,i,1)]
                              };
        $3=S;
        print
      }' file_1

Example for above code:
Code:
 #####Input_file:
 cat file_1
R.Singh test abcdefghABCETRFG

 ######After running above EDIT#3 code in script as follows.
 ./file_1.ksh
R.Singh test cdefghijCDEGVTHI

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-03-2015 at 02:23 AM.. Reason: added a non one-liner form for solution now. Added a note for solution.
# 3  
Old 09-02-2015
Comments on the the previous solution:
The split() and the following loop are static; it is more efficient to place them in the BEGIN section.
And in the main section it is better to initialize a loop variable (use a for loop!), in case there is more than one line.
The split( , , "") is not understood by all awk versions, others might need
Code:
lenA=split("a b c d e f g ...", A)

--
Here is a solution with a string
Code:
 cat file
abc1,abc2,abc3,abc4

Code:
 cat crypt3.sh
awk -F, -v OFS=, '
BEGIN {
  chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  lchars=length(chars)
}
function crypt(s) {
  s1=""
  for (pos=1; pos<=length(s); pos++) {
    c=substr(s,pos,1)
    i=index(chars,c)
    c1=substr(chars,1+(1+i)%lchars,1)
    s1=s1 (i>0 ? c1 : c)
  }
  return s1
}
{
  $3=crypt($3)
  print
}
' "$@"

Code:
 sh crypt3.sh file
abc1,abc2,cde3,abc4


Last edited by MadeInGermany; 09-02-2015 at 05:28 PM.. Reason: retain characters that do not match
# 4  
Old 09-03-2015
Perl version:
Code:
perl -aF, -ne '$F[2]=~tr/a-zA-Z/c-zA-Zab/; print join(",",@F)' file

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

Zfs send & receive with encryption - how to retrieve data?

Good morning everyone, I'm looking for some help to retrieve data in a scenario where I might have made a big mistake. I'm hoping to understand what I did wrong. My system is made of two Solaris 11 Express servers (old free version for evaluation). The first if for data and the second is... (7 Replies)
Discussion started by: rnd
7 Replies

2. Solaris

Vormetric Data Encryption on Solaris Redhat

Does anyone have experience running the Vormetric Data Encryption on Oracle Solaris platform? (0 Replies)
Discussion started by: dwevans
0 Replies

3. Cybersecurity

File encryption tools with MAC address as an encryption key

Hi all, I'm looking for secure file encryption tools that use MAC address as encryption key. FYI, I'm using Red Hat Enterprise Linux OS. For example: when A wants to send file to B A will encrypt the file with B's computer MAC/IP address as an encryption key This file can only be decrypted... (2 Replies)
Discussion started by: sergionicosta
2 Replies

4. UNIX for Dummies Questions & Answers

Password encryption

In unix, i know the password encrypt by using salt But how does it work? And how windows protect its password? Thank you for helping in advance (5 Replies)
Discussion started by: cryogen
5 Replies

5. IP Networking

no encryption type specified

Hi I have created a user in snmpv3 as myuser n gave a password. Its also feching data by snmpget. Then I created another user as arika and the password is same as myuser.. Now I am trying to change the password by the command : snmpusm -v 3 -u arika -l authNoPriv -a MD5 -A my_password... (1 Reply)
Discussion started by: swapna_me
1 Replies

6. UNIX for Dummies Questions & Answers

File encryption/Key encryption ????

My dilemma, I need to send, deemed confidential, information via e-mail (SMTP). This information is sitting as a file on AIX. Typically I can send this data as a e-mail attachment via what we term a "mail filter" using telnet. I now would like to somehow encrypt the data and send it to a e-mail... (1 Reply)
Discussion started by: hugow
1 Replies

7. Shell Programming and Scripting

encryption is possible??

NEED expertise help for this topic!!! Question 1: Is encryption possible for the shell scriping programing? shadow the scriping file, do think is impossible... Question2: built a simple program with the simplicity function that allow user change settings by enter corret name and... (3 Replies)
Discussion started by: trynew
3 Replies

8. Shell Programming and Scripting

which encryption method?

hello ppl, i've been coding a perl script for xchat and i need to store the nick's passwords. i was wondering which encryption to use. picture this situation: i've got a system flaw and some guy hacks the machine and gets his hands on the passwd file; he has access to the script. what encryption... (2 Replies)
Discussion started by: crashnburn
2 Replies
Login or Register to Ask a Question