The A5Cipher class implements the A5/1 stream cipher, which is a widely used encryption algorithm, particularly in mobile communications. This implementation uses a key stream generator to produce a stream of bits that are XORed with the plaintext bits to produce the ciphertext. For more detai
| 15 | * </p> |
| 16 | */ |
| 17 | public class A5Cipher { |
| 18 | |
| 19 | private final A5KeyStreamGenerator keyStreamGenerator; |
| 20 | private static final int KEY_STREAM_LENGTH = 228; // Length of the key stream in bits (28.5 bytes) |
| 21 | |
| 22 | /** |
| 23 | * Constructs an A5Cipher instance with the specified session key and frame counter. |
| 24 | * |
| 25 | * @param sessionKey a BitSet representing the session key used for encryption. |
| 26 | * @param frameCounter a BitSet representing the frame counter that helps in key stream generation. |
| 27 | */ |
| 28 | public A5Cipher(BitSet sessionKey, BitSet frameCounter) { |
| 29 | keyStreamGenerator = new A5KeyStreamGenerator(); |
| 30 | keyStreamGenerator.initialize(sessionKey, frameCounter); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Encrypts the given plaintext bits using the A5/1 cipher algorithm. |
| 35 | * |
| 36 | * This method generates a key stream and XORs it with the provided plaintext |
| 37 | * bits to produce the ciphertext. |
| 38 | * |
| 39 | * @param plainTextBits a BitSet representing the plaintext bits to be encrypted. |
| 40 | * @return a BitSet containing the encrypted ciphertext bits. |
| 41 | */ |
| 42 | public BitSet encrypt(BitSet plainTextBits) { |
| 43 | // create a copy |
| 44 | var result = new BitSet(KEY_STREAM_LENGTH); |
| 45 | result.xor(plainTextBits); |
| 46 | |
| 47 | var key = keyStreamGenerator.getNextKeyStream(); |
| 48 | result.xor(key); |
| 49 | |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Resets the internal counter of the key stream generator. |
| 55 | * |
| 56 | * This method can be called to re-initialize the state of the key stream |
| 57 | * generator, allowing for new key streams to be generated for subsequent |
| 58 | * encryptions. |
| 59 | */ |
| 60 | public void resetCounter() { |
| 61 | keyStreamGenerator.reInitialize(); |
| 62 | } |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected