Skip to content

Commit

Permalink
- fix bugs: padding_header_len is variable
Browse files Browse the repository at this point in the history
- adjust default zero/random padding parameters
  • Loading branch information
ZhangJiupeng committed Oct 19, 2017
1 parent da1ecf5 commit 8db517b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/java/cc/agentx/wrapper/RandomPaddingWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public byte[] wrap(byte[] bytes) {
int randomLength = KeyHelper.generateRandomInteger(
Math.max(paddingThreshold, bytes.length)
, paddingThreshold + paddingRange
) + 1;
) + headerLength;
byte[] wrapBytes = KeyHelper.generateRandomBytes(randomLength);
byte[] headerBytes = KeyHelper.getBytes(headerLength, randomLength - bytes.length);
System.arraycopy(bytes, 0, wrapBytes, wrapBytes.length - bytes.length, bytes.length);
System.arraycopy(headerBytes, 0, wrapBytes, 0, headerBytes.length);
return wrapBytes;
} else {
byte[] wrapBytes = KeyHelper.generateRandomBytes(headerLength + bytes.length);
System.arraycopy(bytes, 0, wrapBytes, 1, bytes.length);
System.arraycopy(KeyHelper.getBytes(headerLength, 1), 0, wrapBytes, 0, headerLength);
System.arraycopy(bytes, 0, wrapBytes, headerLength, bytes.length);
System.arraycopy(KeyHelper.getBytes(headerLength, headerLength), 0, wrapBytes, 0, headerLength);
return wrapBytes;
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/cc/agentx/wrapper/WrapperFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public static Wrapper getInstance(cc.agentx.server.Configuration config, String
return getInstance(config.getEncryption(), config.getPassword(), id);
}

/**
* Notice:
* The frame-based processes below cannot be configured simultaneously in this version of implementation
* <p>
* compress, zero-padding, random-padding
*/
public static Wrapper getInstance(String encryption, String password, String id) throws Exception {
switch (id) {
case "raw":
Expand Down Expand Up @@ -83,9 +89,9 @@ public static Wrapper getInstance(String encryption, String password, String id)
case "compress":
return new FrameWrapper(262144, new CompressWrapper());
case "zero-padding":
return new FrameWrapper(262144, new ZeroPaddingWrapper(500, 500));
return new FrameWrapper(262144, new ZeroPaddingWrapper(200, 56));
case "random-padding":
return new FrameWrapper(262144, new RandomPaddingWrapper(500, 500));
return new FrameWrapper(262144, new RandomPaddingWrapper(200, 56));
default:
throw new Exception("unknown process function");
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cc/agentx/wrapper/ZeroPaddingWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public byte[] wrap(byte[] bytes) {
int randomLength = KeyHelper.generateRandomInteger(
Math.max(paddingThreshold, bytes.length)
, paddingThreshold + paddingRange
) + 1;
) + headerLength;
byte[] wrapBytes = new byte[randomLength];
byte[] headerBytes = KeyHelper.getBytes(headerLength, randomLength - bytes.length);
System.arraycopy(bytes, 0, wrapBytes, wrapBytes.length - bytes.length, bytes.length);
System.arraycopy(headerBytes, 0, wrapBytes, 0, headerBytes.length);
return wrapBytes;
} else {
byte[] wrapBytes = new byte[headerLength + bytes.length];
System.arraycopy(bytes, 0, wrapBytes, 1, bytes.length);
System.arraycopy(KeyHelper.getBytes(headerLength, 1), 0, wrapBytes, 0, headerLength);
System.arraycopy(bytes, 0, wrapBytes, headerLength, bytes.length);
System.arraycopy(KeyHelper.getBytes(headerLength, headerLength), 0, wrapBytes, 0, headerLength);
return wrapBytes;
}
}
Expand Down

0 comments on commit 8db517b

Please sign in to comment.