#what should I write in code to get cipher text zebbw and decrypted text hello

1 messages · Page 1 of 1 (latest)

prime lark
wary socketBOT
#

<@&987246746478460948> please have a look, thanks.

wary socketBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

willow bramble
#

mind sharing ur code as non-picture please

upbeat surge
#

Can you post the code

#

Ah beat me to it

willow bramble
#

also, what decryption is that? caesar/vigenere?

prime lark
#

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;

}
}

upbeat surge
#

Looks like a Caesar, can you confirm what it is? @prime lark

wary socketBOT
# prime lark package mini_project; import java.util.Scanner; public class AffineChiper { ...

Detected code, here are some useful tools:

Formatted code
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;
  }
}
upbeat surge
#

AffineChiper

#

nvm ;p

prime lark
#

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:

  1. 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
  2. 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.
prime lark
upbeat surge
#

I see the issue, just trying to think of a way to explain it

prime lark
upbeat surge
#

your encrypt is a little wrong

prime lark
#

What should I write?

upbeat surge
#
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

prime lark
#

Thank u so much

upbeat surge
#

so knowing this, do you know how to fix the decrypt method now? 🙂

prime lark
#

No 🥲

#

What shd i do

upbeat surge
#

so start with the p and c

#

we need it to fall within the 26 letter alphabet

prime lark
#

Yes

upbeat surge
#

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

prime lark
#

Mod inverse ? How to write that?

upbeat surge
#

The term is called Multiplicative Inverses

prime lark
#

How to write that?

upbeat surge
#

if you want to learn about it, you can readup on google

#

or you can do what I did

willow bramble
#

essentially, as we all learned, the "inverse" of 5 is 1/5

#

cause 5 * 1/5 is 1

upbeat surge
willow bramble
#

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

prime lark
#

What?

willow bramble
#

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

prime lark
#

But what change shd i make in code

willow bramble
#

if u have to ask that, u probably didnt really understand it yet

prime lark
#

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 🥲

upbeat surge
#

int p = ((c - k2) * 17) % 26; (k1 inverse mod 26) would the be exact solution

#

but I hardcoded 17

willow bramble
#

it wont really help u if we give u some code and u copy pasta it

#

then ur lost again 5 minutes after

upbeat surge
#

you can del my msg if you want don't want the solution out zabu right away ;p

prime lark
#

I thought to think and do it myself but

#

They all hv copy pasted and submitted it

prime lark
#

I get biPPc in the decrypted plaintext

willow bramble
#

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

prime lark
#

What should I do then

prime lark
#

Thanks thanks a lot

#

I got it