Skip to content

Commit

Permalink
Adding pkcs5padding
Browse files Browse the repository at this point in the history
  • Loading branch information
kemasm committed Apr 19, 2017
1 parent 2fe187e commit 6c8c37d
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions src/main/java/CryptoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,33 @@ private static void crypto(int cipherMode, byte[] keyBytes, File inputFile, File
cipher.init(cipherMode, secretKey, ivSpec);

byte[] buffer = new byte[8192];
int noBytes = 0;
byte[] cipherBlock =
new byte[cipher.getOutputSize(buffer.length)];
byte[] cipherBlock = new byte[cipher.getOutputSize(buffer.length)];
int cipherBytes;
while((noBytes = inputStream.read(buffer)) != -1){
cipherBytes = cipher.update(buffer, 0, noBytes, cipherBlock);

int lastNoBytes = 0;
int currNoBytes = inputStream.read(buffer);
int nextNoBytes = 0;
while(currNoBytes != -1){
cipherBytes = cipher.update(buffer, 0, currNoBytes, cipherBlock);
nextNoBytes = inputStream.read(buffer);
if(nextNoBytes == -1 && cipherMode == Cipher.DECRYPT_MODE){
cipherBytes -= cipherBlock[cipherBytes-1];
}

outputStream.write(cipherBlock, 0, cipherBytes);
lastNoBytes = currNoBytes;
currNoBytes = nextNoBytes;
}

if(cipherMode == Cipher.ENCRYPT_MODE){
int pad = 16 - (lastNoBytes % 16);
byte[] padding = addPadding(pad);

System.out.println(pad);
cipherBytes = cipher.update(padding, 0, pad, cipherBlock);
outputStream.write(cipherBlock, 0, cipherBytes);
}

cipherBytes = cipher.doFinal(cipherBlock,0);
outputStream.write(cipherBlock, 0, cipherBytes);

Expand All @@ -87,6 +106,29 @@ private static void crypto(int cipherMode, byte[] keyBytes, File inputFile, File
}
}

public static byte[] addPadding(int pad){
int size = pad;
if(pad % 16 == 0){
size = 16;
}

byte[] padding = new byte[size];
for(int i = 0; i < size; i++){
padding[i] = (byte) pad;
}

return padding;
}

public static String printArr(byte[] arr){
String buff = "";
int len = arr.length;
for(int i = 0; i < len; i++){
buff += arr[i] + ",";
}
return buff;
}

public static byte[] keyExtract(File keyFile) throws Exception{
String keyDir = keyFile.getAbsolutePath();
String key = "";
Expand Down

0 comments on commit 6c8c37d

Please sign in to comment.