Created
November 9, 2012 08:39
-
-
Save basavesh/4044493 to your computer and use it in GitHub Desktop.
MerkleHellmanKnapsackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.cmu.cs771.hw1; | |
import java.nio.charset.Charset; | |
import java.util.*; | |
import java.math.BigInteger; | |
public class MerkleHellmanKnapsackTest { | |
public MerkleHellmanKnapsackTest() { | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
String plainTextStr = "a"; | |
LinkedList<Integer> wList = new LinkedList<Integer>(); | |
int wSum = 0; | |
// w = {2, 7, 11, 21, 42, 89, 180, 354} | |
int wInt[] = { 2, 7, 11, 21, 42, 89, 180, 354 }; | |
for (int i = 0; i < wInt.length; i++) { | |
wList.add(wInt[i]); | |
} | |
Iterator wIterator = wList.iterator(); | |
while (wIterator.hasNext()) { | |
wSum += Integer.parseInt(wIterator.next().toString()); | |
} | |
System.out.println("The sum is : " + wSum); | |
int qInt = 881;// qInt > wSum | |
// generate the number that is in the range [1,q) | |
for (int i = 1; i < qInt; i++) { | |
if (gcd(i, qInt) == 1 && i != 2) { | |
// System.out.println(i); | |
} | |
} | |
int rInt = 588; | |
wIterator = wList.iterator(); | |
LinkedList<Integer> bList = new LinkedList<Integer>(); | |
while (wIterator.hasNext()) { | |
bList.add(Integer.parseInt(wIterator.next().toString()) * rInt | |
% qInt); | |
} | |
System.out.println(bList.toString()); | |
//System.out.println(stringToBin(plainTextStr)); | |
Charset encoding = Charset.forName("US-ASCII"); | |
System.out.println(plainTextStr.getBytes(encoding)); | |
int ch = (int) plainTextStr.charAt(0); | |
//System.out.println(Integer.toBinaryString(ch & 0xFF)); | |
String chBin = Integer.toBinaryString(ch & 0XFF); | |
System.out.println(chBin); | |
chBin = String.format("%8s", chBin).replace(' ', '0'); | |
System.out.println(chBin); | |
Iterator bIterator = bList.iterator(); | |
int chBinIdx = 0; | |
int chSum = 0; | |
while(bIterator.hasNext()){ | |
chSum += Integer.parseInt(bIterator.next().toString()) * Integer.parseInt(Character.toString(chBin.charAt(chBinIdx))); | |
chBinIdx ++; | |
} | |
System.out.println(chSum); | |
BigInteger k = new BigInteger("588"); | |
BigInteger rMI = k.modInverse(new BigInteger("881")); | |
//System.out.println(k.modInverse(new BigInteger("881"))); | |
int rr = (chSum * Integer.parseInt(rMI.toString())) % qInt; | |
System.out.println(rr); | |
wIterator = wList.descendingIterator(); | |
LinkedList<Integer> de = new LinkedList<Integer>(); | |
int tmp = 0; | |
while(wIterator.hasNext()){ | |
tmp = rr - Integer.parseInt(wIterator.next().toString()); | |
if(tmp < 0){ | |
de.addFirst(0); | |
//System.out.print(0); | |
}else{ | |
rr = tmp; | |
de.addFirst(1); | |
//System.out.print(1); | |
} | |
} | |
System.out.println(de.toString()); | |
int test = 01100001; | |
System.out.println((char) test); | |
} | |
public static int gcd(int a, int b) { | |
if (b == 0) | |
return a; | |
return gcd(b, a % b); | |
} | |
public static String[] stringToBin(String s) { | |
System.out.println("Converting: " + s); | |
byte[] b = s.getBytes(); | |
String[] sa = new String[s.getBytes().length]; | |
for (int i = 0; i < b.length; i++) { | |
sa[i] = Integer.toBinaryString(b[i] & 0xFF); | |
} | |
return sa; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment