So i have been working on an encryption algorithm and i wanted to see if it'd successfuly encrypt a text file (i want the program to be versatile so it's reading bytes).
I run the program, encryption works (amount of letter match the amount of letters in plain text) fine BUT whenever i try to read the encrypted file it adds a random byte. \xc3\x81 to be exact.
For example:
if plain text is "noob" decrypted text is, for some reason, Ánoob. "Á" being the random byte.
Here's the code,
.
.
.
# I tried avoiding the byte by adding the statements about 193
# I couldn't add some of the
# While loops are there to make sure the byte is within supported ascii range
def encryption_algorithm(self):
NBYTE = b''
for file in self.file_path_generator(): # self.file_path_generator() just gets the list of files
if file.name != os.path.basename(__file__):
read = self.access_files(file, None ,"rb", False, False) # same thing as "with open("filename", "rb") as f:"
for byte in read:
if byte % 5 == 0 and byte != 193:
byte = (byte + 30)
while byte > 255:
byte -= 255
NBYTE += bytes(chr(byte), "utf-8")
elif byte % 5 == 4 and byte != 193:
byte = (byte + 2)
while byte > 255:
byte -= 255
NBYTE += bytes(chr(byte), "utf-8")
elif byte == 193:
NBYTE += bytes(chr(byte), "utf-8")
else:
byte = (byte + 1)
while byte > 255:
byte -= 255
NBYTE += bytes(chr(byte), "utf-8")
self.access_files(file, NBYTE, "wb", False, False)
def decryption_algorithm(self):
NBYTE = b''
for file in self.file_path_generator():
if file.name != os.path.basename(__file__):
read = self.access_files(file, None ,"rb", False, False)
for byte in read:
if byte % 5 == 0 and byte != 193:
byte = (byte - 30)
while byte < 0:
byte += 255
NBYTE += bytes(chr(byte), "utf-8")
elif byte % 5 == 1 and byte != 193:
byte = (byte - 2)
while byte < 0:
byte += 255
NBYTE += bytes(chr(byte), "utf-8")
elif byte == 193:
NBYTE += (chr(byte), "utf-8")
else:
byte = (byte - 1)
while byte < 0:
byte += 255
NBYTE += bytes(chr(byte), "utf-8")
self.access_files(file, NBYTE, "wb", False, False)
Note: Replacing the "Á" with None does work for text files but not image files.
Note2: I'd add the full encrypted text aswell but some characters are not supported. It was something like "?ppc"
utf-8 can use more than one byte.
https://en.wikipedia.org/wiki/UTF-8#Description
with your example noob. n=0x6e when you add 30.
The result is 140 (0x8c).
And it will need 2 bytes to be encoded in utf-8 .
you can try:
NBYTE = b''
byte=140
NBYTE += bytes(chr(byte), "utf-8")
print(NBYTE)
you will see that the result will be:
b'\xc2\x8c'
Related
I am learning programming on my own at home. I'm interested in C right now and so, this is my first question. I have been trying to figure out why my code is not printing the right answer but I'm not sure what is wrong. I will attach the question and my code below. If some could help me figure out what I am doing wrong I'd be really grateful.
"Write a program that takes a string and displays it, replacing each of its
letters by the letter 13 spaces ahead in alphabetical order.
'z' becomes 'm' and 'Z' becomes 'M'. Case remains unaffected.
The output will be followed by a newline.
If the number of arguments is not 1, the program displays a newline."
I'm using command line arguments to read in a string "My horse is Amazing." and the expected output should be "Zl ubefr vf Nznmvat." but I am getting this as my output "Zå ubeÇr vÇ Nznçvat."
This is my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv[]){
char ch, str[100], newStr[100];
int i, len;
if(argc != 2){
printf("\n");
return (-1);
}
strcpy(str, argv[1]);
len = strlen(str);
printf("%s %d\n\n", str, len);
for (i = 0; i < len; i++)
{
ch = str[i];
if ((ch >= 65) && (ch <= 90)){
ch = ch + 13;
if(ch > 90){
ch = ch - 90;
ch = ch + 64;
}
}else if ((ch >= 97) && (ch <= 122)){
ch = ch + 13;
if(ch > 122){
ch = ch - 122;
ch = ch + 96;
}
}
newStr[i] = ch;
}
printf("%s \n", newStr);
return 0;
}
ch is a signed 1-byte variable, meaning it can only represent values between -128 and 127. Adding 13 to y results in a value outside of that range.
I am reversing an old game for translate it, here is the compression algorithm , I want to know what algorithm this is.
the following is the python pseudocode
while True:
byte = rawdata[praw]
low_byte = byte & 0xF
high_byte = byte >> 4
for i in range(low_byte):
buffer[pbuffer] = rawdata[praw]
pbuffer += 1
praw += 1
for i in range(high_byte):
low_byte2 = rawdata[praw] & 0xF
high_byte2 = rawdata[praw] >> 4
p = pbuffer - low_byte - 1
for i in range(high_byte2):
buffer[pbuffer] = buffer[p]
pbuffer += 1
praw += 1
if low_byte or high_byte is 0, will call this function
def func(data):
# do...while...
while data is even number:
data = (data << 7) | rwadata[praw]
p += 1
if (data & 1) != 0:
break
return (data >> 1)
Recently, I have been trying to educate myself on how to encrypt and decrypt using the Vigenere Cipher.
I have successfully encrypted the message and these are the steps I undertook to achieve encryption:
Encryption Key: Set
Message: Top secret
Step 1: Numerical representation of key is 18, 4, 19 (Using the table below)
Working Out:
Reminder:
P is the set of plaintext units
C is the set of ciphertext units
K is the set of keys
E: P x K -> C is the encryption function
D: C x K -> P is the decryption function
Plaintext: top secret
Ciphertext: ISIKIVJIM
Although I have managed to encrypt the message "top secret" I am struggling to decrypt messages using the Vigenere Cipher method using the numerical technique I used above. Can someone explain to me how I can decrypt lets say: ISIKIVJIM (the ciphertext from above) to its original plain text message which is "top secret".
Thanks.
As pointed out in the comments the decryption formula is : p = c - k mod 26, also note that we have to perform modular arithmetic so our answer for any input should belong in the range of 0 - 25, i.e if we get a negative number we can add 26(i.e the number we are taking modulo by) until we are within this range you can read more about this here :
https://en.wikipedia.org/wiki/Modular_arithmetic
So the decryption will be like :
L = 11 - 18 = -7 mod 26 = -7 + 26 = 19 = T
S = 18 - 4 = 14 mod 26 = 14 = O
I = 8 - 19 = -11 mod 26 = -11 + 26 = 15 = P
ans so on...
I have also written a c++ code : http://ideone.com/M3BAmq
I recently wrote a java program that encrypts and decrypts in Vigenere using bytes. You need to convert the plain text/ crypt text into byte array and pass it in.
public static byte [] encryptVigenere (byte [] pt, String key)
{
byte [] c_text = new byte [pt.length];
byte [] key_text = key.getBytes();
byte tmp;
int shift;
for (int i = 0, j = 0; i < pt.length; i++)
{
if (j >= key_text.length)
j = 0;
shift = key_text[j] - 65; //index of alphabet
tmp = (byte) (pt[i] + shift);
if (tmp > 'Z')
tmp = (byte) (pt[i] - (26-shift));
c_text[i] = tmp;
j++;
}
return c_text;
}
public static byte [] decryptVigenere (byte [] ct, String key)
{
byte [] p_text = new byte [ct.length];
byte [] key_text = key.getBytes();
byte tmp;
int shift;
for (int i = 0, j = 0; i < ct.length; i++)
{
if (j >= key_text.length)
j = 0;
shift = key_text[j] - 65; //index of alphabet
tmp = (byte) (ct[i] - shift);
if (tmp < 'A')
tmp = (byte) (ct[i] + (26-shift));
p_text[i] = tmp;
j++;
}
return p_text;
}
We have a problem right here we can't seem to get out of.
We have a hexadecimal byte array
Let's say its:
0x12 0x34
We now like to output this as ascii char on the display of our device so it should output:
0x31 0x32 0x33 0x34
So we'd like to convert between the values of the byte array, and the ascii equivailent of the number in the byte.
Is there anybody who knows how to do this?
Thanks in advance
edit:
ok so we know:
0x85 = 1000 0101
to get 8 we need to grab the left 4 bits.
In C you can use printf,
unsigned char array[2] = {0x12, 0x34};
printf("%x%x", array[0], array[1]);
prints out "1234"
Edit:
If you need it to print out "0x31 0x32 0x33 0x34" use this,
char ascii[] = {"0123456789ABCDEF"};
unsigned char array[] = {0x12, 0x34};
int length = sizeof(array)/sizeof(array[0]);
int i;
for (i = 0; i < length; ++i) {
printf("0x%02X 0x%02X ", ascii[(array[i] >> 4) & 0xf], ascii[array[i] & 0xf]);
}
prints out "0x31 0x32 0x33 0x34"
HI I am tiring to implement a CFB with DES. I think i am able to encrypt using with CFB but how can I decrypt?? My main issue is CFB code for encrypting using CFB correct ??. Due to the restriction I have, I am unable to use other library.
for (int i = 0; i < VecMSG.size(); i++) {
DESEncrypt(IV, Key);
stringstream str;
str << bitset < 32 > (V[0]); //First 32 bits convert to string
str << bitset < 32 > (V[1]); //Second 32 bits covert to string and join with the first
VText2 = VText = str.str(); //Store in 2 different strings
VText = VText.substr(0, 5); //Take the most significant first 5 bits in the form of
str.str("");
bitset < 2 > mybits(VText); //covert to bits
bitset < 2 > mybits2(VecMSG[i]); //covert plaintext bits from string to bits
str << (mybits ^= mybits2); //XOR with and convert to string
VecCipher.push_back(str.str()); //Store in a different vector
str.str("");
VText2 = VText2.substr(5) + VecCipher[i]; //Remove the first 5 bits and join ciphertext to the end
V[0] = (unsigned int)VText2.substr(0,32).c_str();
V[1] = (unsigned int)VText2.substr(32).c_str();
}