Skip to content

Instantly share code, notes, and snippets.

@renzhexigua
Last active April 6, 2024 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renzhexigua/eb57781a875523d1c92fe40544fa1751 to your computer and use it in GitHub Desktop.
Save renzhexigua/eb57781a875523d1c92fe40544fa1751 to your computer and use it in GitHub Desktop.
XXX Login activity
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public final class Base64Util {
private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
public static String encode(byte[] data) {
int d;
int len = data.length;
StringBuffer buf = new StringBuffer((data.length * 3) / 2);
int end = len - 3;
int i = 0;
int n = 0;
while (i <= end) {
d = (((data[i] & 255) << 16) | ((data[i + 1] & 255) << 8)) | (data[i + 2] & 255);
buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append(legalChars[d & 63]);
i += 3;
int n2 = n + 1;
if (n >= 14) {
n2 = 0;
buf.append(" ");
}
n = n2;
}
if (i == (0 + len) - 2) {
d = ((data[i] & 255) << 16) | ((data[i + 1] & 255) << 8);
buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append(legalChars[(d >> 6) & 63]);
buf.append("=");
} else if (i == (0 + len) - 1) {
d = (data[i] & 255) << 16;
buf.append(legalChars[(d >> 18) & 63]);
buf.append(legalChars[(d >> 12) & 63]);
buf.append("==");
}
return buf.toString();
}
private static int decode(char c) {
if (c >= 'A' && c <= 'Z') {
return c - 65;
}
if (c >= 'a' && c <= 'z') {
return (c - 97) + 26;
}
if (c >= '0' && c <= '9') {
return ((c - 48) + 26) + 26;
}
switch (c) {
case '+':
return 62;
case '/':
return 63;
case '=':
return 0;
default:
throw new RuntimeException("unexpected code: " + c);
}
}
public static byte[] decode(String s) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
decode(s, bos);
byte[] decodedBytes = bos.toByteArray();
try {
bos.close();
} catch (IOException ex) {
System.err.println("Error while decoding BASE64: " + ex.toString());
}
return decodedBytes;
} catch (IOException e) {
throw new RuntimeException();
}
}
private static void decode(String s, OutputStream os) throws IOException {
int i = 0;
int len = s.length();
while (true) {
if (i < len && s.charAt(i) <= ' ') {
i++;
} else if (i != len) {
int tri = (((decode(s.charAt(i)) << 18) + (decode(s.charAt(i + 1)) << 12)) + (decode(s.charAt(i + 2)) << 6)) + decode(s.charAt(i + 3));
os.write((tri >> 16) & 255);
if (s.charAt(i + 2) != '=') {
os.write((tri >> 8) & 255);
if (s.charAt(i + 3) != '=') {
os.write(tri & 255);
i += 4;
} else {
return;
}
}
return;
} else {
return;
}
}
}
public static String filter(String str) {
String output = "";
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
int asc = str.charAt(i);
if (!(asc == 10 || asc == 13)) {
sb.append(str.subSequence(i, i + 1));
}
}
return new String(sb);
}
}
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.lang.Exception;
import java.util.Date;
import javax.crypto.Cipher;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
class Main {
private static String ALGORITHM = "RSA/ECB/PKCS1Padding";
public static void main(String[] args) throws Exception {
String source = PasswordSalt("password");
System.out.println(encrypt(source));
}
public static String PasswordSalt(String password) {
long salt = new Date().getTime();
return "{\"password\":\"" + password + "\",\"salt\":" + salt + '}';
}
public static String getiOSKey(Key key) {
return new String(Base64Util.encode(new PKCS8EncodedKeySpec(key.getEncoded()).getEncoded()));
}
public static void getPublicComponents() throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./PublicKey"));
Key key = (Key) ois.readObject();
ois.close();
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPKS = kf.getKeySpec(key, RSAPublicKeySpec.class);
System.out.println(rsaPKS.getModulus().toString(16));
System.out.println(rsaPKS.getPublicExponent());
}
public static String encrypt(String source) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./PublicKey"));
Key key = (Key) ois.readObject();
ois.close();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(1, key);
return Base64Util.encode(cipher.doFinal(source.getBytes()));
}
public static String decrypt(String cryptograph) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./PrivateKey"));
Key key = (Key) ois.readObject();
ois.close();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(2, key);
return new String(cipher.doFinal(Base64Util.decode(cryptograph)));
}
}
@LoebalKyi
Copy link

Uploading 1708274817610.jpg…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment