mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
General performance improvement (#6078)
This commit is contained in:
committed by
GitHub
parent
7b962a4a1d
commit
df0c997e4b
@@ -2418,8 +2418,6 @@ public final class AES {
|
||||
rBytes = new StringBuilder(rBytes.substring(0, i * 2) + currentByteBits + rBytes.substring((i + 1) * 2));
|
||||
}
|
||||
|
||||
// t = new BigInteger(rBytes, 16);
|
||||
// return t;
|
||||
return new BigInteger(rBytes.toString(), 16);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,19 +34,19 @@ final class AffineCipher {
|
||||
*/
|
||||
static String encryptMessage(char[] msg) {
|
||||
// Cipher Text initially empty
|
||||
String cipher = "";
|
||||
StringBuilder cipher = new StringBuilder();
|
||||
for (int i = 0; i < msg.length; i++) {
|
||||
// Avoid space to be encrypted
|
||||
/* applying encryption formula ( a * x + b ) mod m
|
||||
{here x is msg[i] and m is 26} and added 'A' to
|
||||
bring it in the range of ASCII alphabet [65-90 | A-Z] */
|
||||
if (msg[i] != ' ') {
|
||||
cipher += (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
|
||||
cipher.append((char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'));
|
||||
} else { // else simply append space character
|
||||
cipher += msg[i];
|
||||
cipher.append(msg[i]);
|
||||
}
|
||||
}
|
||||
return cipher;
|
||||
return cipher.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +56,7 @@ final class AffineCipher {
|
||||
* @return the decrypted plaintext message
|
||||
*/
|
||||
static String decryptCipher(String cipher) {
|
||||
String msg = "";
|
||||
StringBuilder msg = new StringBuilder();
|
||||
int aInv = 0;
|
||||
int flag;
|
||||
|
||||
@@ -75,13 +75,13 @@ final class AffineCipher {
|
||||
{here x is cipher[i] and m is 26} and added 'A'
|
||||
to bring it in the range of ASCII alphabet [65-90 | A-Z] */
|
||||
if (cipher.charAt(i) != ' ') {
|
||||
msg += (char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A');
|
||||
msg.append((char) (((aInv * ((cipher.charAt(i) - 'A') - b + 26)) % 26) + 'A'));
|
||||
} else { // else simply append space character
|
||||
msg += cipher.charAt(i);
|
||||
msg.append(cipher.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
return msg;
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
// Driver code
|
||||
|
||||
@@ -1078,7 +1078,7 @@ public class Blowfish {
|
||||
* @return String object which is a binary representation of the hex number passed as parameter
|
||||
*/
|
||||
private String hexToBin(String hex) {
|
||||
String binary = "";
|
||||
StringBuilder binary = new StringBuilder();
|
||||
long num;
|
||||
String binary4B;
|
||||
int n = hex.length();
|
||||
@@ -1089,9 +1089,9 @@ public class Blowfish {
|
||||
binary4B = "0000" + binary4B;
|
||||
|
||||
binary4B = binary4B.substring(binary4B.length() - 4);
|
||||
binary += binary4B;
|
||||
binary.append(binary4B);
|
||||
}
|
||||
return binary;
|
||||
return binary.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1103,12 +1103,12 @@ public class Blowfish {
|
||||
*/
|
||||
private String binToHex(String binary) {
|
||||
long num = Long.parseUnsignedLong(binary, 2);
|
||||
String hex = Long.toHexString(num);
|
||||
StringBuilder hex = new StringBuilder(Long.toHexString(num));
|
||||
while (hex.length() < (binary.length() / 4)) {
|
||||
hex = "0" + hex;
|
||||
hex.insert(0, "0");
|
||||
}
|
||||
|
||||
return hex;
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1121,12 +1121,12 @@ public class Blowfish {
|
||||
private String xor(String a, String b) {
|
||||
a = hexToBin(a);
|
||||
b = hexToBin(b);
|
||||
String ans = "";
|
||||
StringBuilder ans = new StringBuilder();
|
||||
for (int i = 0; i < a.length(); i++) {
|
||||
ans += (char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0');
|
||||
ans.append((char) (((a.charAt(i) - '0') ^ (b.charAt(i) - '0')) + '0'));
|
||||
}
|
||||
ans = binToHex(ans);
|
||||
return ans;
|
||||
ans = new StringBuilder(binToHex(ans.toString()));
|
||||
return ans.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user