1
0
forked from kalmar/DALGLI0

Prześlij pliki do ''

> Program uruchamia się z konsoli w sposób:
>
> java CRC ciąg_znaków_n-2 -e     //aby obliczyć FCS dla podanego ciąg
>
> java CRC ciąg_znaków_n -d        //aby sprawdzić czy ostanie suma kontrolna
> jest zgodna z treścią wiadomości
This commit is contained in:
Rafał Dąbkowski 2018-06-25 16:56:44 +00:00
parent 901ec91940
commit 17ff110b93
4 changed files with 261 additions and 0 deletions

15
CRC.java Normal file
View File

@ -0,0 +1,15 @@
public class CRC {
public static void main(String[] args) {
try {
if (args[1].equals("-e")){
Encoding tmp = new Encoding(args[0]);
System.out.println(args[0] + tmp.getFcsChars());
} else if (args[1].equals("-d")){
Decoding d = new Decoding(args[0]);
System.out.println(d.getCheck());
}
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Za mało argumentów");
}
}
}

60
Decoding.java Normal file
View File

@ -0,0 +1,60 @@
import java.util.ArrayList;
import java.util.Collections;
public class Decoding {
private Boolean check;
public Decoding(String arg){
Polynomials poly = new Polynomials(2);
ArrayList<Byte> message = new ArrayList<>();
ArrayList<Integer> g = new ArrayList<>();
ArrayList<Integer> l = new ArrayList<>();
ArrayList<Integer> tmp = new ArrayList<>();
ArrayList<Integer> tmp2 = new ArrayList<>();
for (int i = 0; i < arg.length(); ++i){
message.add((byte)arg.charAt(i));
}
for (int i = 0; i < 16; ++i){
if (i == 12 || i == 5 || i==0){
g.add(1);
} else{
g.add(0);
}
l.add(1);
}
g.add(1);
for (int i = 0; i < 16 ; ++i){
tmp.add(0);
}
for (byte b : message){
String s2 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
for (int i = 0; i < 8; ++i){
tmp2.add(Character.getNumericValue(s2.charAt(i)));
}
}
Collections.reverse(tmp2);
tmp.addAll(tmp2);
tmp2.clear();
for (int i = 0; i < arg.length() * 8; ++i) tmp2.add(0);
for (int i = 0; i < 16; ++i) tmp2.add(1);
tmp = poly.divModN(poly.add(tmp, tmp2),g);
if (tmp.size() == 0) {
check = true;
} else
check = false;
// String m = arg.substring(0, arg.length()-2);
// String fcs = arg.substring(arg.length()-2);
// Encoding e = new Encoding(m);
// if (e.getFcsChars().equals(fcs)) check = true;
// else check = false;
}
public Boolean getCheck() {
return check;
}
}

65
Encoding.java Normal file
View File

@ -0,0 +1,65 @@
import java.util.ArrayList;
import java.util.Collections;
public class Encoding {
private ArrayList<Integer> fcs;
public Encoding(String args) {
ArrayList<Byte> message = new ArrayList<>();
ArrayList<Integer> g = new ArrayList<>();
fcs = new ArrayList<>();
Polynomials poly = new Polynomials(2);
for (int i = 0; i < args.length(); ++i){
message.add((byte)args.charAt(i));
}
for (int i = 0; i < 16; ++i){
if (i == 12 || i == 5 || i==0){
g.add(1);
} else{
g.add(0);
}
}
g.add(1);
ArrayList<Integer> tmp = new ArrayList<>();
ArrayList<Integer> tmp2 = new ArrayList<>();
for (int i = 0; i < 16 ; ++i){
tmp.add(0);
}
for (byte b : message){
String s2 = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
for (int i = 0; i < 8; ++i){
tmp2.add(Character.getNumericValue(s2.charAt(i)));
}
}
Collections.reverse(tmp2);
tmp.addAll(tmp2);
tmp2.clear();
for (int i = 0; i < args.length() * 8; ++i) tmp2.add(0);
for (int i = 0; i < 16; ++i) tmp2.add(1);
fcs = poly.divModN(poly.add(tmp, tmp2), g);
while(fcs.size() < 16) fcs.add(0);
Collections.reverse(fcs);
}
public ArrayList<Integer> getFcs() {
return fcs;
}
public String getFcsChars(){
int a = 0;
int b = 0;
for (int i = 0; i < 8; ++i){
if (fcs.get(i) == 1){
a += Math.pow(2, 7-i);
}
if (fcs.get(i + 8) == 1){
b += Math.pow(2, 7-i);
}
}
return Character.toString((char)a) + Character.toString((char)b);
}
}

