#what should I write in code to get cipher text zebbw and decrypted text hello
1 messages · Page 1 of 1 (latest)
<@&987246746478460948> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
mind sharing ur code as non-picture please
also, what decryption is that? caesar/vigenere?
package mini_project;
import java.util.Scanner;
public class AffineChiper {
public static void main(String[]Args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the plaintext to be encrypted: ");
String plaintext = sc.nextLine();
String ciphertext = encrypt(plaintext);
System.out.println("Ciphertext: "+ciphertext);
System.out.println("Decrypted Plaintext: "+decrypt(ciphertext));
}
public static String encrypt(String plaintext) {
String ciphertext = "";
int k1 = 7;
int k2 = 2;
for(int i=0;i<plaintext.length();i++) {
int p = (int)plaintext.charAt(i);
int c = ((p*k1)+k2)%26;
ciphertext += (char)((c-k2)%26);
}
return ciphertext;
}
public static String decrypt(String ciphertext) {
String plaintext = "";
int k1 = 7;
int k2 = 2;
//i
//
for(int i=0;i<ciphertext.length();i++) {
int c = (int)ciphertext.charAt(i);
int p = ((c-k2)*1/k1)%26;
plaintext += (char)(p*1/k1)%26;
}
return plaintext;
}
}
Looks like a Caesar, can you confirm what it is? @prime lark
Detected code, here are some useful tools:
package mini_project;
import java.util.Scanner;
public class AffineChiper {
public static void main(String[] Args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the plaintext to be encrypted: ");
String plaintext = sc.nextLine();
String ciphertext = encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
System.out.println("Decrypted Plaintext: " + decrypt(ciphertext));
}
public static String encrypt(String plaintext) {
String ciphertext = "";
int k1 = 7;
int k2 = 2;
for (int i = 0; i < plaintext.length(); i++) {
int p = (int ) plaintext.charAt(i);
int c = ((p * k1) + k2) % 26;
ciphertext += (char ) ((c - k2) % 26);
}
return ciphertext;
}
public static String decrypt(String ciphertext) {
String plaintext = "";
int k1 = 7;
int k2 = 2;
//i
//
for (int i = 0; i < ciphertext.length(); i++) {
int c = (int ) ciphertext.charAt(i);
int p = ((c - k2) * 1 / k1) % 26;
plaintext += (char ) (p * 1 / k1) % 26;
}
return plaintext;
}
}
In a multiplicative cipher, the encryption algorithm multiplies the plaintext characters by the
key and the decryption algorithm divides the ciphertext characters by the key as shown in
the following Figure.
In this project our goal is to design a Affine chiper. Here, we can combine the additive and
multiplicative ciphers with a pair of keys to get what is called the affine cipher. The first
key is used with the multiplicative cipher; the second key is used with the additive cipher.
The following figure shows that the affine cipher is actually two ciphers, applied one after
another.
In the affine cipher, the relationship between the plaintext P and the ciphertext C is
C = (P k1 + k2) mod 26 × k1 + k2) mod 26
P = ((C k2) k1 − k2) × k1 × k1 + k2) mod 26 − k2) × k11
) mod 26
Representation of plaintext and ciphertext characters in Z26 is shown below.
Example: Using affine cipher encrypt the message “hello” then decript with key pair (7, 2).
Encryption:
We use 7 for the multiplicative key and 2 for the additive key. We get “ZEBBW”.
Decryption:
Add the additive inverse of 2 24 (mod 26) to the received ciphertext. Then multiply the − k2) × k1 ≡ 24 (mod 26) to the received ciphertext. Then multiply the
result by the multiplicative inverse of 7− k2) × k11 15 (mod 26) to find the plaintext “hello”. ≡ 24 (mod 26) to the received ciphertext. Then multiply the
Definitions:
- Additive Inverse : In Zn
, two numbers a and b are additive inverses of each other if
a + b 0 (mod n) ≡ 24 (mod 26) to the received ciphertext. Then multiply the
For example, the additive inverse of 4 in Z10 is 10 4 = 6. − k2) × k1 - Multiplicative Inverse : In Zn
, two numbers a and b are the multiplicative inverse of
each other if
a b 1 (mod n) × k1 + k2) mod 26 ≡ 24 (mod 26) to the received ciphertext. Then multiply the
For example, if the modulus is 10, then the multiplicative inverse of 3 is 7.
In other words, we have (3 7) mod 10 = 1.
It's like encrypting text then decrypting it
I see the issue, just trying to think of a way to explain it
your encrypt is a little wrong
What should I write?
for (int i = 0; i < plaintext.length(); i++) {
int p = (int) plaintext.charAt(i) - 'a';
int c = ((p * k1) + k2) % 26;
ciphertext += (char) (c + 'a');
}
The - 'a' puts it within the range of 0-25
the int c is already the encoded ciper text
(char) (c + 'a') here we get the correct value back
the rest was correct
so the only bit I changed was int p and the ciphertext += bit
ciphertext += (char ) ((c - k2) % 26); you don't need this bit because int c = ((p * k1) + k2) % 26; is the encryption
Thank u so much
so knowing this, do you know how to fix the decrypt method now? 🙂
Yes
then with the ciphertext we want to put it back to what it was before
so + 'a'
but that's not the important bit
i mean it is, but the actual decryption int p = ((c - k2) * 1 / k1) % 26; this formula is wrong
int c = (int) ciphertext.charAt(i) - 'a';
int p = ((c - k2) * 1 / k1) % 26;
plaintext += (char) (p + 'a');
so yuo need to fix p
the reason it's wrong is because you're not reversing what the encrypt did
the encrypt does a % 26
so the decrypt needs to do the reverse
sorry mod inverse % 26
Mod inverse ? How to write that?
The term is called Multiplicative Inverses
How to write that?
but if u add modulo to the deal, it becomes tricky
lets say we have mod 5
so the numbers 0, 1, 2, 3, 4
What?
now, look at a number like 3
what do u need to multiple 3 with to get 1 (mod 5)
and thats what he was talking about
the answer is 2 btw
3 * 2 = 6 -> mod 5 -> 1
But what change shd i make in code
if u have to ask that, u probably didnt really understand it yet
I haven't even understand anything about the project tbh the students in our college hv already submitted
I am left behind i hope they just accept my project 🥲
int p = ((c - k2) * 17) % 26; (k1 inverse mod 26) would the be exact solution
but I hardcoded 17
well, then u have worse problems than this. u should step back and learn
it wont really help u if we give u some code and u copy pasta it
then ur lost again 5 minutes after
you can del my msg if you want don't want the solution out zabu right away ;p
No student really learned in our college they just copy pasted it
I thought to think and do it myself but
They all hv copy pasted and submitted it
It's not coming
I get biPPc in the decrypted plaintext
as said. u wont be able to effectively use snippets from others that u dont fully understand
u probably made a minor mistake here or there, or applied the code wrong
this will continue to happen until u actually fully understand what ur doing and whats going on
What should I do then