convert this code to python public class LFSR { public static void main(String[] args) {…
- User
convert this code to python
public class LFSR {
public static void main(String[] args) { // initial fill boolean[] a = { false, true, false, false, false, false, true, false, true, true, false }; int trials = Integer.parseInt(args[0]); // number of steps int n = a.length; // length of register int TAP = 8; // tap position // Simulate operation of shift register. for (int t = 0; t < trials; t++) { // Simulate one shift-register step. boolean next = (a[n-1] ^ a[TAP]); // Compute next bit. for (int i = n-1; i > 0; i--) a[i] = a[i-1]; // Shift one position. a[0] = next; // Put next bit on right end. if (next) System.out.print("1"); else System.out.print("0"); } System.out.println(); }} /**
-
Linear Feedback Shift Register */ public final class LFSR {
private static final int M = 32; // hard-coded for 32-bits private static final int[] TAPS = {1, 2, 22, 32};
private final boolean[] bits = new boolean[M + 1];
public LFSR(int seed) { for(int i = 0; i < M; i++) { bits[i] = (((1 << i) & seed) >>> i) == 1; } }
public LFSR() { this((int)System.currentTimeMillis()); }
/* generate a random int uniformly on the interval [-2^31 + 1, 2^31 - 1] */ public int nextInt() { //printBits();
// calculate the integer value from the registers int next = 0; for(int i = 0; i < M; i++) { next |= (bits[i] ? 1 : 0) << i; } // allow for zero without allowing for -2^31 if (next < 0) next++; // calculate the last register from all the preceding bits[M] = false; for(int i = 0; i < TAPS.length; i++) { bits[M] ^= bits[M - TAPS[i]]; } // shift all the registers for(int i = 0; i < M; i++) { bits[i] = bits[i + 1]; } return next;}
/** returns random double uniformly over [0, 1) */ public double nextDouble() { return ((nextInt() / (Integer.MAX_VALUE + 1.0)) + 1.0) / 2.0; }
/** returns random boolean */ public boolean nextBoolean() { return nextInt() >= 0; }
private void printBits() { System.out.print(bits[M] ? 1 : 0); System.out.print(" -> "); for(int i = M - 1; i >= 0; i--) { System.out.print(bits[i] ? 1 : 0); } System.out.println(); }
public static void main(String[] args) {
LFSR rng = new LFSR(); for(int i = 1; i <= 10; i++) { boolean next = rng.nextBoolean(); System.out.println(next); } }
}
-
- Assistant
Here's the Python equivalent of the provided Java code:
class LFSR: def __init__(self, seed=int(time.time())): self.M = 32 self.TAPS = [1, 2, 22, 32] self.bits = [False] * (self.M + 1) for i in range(self.M): self.bits[i] = ((1 << i) & seed) >> i == 1 def next_int(self): next_num = 0 for i in range(self.M): next_num |= int(self.bits[i]) << i if next_num < 0: next_num += 1 self.bits[self.M] = False for tap in self.TAPS: self.bits[self.M] ^= self.bits[self.M - tap] for i in range(self.M): self.bits[i] = self.bits[i + 1] return next_num def next_double(self): return (self.next_int() / (2**31 - 1.0) + 1.0) / 2.0 def next_boolean(self): return self.next_int() >= 0 def print_bits(self): print(int(self.bits[self.M]), "->", end=" ") for i in range(self.M - 1, -1, -1): print(int(self.bits[i]), end="") print() rng = LFSR() for i in range(1, 11): next_val = rng.next_boolean() print(next_val)Note: The original code uses the
nextInt()method to generate random numbers, so in this Python equivalent, we use to match the Java code. - User
def init(self, seed=int(time.time())): NameError: name 'time' is not defined
- Assistant
To fix the
NameError: name 'time' is not definederror, you need to import thetimemodule. You can add the following line at the start of your code:import timeHere's the corrected code:
import time class LFSR: def __init__(self, seed=int(time.time())): self.M = 32 self.TAPS = [1, 2, 22, 32] self.bits = [False] * (self.M + 1) for i in range(self.M): self.bits[i] = ((1 << i) & seed) >> i == 1 def next_int(self): next_num = 0 for i in range(self.M): next_num |= int(self.bits[i]) << i if next_num < 0: next_num += 1 self.bits[self.M] = False for tap in self.TAPS: self.bits[self.M] ^= self.bits[self.M - tap] for i in range(self.M): self.bits[i] = self.bits[i + 1] return next_num def next_double(self): return (self.next_int() / (2**31 - 1.0) + 1.0) / 2.0 def next_boolean(self): return self.next_int() >= 0 def print_bits(self): print(int(self.bits[self.M]), "->", end=" ") for i in range(self.M - 1, -1, -1): print(int(self.bits[i]), end="") print() rng = LFSR() for i in range(1, 11): next_val = rng.next_boolean() print(next_val) - User
LFSR encryption and decryption in python
- Assistant
Sure! Here's an example of how you can implement LFSR encryption and decryption in Python:
class LFSR: def __init__(self, seed, taps): self.seed = seed self.taps = taps self.state = seed def shift(self): feedback = sum(self.state[tap] for tap in self.taps) % 2 self.state = self.state[1:] + [feedback] return self.state[-1] def encrypt(self, plaintext): ciphertext = "" for char in plaintext: key_bit = self.shift() cipher_char = chr(ord(char) ^ key_bit) ciphertext += cipher_char return ciphertext def decrypt(self, ciphertext): return self.encrypt(ciphertext) # LFSR encryption and decryption are the same # Example usage seed = [1, 1, 0, 1] # Seed value for LFSR taps = [3, 4] # Tap positions for LFSR plaintext = "Hello, World!" lfsr = LFSR(seed, taps) encrypted = lfsr.encrypt(plaintext) print("Encrypted:", encrypted) decrypted = lfsr.decrypt(encrypted) print("Decrypted:", decrypted)This example implements an LFSR class that takes a seed value and tap positions as input during initialization. The method performs one shift of the LFSR and returns the output bit. The and methods use the method to generate a key bit and perform bitwise XOR operation on the plaintext to encrypt and decrypt the message.
- User
use this code for this case Use LSFR to encrypt and decrypt a message with the following requirements for the LSFR: Input : Message “string ’your name’ ” m value not fixed (max =9) p(x) like equation if m=4 p(x) from user EX: 𝑥^4+𝑥^3+𝑥^2+1 initial vector as binary input (1/0) Use LSFR to encrypt and decrypt Start using flip flop and show table for clock and flip flop in console and show encrypted binary values after that convert to string Then decrypt the ciphered text and output the result of the decryption.
- Assistant
- User
give me example of input
- Assistant
Sure! Here's an example of how you can input values for the LFSR encryption and decryption process:
Enter m value (max=9): 4 Enter the polynomial, p(x), in terms of powers of x (Example: x^4 + x^3 + x^2 + 1): x^4 + x^3 + x^2 + 1 Enter the initial vector as binary input (1/0): 1101In this example, we choose
mas 4, meaning the LFSR will have 4 bits. The polynomialp(x)is defined asx^4 + x^3 + x^2 + 1. The initial vector (seed) is set to1101. You can modify these values according to your desired inputs. - User
raceback (most recent call last): File "C:\Users\Rana\pythonProject4\main.py", line 57, in lfsr = LFSR([int(bit) for bit in seed], taps) TypeError: LFSR() takes no arguments
- Assistant
Apologies for the confusion. It seems that there was an error in the code related to the
__init__method of theLFSRclass.To resolve the issue, please try updating the
__init__method of theLFSRclass as follows:class LFSR: def __init__(self, seed, taps): self.seed = seed self.taps = taps self.state = seed[:]With this change, the code should run successfully without any errors.
Please let me know if you encounter any further issues.
- User
same error
- Assistant