-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementazione Base36 e correzione bug
Dopo aver implementato la codifica Base36, è stato risolto il bug che obbligava l'utente a riavviare il programma quando inseriva una nuova chiave dopo la creazione di un nuovo keystore.
- Loading branch information
1 parent
4a6961f
commit 27de43f
Showing
5 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package it.HackerInside.TextEncryptionUtility; | ||
|
||
import java.math.BigInteger; | ||
|
||
public class Base36 { | ||
|
||
private static final char[] BASE_36_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); | ||
|
||
public static String encode(byte[] bytes) { | ||
BigInteger bigInt = new BigInteger(1, bytes); | ||
StringBuilder sb = new StringBuilder(); | ||
while (bigInt.compareTo(BigInteger.ZERO) > 0) { | ||
BigInteger[] quotientAndRemainder = bigInt.divideAndRemainder(BigInteger.valueOf(36)); | ||
sb.append(BASE_36_ALPHABET[quotientAndRemainder[1].intValue()]); | ||
bigInt = quotientAndRemainder[0]; | ||
} | ||
return sb.reverse().toString(); | ||
} | ||
|
||
public static byte[] decode(String base36String) { | ||
BigInteger bigInt = new BigInteger(base36String, 36); | ||
byte[] bytes = bigInt.toByteArray(); | ||
if (bytes[0] == 0) { | ||
byte[] trimmedBytes = new byte[bytes.length - 1]; | ||
System.arraycopy(bytes, 1, trimmedBytes, 0, trimmedBytes.length); | ||
return trimmedBytes; | ||
} | ||
return bytes; | ||
} | ||
|
||
} |
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
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