Skip to content

Commit

Permalink
Handle Invalid Key Format
Browse files Browse the repository at this point in the history
  • Loading branch information
kemasm committed Apr 19, 2017
1 parent 6c8c37d commit f1bd1f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions demofiles/InvalidKeyFormat-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
010230405000102030405060708090a0b0c0d0e0g
1 change: 1 addition & 0 deletions demofiles/InvalidKeyLength-example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
000102030405060708090a0b0c0d0e
26 changes: 16 additions & 10 deletions src/main/java/CryptoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,20 @@ public static byte[] keyExtract(File keyFile) throws Exception{
return keyBytes;
}

public static int hexDigit(char ch) {
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;

return(0); // any other char is treated as 0
}
public static int hexDigit(char ch) throws Exception{
int ret;

if (ch >= '0' && ch <= '9')
ret = ch - '0';
else if (ch >= 'A' && ch <= 'F')
ret = ch - 'A' + 10;
else if (ch >= 'a' && ch <= 'f')
ret = ch - 'a' + 10;
else ret = -1;

if(ret == -1){
throw new CryptoException("Invalid key format", new Exception());
}
return ret;
}
}

0 comments on commit f1bd1f6

Please sign in to comment.