forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA5Cipher.java
More file actions
30 lines (22 loc) · 856 Bytes
/
A5Cipher.java
File metadata and controls
30 lines (22 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.thealgorithms.ciphers.a5;
import java.util.BitSet;
// https://en.wikipedia.org/wiki/A5/1
public class A5Cipher {
private final A5KeyStreamGenerator keyStreamGenerator;
private static final int KEY_STREAM_LENGTH = 228; // 28.5 bytes so we need to pad bytes or something
public A5Cipher( BitSet sessionKey, BitSet frameCounter ) {
keyStreamGenerator = new A5KeyStreamGenerator();
keyStreamGenerator.initialize( sessionKey, frameCounter );
}
public BitSet encrypt( BitSet plainTextBits ) {
// create a copy
var result = new BitSet( KEY_STREAM_LENGTH );
result.xor( plainTextBits );
var key = keyStreamGenerator.getNextKeyStream();
result.xor( key );
return result;
}
public void resetCounter() {
keyStreamGenerator.reInitialize();
}
}