121
Polynomials.java Normal file
View File

@ -0,0 +1,121 @@
import java.util.ArrayList;
public class Polynomials {
int n;
public Polynomials(int n) {
this.n = n;
}
private ArrayList<Integer> normalize(ArrayList<Integer> w){
for (int i = w.size()-1; i >= 0; --i){
if (w.get(i).equals(0)) {
w.remove(i);
}else {
return w;
}
}
return w;
}
public ArrayList<Integer> multiply(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
int tmp;
for (int i = 0; i < f.size(); ++i){
for (int j = 0; j < g.size(); ++j){
tmp = f.get(i) * g.get(j);
if (i + j < result.size()){
tmp += result.get(i+j);
result.set(i + j, tmp % n);
} else {
while ((i + j) >= (result.size())){
result.add(0);
}
result.set(i + j, tmp % n);
}
}
}
return (result);
}
private int getZero(int a, int b) throws ArithmeticException{
for (int i = 0; i <= n; ++i){
if ((a + (-(b * i))) == 0) return i;
}
throw new ArithmeticException("not reversible");
}
public ArrayList<Integer> multiplyByConst(ArrayList<Integer> f, Integer a){
ArrayList<Integer> result = new ArrayList<>();
for (Integer i : f){
result.add((i * a) % n);
}
return result;
}
public ArrayList<Integer> subtract(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
for (int i = 0; i < f.size(); ++i){
if (i < g.size()){
result.add(Math.floorMod(f.get(i) - g.get(i), n));
} else{
result.add(f.get(i));
}
}
return result;
}
public ArrayList<Integer> add(ArrayList<Integer> f, ArrayList<Integer> g){
ArrayList<Integer> result = new ArrayList<>();
if (f.size() >= g.size()) {
for (int i = 0; i < f.size(); ++i) {
if (i < g.size()) {
result.add(Math.floorMod(f.get(i) + g.get(i), n));
} else {
result.add(f.get(i));
}
}
} else{
for (int i = 0; i < g.size(); ++i) {
if (i < f.size()) {
result.add(Math.floorMod(f.get(i) + g.get(i), n));
} else {
result.add(g.get(i));
}
}
}
return result;
}
public ArrayList<Integer> divModN(ArrayList<Integer> f, ArrayList<Integer> g) throws ArithmeticException{
ArrayList<Integer> quotient = new ArrayList<>();
ArrayList<Integer> remainder = new ArrayList<>(f);
ArrayList<Integer> tmpList;
int a = f.size() - 1;
int b = g.size() - 1;
for (int i = 0; i < a - b + 1; ++i){
quotient.add(0);
}
int q = quotient.size()-1;
int tmp;
while (a >= b) {
tmp = getZero(remainder.get(a), g.get(b));
quotient.set(q--, tmp);
tmpList = multiplyByConst(g, tmp);
while(tmpList.size() <= a) tmpList.add(0,0);
remainder = subtract(remainder, tmpList);
--a;
}
return normalize(remainder);
}
public ArrayList<Integer> gcc (ArrayList<Integer> f, ArrayList<Integer> g){
if (g.size() == 0) return f;
if (f.size() > g.size()){
return gcc(g, divModN(f, g));
} else return gcc(g, divModN(g, f));
}
}