62,567
社区成员




package mars.security;
import java.nio.charset.Charset;
import mars.security.Tool.ValidateType;
public class EncryptHelper {
/*
* 密文二进制数组
*/
public static String[] BinaryDigit=new String[]{
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001"
};
/**
* 将加密完成的数字序列转化为二进制字符串
* @param digits
* @return
* @throws Exception 当录入的数字序列不合法时,会出现转换出错,但是由于是直接调用,不会出现异常情况
*/
public static String convertToCipher(String digits) throws Exception
{
String returnValue="";
char[] chars=digits.toCharArray();
int i=0;
while(i<chars.length)
{
returnValue+=EncryptHelper.BinaryDigit[java.lang.Integer.valueOf(String.valueOf((chars[i++])))];
}
return returnValue;
}
/**
* 按照ASCII编码把input转换成bytes数组
* @param input
* @return
* @throws Exception
*/
public static String convert(ValidateType type,String input) throws Exception
{
String returnValue="";
if(!Tool.validateChars(type,input))
{
throw new Exception("加密字符串不合法!");
}
byte[] bytes=input.getBytes(Charset.forName("ASCII"));
int i=0;
while(i<bytes.length)
{
returnValue+=Tool.convert(bytes[i]);
i++;
}
return returnValue;
}
public static String convert(String input) throws Exception
{
return convert(ValidateType.WORDS,input);
}
}
package mars.security;
import java.util.LinkedList;
public class EncryptTree {
private BinaryTree tree=null;
/*
* 用于密钥初始化后,数字对应的二进制流的计算
*/
public static LinkedList<String> DigitValue=new LinkedList<String>();
public static EncryptTree Instance=new EncryptTree();
private EncryptTree()
{
if(tree==null)
{
DigitValue.add("");//0
DigitValue.add("");//1
DigitValue.add("");//2
DigitValue.add("");//3
DigitValue.add("");//4
DigitValue.add("");//5
DigitValue.add("");//6
DigitValue.add("");//7
DigitValue.add("");//8
DigitValue.add("");//9
/*
* 这里相当于密钥的初始化,当改变这个树时,加密后的密文就会不同
* */
Node n1=new Node(1,'R');
Node n2=new Node(2,'0');
Node n3=new Node(3,'1');
Node n4=new Node(4,'0');
Node n5=new Node(5,'1');
Node n6=new Node(6,'0');
Node n7=new Node(7,'1');
Node n8=new Node(8,'0');
Node n9=new Node(9,'1');
n1.setLeft(n2);
n1.setRight(n3);
n2.setLeft(n4);
n2.setRight(n5);
n3.setLeft(n6);
n4.setRight(n7);
n5.setLeft(n8);
n6.setRight(n9);
tree=new BinaryTree(n1);
computeDigitValue(n1);
}
}
public BinaryTree getTree() {
return tree;
}
public void computeDigitValue(Node node)
{
if(node.getLeft()!=null)
{
DigitValue.set(node.getLeft().getId()-1,DigitValue.get(node.getId()-1)+"0");
computeDigitValue(node.getLeft());
}
if(node.getRight()!=null)
{
DigitValue.set(node.getRight().getId()-1,DigitValue.get(node.getId()-1)+"1");
computeDigitValue(node.getRight());
}
}
}
package mars.security;
public class Node {
private int id;//这个节点ID不需要的,为了加密速度,特意加这个id
private char data; //0|1|R根结点
private Node left=null;
private Node right=null;
public Node(int id,char data)
{
this.id=id;
this.data=data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getData() {
return data;
}
public void setData(char data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
package mars.security;
public class BinaryTree {
private Node binaryTree=null;
public BinaryTree(Node node)
{
binaryTree=node;
}
public Node getBinaryTree() {
return binaryTree;
}
public void setBinaryTree(Node binaryTree) {
this.binaryTree = binaryTree;
}
// /**
// *
// * @param list
// * @return
// */
// public static BinaryTree init(LinkedList<Node> list)
// {
// BinaryTree returnValue=null;
//
// if(validateNodeCount(list.size()))
// {
//
// }
//
// return returnValue;
// }
//
// /**
// * 判断结点个数是否有效
// * @param count 结点个数
// * @return true有效/false无效
// *
// */
// public static boolean validateNodeCount(int count)
// {
// boolean returnValue=false;
// for(int i=1;i>0;i++)
// {
// if(getNodeCount(i)==count)
// {
// returnValue=true;
// break;
// }
// }
// return returnValue;
// }
//
// /**
// * 计算结点个数
// * @param layer 层数
// * @return 结点个数
// */
// public static int getNodeCount(int layer)
// {
// int returnValue=0;
//
// if(layer<=0)
// {
// returnValue=0;
// }
//
// if(layer>0)
// {
// returnValue=(int)Math.pow(2,layer-1)+getNodeCount(layer-1);
//
// }
// return returnValue;
// }
}