From 17ff110b93a373115c8c28d9ccdb6d0dc60aa20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20D=C4=85bkowski?= Date: Mon, 25 Jun 2018 16:56:44 +0000 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit > 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 --- CRC.java | 15 ++++++ Decoding.java | 60 +++++++++++++++++++++++ Encoding.java | 65 +++++++++++++++++++++++++ Polynomials.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 CRC.java create mode 100644 Decoding.java create mode 100644 Encoding.java create mode 100644 Polynomials.java diff --git a/CRC.java b/CRC.java new file mode 100644 index 0000000..687e7e9 --- /dev/null +++ b/CRC.java @@ -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"); + } + } +} \ No newline at end of file diff --git a/Decoding.java b/Decoding.java new file mode 100644 index 0000000..73f12ca --- /dev/null +++ b/Decoding.java @@ -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 message = new ArrayList<>(); + ArrayList g = new ArrayList<>(); + ArrayList l = new ArrayList<>(); + ArrayList tmp = new ArrayList<>(); + ArrayList 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; + } +} \ No newline at end of file diff --git a/Encoding.java b/Encoding.java new file mode 100644 index 0000000..4974926 --- /dev/null +++ b/Encoding.java @@ -0,0 +1,65 @@ +import java.util.ArrayList; +import java.util.Collections; + +public class Encoding { + private ArrayList fcs; + + public Encoding(String args) { + ArrayList message = new ArrayList<>(); + ArrayList 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 tmp = new ArrayList<>(); + ArrayList 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 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); + } +} \ No newline at end of file diff --git a/Polynomials.java b/Polynomials.java new file mode 100644 index 0000000..fb0ab55 --- /dev/null +++ b/Polynomials.java @@ -0,0 +1,121 @@ +import java.util.ArrayList; + +public class Polynomials { + int n; + + public Polynomials(int n) { + this.n = n; + } + + private ArrayList normalize(ArrayList 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 multiply(ArrayList f, ArrayList g){ + ArrayList 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 multiplyByConst(ArrayList f, Integer a){ + ArrayList result = new ArrayList<>(); + for (Integer i : f){ + result.add((i * a) % n); + } + return result; + } + + public ArrayList subtract(ArrayList f, ArrayList g){ + ArrayList 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 add(ArrayList f, ArrayList g){ + ArrayList 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 divModN(ArrayList f, ArrayList g) throws ArithmeticException{ + ArrayList quotient = new ArrayList<>(); + ArrayList remainder = new ArrayList<>(f); + ArrayList 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 gcc (ArrayList f, ArrayList 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)); + } +} \ No newline at end of file