One-time pad solution incorrect - encryption

A one time pad encryption system represents the letters A-E with numbers 1-5 and a space with the number 0. Given the cipher text: DBCA
and the one time pad: BCBC
What is the encrypted word?
My solution:
A B C D E _
1 2 3 4 5 0
Cipher text: DBCA
One-time pad & key: BCBC
B C B C
2 3 2 3
D B C A
4 2 3 1
Encrypted text:
B A A B
2 1 1 2
The encrypted word I got is: BAAB which isn't a word so I'm so it's incorrect
I've researched how to use one-time pad but I still don't understand it

One-time pad is not like that.
your code length must be at least as long as your text. Since you are going to use it one time for each encrypting and decrypting your text.
f = open("plain.txt", "rb+")
kf = open ("key.txt", "rb" )
f2= open("cipher.txt", "r+")
allplain = f.read()
key =[]
key = kf.read()
# - encrypt plain text and save into cipher.txt
for i in range(len(allplain)):
k = chr (key[i])
p = chr (allplain[i])
c = chr ((ord(p)) ^ (ord(k)))
f2.write(c)
this is a complete implementation of One-Time Pad encryption in Python.
One-Time Pad Encryption

Related

Checking on equal values of 2 different data frame row by row

I have 2 different data frame, one is of 5.5 MB and the other is 25 GB. I want to check if these two data frame have the same value in 2 different columns for each row.
For e.g.
x 0 0 a
x 1 2 b
y 1 2 c
z 3 4 d
and
x 0 0 w
x 1 2 m
y 5 6 p
z 8 9 q
I want to check if the 2° and 3° column are equal for each row, if yes I return the 4° columns for the both data frame.Then I should have:
a w
b m
c m
the 2 data frame are sorted respect the 2° and 3° column value. I try in R but the 2° file (25 GB) is too big. How can I obtain this new file in a "faster" (even some hours) way ???
With GNU awk for arrays of arrays:
$ cat tst.awk
NR==FNR { a[$2,$3][$4]; next }
($2,$3) in a {
for (val in a[$2,$3]) {
print val, $4
}
}
$ awk -f tst.awk small_file large_file
a w
b m
c m
and with any awk (a bit less efficiently):
$ cat tst.awk
NR==FNR { a[$2,$3] = a[$2,$3] FS $4; next }
($2,$3) in a {
split(a[$2,$3],vals)
for (i in vals) {
print vals[i], $4
}
}
$ awk -f tst.awk small_file large_file
a w
b m
c m
The above when reading small_file (NR==FNR is only true for the first file read - look up those variables in the awk man page or google) creates an associative array a[] that maps an index created from the concatenation of the 2nd+3rd fields to the list of value of the 4th field for those 2nd/3rd field combinations. Then when reading large_file it looks up that array for the current 2nd/3rd field combination and loops through all of the values stored for that combination in the previous phase printing that value (the $4 from small_file) plus the current $4.
You said your small file is 5.5 MB and the large file is 25 GB. Since 1 MB is about 1,047,600 characters (see https://www.computerhope.com/issues/chspace.htm) and each of your lines is about 8 characters long that means your small file is about 130 thousand lines long and your large one about 134 million lines long so I expect on an average powered computer the above should take no more than a minute or 2 to run, it certainly won't take anything like an hour!
An alternative to the solution of Ed Morton, but with an identical idea:
$ cat tst.awk
NR==FNR { a[$2,$3] = a[$2,$3] $4 ORS; next }
($2,$3) in a {
s=a[$2,$3]; gsub(ORS,OFS $4 ORS,s)
printf "%s",s;
}
$ awk -f tst.awk small_file large_file
a w
b m
c m

removing some part of string from url

I want to remove jsessionid from given string url and backslash #start
/product.screen?productId=BS-AG-G09&JSESSIONID=SD1SL6FF6ADFF6510
so that output would be like
product.screen?productId=BS-AG-G09
More data like :
1 /product.screen?productId=WC-SH-A02&JSESSIONID=SD0SL6FF7ADFF4953
2 /oldlink?itemId=EST-6&JSESSIONID=SD0SL6FF7ADFF4953
3 /product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953
4 /product.screen?productId=FS-SG-G03&JSESSIONID=SD0SL6FF7ADFF4953
5 /cart.do?action=remove&itemId=EST-11&productId=WC-SH-A01&JSESSIONID=SD0SL6FF7ADFF4953
6 /oldlink?itemId=EST-14&JSESSIONID=SD0SL6FF7ADFF4953
7 /cart.do?action=view&itemId=EST-6&productId=MB-AG-T01&JSESSIONID=SD1SL6FF6ADFF6510
8 /product.screen?productId=BS-AG-G09&JSESSIONID=SD1SL6FF6ADFF6510
9 /product.screen?productId=WC-SH-A02&JSESSIONID=SD1SL6FF6ADFF6510
10 /cart.do?action=view&itemId=EST-6&productId=WC-SH-A02&JSESSIONID=SD1SL6FF6ADFF6510
11 /product.screen?productId=WC-SH-A02&JSESSIONID=SD1SL6FF6ADFF6510
You may use:
library(stringi)
lf1 = "/product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953"
stri_replace_all_regex(
"/product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953",
"&JSESSIONID=.*","")
Where the string: &JSESSIONID=.* (up to the end .*) gets replaced with nothing ("").
or simply: gsub("&JSESSIONID=.*","",lf1)

Complex select in SQLite view

I have two tables where Security holds the access bit mask for a given NTFS file system scan and FileSystemRights which equates to the string representations for the well known bit masks. I need to create a view which exposes the expected (not just proper) string representations for a given bit mask. The problem is several enum values composite and contain combinations of lower values, so the desired idea is not to repeat the implicit values.
For example, a value of 1179817 (Security.Id = 24) should only report ReadAndExecute and Synchronize, excluding ExecuteFile, ListDirectory, Read, ReadAttributes, ReadData, ReadExtendedAttributes, ReadPermissions and Traverse, as those are all part of ReadAndExecute (eg. ReadAndExecute & Read == Read). Its obviously correct to show them all, but a user wants only to see the non implicit values.
I'm lost within the constraints of SQL to produce a join that behaves like this without some abysmal nested case that would be a nightmare to look at.
Does a better programmatic approach exist?
FileSystemRights
================
Id Name Value
-- ---- -----
1 None 0
2 ListDirectory 1
3 ReadData 1
4 WriteData 2
5 CreateFiles 2
6 CreateDirectories 4
7 AppendData 4
8 ReadExtendedAttributes 8
9 WriteExtendedAttributes 16
10 ExecuteFile 32
11 Traverse 32
12 DeleteSubdirectoriesAndFiles 64
13 ReadAttributes 128
14 WriteAttributes 256
15 Write 278
16 Delete 65536
17 ReadPermissions 131072
18 Read 131209
19 ReadAndExecute 131241
20 Modify 197055
21 ChangePermissions 262144
22 TakeOwnership 524288
23 Synchronize 1048576
24 FullControl 2032127
25 GenericAll 268435456
26 GenericExecute 536870912
27 GenericWrite 1073741824
28 GenericRead 2147483648
Security
========
Id FileSystemRights IdentityReference
-- ---------------- -----------------
20 2032127 BUILTIN\Administrators
21 2032127 BUILTIN\Administrators
22 2032127 NT AUTHORITY\SYSTEM
23 268435456 CREATOR OWNER
24 1179817 BUILTIN\Users
25 4 BUILTIN\Users
26 2 BUILTIN\Users
MyView
======
SELECT s.Id AS SecurityId,
f.Name
FROM Security s
JOIN FileSystemRights f
ON CASE f.Value
WHEN 0 THEN s.FileSystemRights = f.Value
ELSE (s.FileSystemRights & f.Value) == f.Value
END
ORDER BY s.Id, f.Name;
Add the actual value of the name to the query.
Then wrap another query around that to filter out values for the same entry that are a subset of another value:
WITH AllValues(SecurityId, Name, Value) AS (
SELECT s.Id,
f.Name,
f.Value
FROM Security s
JOIN FileSystemRights f
ON CASE f.Value
WHEN 0 THEN s.FileSystemRights = f.Value
ELSE (s.FileSystemRights & f.Value) == f.Value
END
)
SELECT SecurityId,
Name
FROM AllValues
WHERE NOT EXISTS (SELECT *
FROM AllValues AS AV2
WHERE AV2.SecurityId = AllValues.SecurityId
AND (AV2.Value & AllValues.Value) != 0
AND AV2.Value > AllValues.Value
)
ORDER BY 1, 2;

Can two Unix processes simultaneous write to different positions in a single file?

This is an unresolved exam question of mine.
Can two Unix processes simultaneous write to different positions
in a single file?
Yes, the two processes will have their own file table entries
no, the shared i-node contains a single offset pointer
only one process will have write privilege
yes, but only if we operate using NFS
There is no file offset recorded in an inode so answer 2. is incorrect.
There is no documented reason for a process to have its access rights modified so 3. is incorrect.
NFS allows simultaneous access by processes on different hosts, the question here is for processes on the same host so NFS shouldn't make a difference.
Here is a shell script demonstrating the remaining answer 1. is correct:
# create a 10m file
dd if=/dev/zero of=/var/tmp/file bs=1024k count=10
# create two 1 MB files
cd /tmp
printf "aaaaaaaa" > aa
printf "bbbbbbbb" > bb
i=0
while [ $i -lt 17 ]; do
cat aa aa > aa.new && mv aa.new aa
cat bb bb > bb.new && mv bb.new bb
i=$((i+1))
done
ls -lG /var/tmp/file /tmp/aa /tmp/bb
# launch 10 processes that will write at different locations in the same file.
# Uses dd notrunc option for the file not to be truncated
# Uses GNU dd fdatasync option for unbuffered writes
i=0
while [ $i -lt 5 ]; do
(
dd if=/tmp/aa of=/var/tmp/file conv=notrunc,fdatasync bs=1024k count=1 seek=$((i*2)) 2>/dev/null &
dd if=/tmp/bb of=/var/tmp/file conv=notrunc,fdatasync bs=1024k count=1 seek=$((i*2+1)) 2>/dev/null &
) &
i=$((i+1))
done
# Check concurrency
printf "\n%d processes are currently writing to /var/tmp/file\n" "$(fuser /var/tmp/file 2>/dev/null | wc -w)"
# Wait for write completion and check file contents
wait
printf "/var/tmp/file contains:\n"
od -c /var/tmp/file
Its output shows ten processes successfully and simultaneously write to the very same file:
-rw-r--r-- 1 jlliagre 1048576 oct. 30 08:25 /tmp/aa
-rw-r--r-- 1 jlliagre 1048576 oct. 30 08:25 /tmp/bb
-rw-r--r-- 1 jlliagre 10485760 oct. 30 08:25 /var/tmp/file
10 processes are currently writing to /var/tmp/file
/var/tmp/file contains:
0000000 a a a a a a a a a a a a a a a a
*
4000000 b b b b b b b b b b b b b b b b
*
10000000 a a a a a a a a a a a a a a a a
*
14000000 b b b b b b b b b b b b b b b b
*
20000000 a a a a a a a a a a a a a a a a
*
24000000 b b b b b b b b b b b b b b b b
*
30000000 a a a a a a a a a a a a a a a a
*
34000000 b b b b b b b b b b b b b b b b
*
40000000 a a a a a a a a a a a a a a a a
*
44000000 b b b b b b b b b b b b b b b b
*
50000000
Definition:
Yes, the two processes will have their own file table entries.
If the file is opened twice with the open function the two file descriptor is created.
Each file descriptor have seperate file status flags.
So the two file descriptor have a write permission file descriptor1 and file descriptor2 have initial position of point to first character to file.
If we specify some position to both descriptor and write in file it can be tested easily.
The content of file.txt
My name is Chandru. This is an empty file.
Coding for testing:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
main()
{
int fd1, fd2;
if((fd1=open("file.txt", O_WRONLY)) <0){
perror("Error");
exit(0);
}
if((fd2=open("file.txt", O_WRONLY)) < 0) {
perror("Error");
exit(0);
}
if(lseek(fd1,20,SEEK_SET) != 20)
{
printf("Cannot seek\n");
exit(0);
}
if(write(fd1,"testing",7) != 7)
{
printf("Error write\n");
exit(0);
}
if(lseek(fd2,10,SEEK_SET) != 10)
{
printf("Cannot seek\n");
exit(0);
}
if(write(fd2,"condition",9) != 9)
{
printf("Error write\n");
exit(0);
}
}
Output:
After that my output is
My name isconditionitesting empty file.
Yes, they can of course, with the following caveats:
Depending on open() mode, one process can easily wipe the file contents
Depending on scheduling, the order of write operations is not deterministic
There is no mandatory locking (in general) - careful design calls for advisory locking.
If they write in the same area using buffered I/O results can be non-deterministic.

Classic ASP - Subtracting numbers but keep leading zeros

I was wondering if someone could help me out.
I have a number, it could be 004 or 010 ... I would like to subtract it by 1 and keep the leading zero.
Everytime i try to subtract, it always takes away the leading zero.
for instance
004 - 1
This always ends up as just
3
But i would like it to be
003
Having said that if i have 010 and i subtract 1 i would like it to be 009
Any help would be greatly appreciated.
Cheers,
Do your calculations with integers then change to a string to display them. then concatenate "0" or "00" where needed eg
c = a - b
displayvalue = cstr(c)
if c < 100 then
displayvalue = "0" & cstr(c)
end if
if c < 10 then
displayvalue = "00" & cstr(c)
end if
Continue doing the math as you are but include some padding 0's using the Right and String functions.
Dim a, b
a = 003
b = 1
Right(String(3,"0") + cstr(a-b), 3)

Resources