0

yeah it is simply feistel cipher.. I need to implement a simple program implementing it without using any preexisting code. I think that my main problem is in conversion of string to bit to do some functions to it? I'm using java by the way, and I'm not that expert. Please, help me with any info. you think it might help..

this is my code:

public static String encrypt(String s) {
// convert to bits
int sByte = new BigInteger(s.getBytes()).intValue();
for(int i = 0 ; i<=1 ; i++) {

// split 
String splitS1 = (""+sByte).substring(0,(""+sByte).length()/2);
String splitS2 = (""+sByte).substring((""+sByte).length()/2,(""+sByte).length());

// convert to int for function + xor 
int spl1 = new BigInteger(splitS1.getBytes()).intValue();
int spl2 = new BigInteger(splitS2.getBytes()).intValue();

int F = 0;

// key based on i 
if (i == 0) 
F = spl2 + 0000111; // key 7 

if (i == 1)
F = spl2 + 00001011; // key 11

// xor
int xOr = spl1^F;

// swap
sByte = spl2 + xOr;
}
// convert to String
String AfterEnc = new String(new BigInteger(""+sByte, 2).toByteArray());

return AfterEnc;
}
W M
  • 9
  • 3

2 Answers2

3

my main problem is in conversion of string to bit

Do it as follows:

import java.util.BitSet;

public class Main {
    public static void main(String[] args) {
        String str = "text";
        byte[] strBytes = str.getBytes();
        BitSet strBitSet = BitSet.valueOf(strBytes);
        System.out.println("BitSet: " + strBitSet);
        String strToBinary = bitSetToBinary(strBitSet);
        System.out.println("Binary representation: " + strToBinary);
    }

    static String bitSetToBinary(BitSet bitSet) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < bitSet.length(); i++) {
            s.append(bitSet.get(i) == true ? 1 : 0);
        }
        return s.reverse().toString();
    }
}

Output:

BitSet: {2, 4, 5, 6, 8, 10, 13, 14, 19, 20, 21, 22, 26, 28, 29, 30}
Binary representation: 1110100011110000110010101110100

[Update]

Note: I suggest you split the string in the beginning and then create the BitSet for the two parts e.g.

import java.util.BitSet;

public class Main {
    public static void main(String[] args) {
        String str = "text";
        String part1 = str.substring(0, str.length() / 2);
        String part2 = str.substring(str.length() / 2);
        byte[] part1Bytes = part1.getBytes();
        byte[] part2Bytes = part2.getBytes();
        BitSet part1BitSet = BitSet.valueOf(part1Bytes);
        BitSet part2BitSet = BitSet.valueOf(part2Bytes);
        System.out.println("Part1 BitSet: " + part1BitSet);
        System.out.println("Part2 before performing XOR: " + part2BitSet);
        part2BitSet.xor(part1BitSet);// You can create a new BitSet for performing XOR. I am using an existing BitSet for test.
        System.out.println("Part2 after performing XOR: " + part2BitSet);
    }
}

Output:

Part1 BitSet: {2, 4, 5, 6, 8, 10, 13, 14}
Part2 before performing XOR: {3, 4, 5, 6, 10, 12, 13, 14}
Part2 after performing XOR: {2, 3, 8, 12}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • how can I xor it or split it using this? – W M Feb 24 '20 at 15:59
  • can you check my code again? I need to covert them back to string and perform a function on part 2 before xor it – W M Feb 24 '20 at 18:50
  • @WM - I hope the solution worked for you. Do not forget to accept the answer so that future visitors can also use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash May 24 '20 at 11:05
1

In Java, there is no type like 'bit', so you are not able to convert into it. The "smallest" primitive data type is byte. So you can simply convert your String into array of bytes by implementing following code snippet:

String myString = "hello world!";
byte[] myStringInBytes = myString.getBytes();

Hope this will help

Villa_7
  • 508
  • 4
  • 14