简体   繁体   中英

How can I convert string to bit then split it in half and do some function on a part and then xor it with the other then swap using java?

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;
}

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 eg

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}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM