作者论坛账号:正己
1.密码学基础
2.常见编码与算法
3.非标准加密对抗
1.教程Demo(更新)
2.MT管理器/NP管理器
3.算法助手
4.雷电模拟器
5.Android Studio
6.jadx-gui
CTF
爬虫:算法还原、协议分析
网络安全:渗透测试、安全防护
密码学(cryptography)是一种将信息表述为不可读的方式,并使用一种秘密的方法将信息恢复出来的科学。密码学提供的最基本的服务是数据机密性服务,就是使通信双方可以互相发送消息,并且避免他人窃取消息的内容。加密算法是密码学的核心。
明文:原始消息
密文:加密后的消息
加密:从明文到密文的变换过程
解密:从密文到明文的变换过程
密钥:相用来完成加解密等过程的秘密信息
CyberChef
定义:
Base64是一种用64个字符表示任意二进制数据的方法,是一种编码,并非加密字符编码,由 A-Z a-z 0-9 + / 和补充字符 “=” 组成,Base64编码后的字符数是4的倍数(不足会补"=")
明文:
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 5ZC+54ix56C06Kej6K665Z2b
复制代码 隐藏代码 import java.util.Base64; public class Base64Example {
public static void main(String[] args) {
String text = "吾爱破解论坛";// 编码
String encodedString = Base64.getEncoder().encodeToString(text.getBytes());
System.out.println("Encoded string: " + encodedString);// 解码
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println("Decoded string: " + decodedString);
}
}
复制代码 隐藏代码 public class Base64Example {
//base64码表
private static final String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";public static void main(String[] args) {
String originalInput = "吾爱破解论坛";// 编码
String encodedString = encodeBase64(originalInput.getBytes());
System.out.println("Encoded string: " + encodedString);// 解码
byte[] decodedBytes = decodeBase64(encodedString);
String decodedString = new String(decodedBytes);
System.out.println("Decoded string: " + decodedString);
}private static String encodeBase64(byte[] inputBytes) {
StringBuilder sb = new StringBuilder();
int paddingCount = (3 - inputBytes.length % 3) % 3;for (int i = 0; i < inputBytes.length; i += 3) {
int b = ((inputBytes[i] & 0xFF) << 16) | ((i + 1 < inputBytes.length ? inputBytes[i + 1] & 0xFF : 0) << 8) | (i + 2 < inputBytes.length ? inputBytes[i + 2] & 0xFF : 0);
sb.append(base64Chars.charAt((b >> 18) & 0x3F)).append(base64Chars.charAt((b >> 12) & 0x3F)).append(base64Chars.charAt((b >> 6) & 0x3F)).append(base64Chars.charAt(b & 0x3F));
}for (int i = 0; i < paddingCount; i++) {
sb.setCharAt(sb.length() - i - 1, '=');
}return sb.toString();
}private static byte[] decodeBase64(String inputString) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int paddingCount = 0;for (int i = 0; i < inputString.length(); i += 4) {
int b = (base64Chars.indexOf(inputString.charAt(i)) << 18) | (base64Chars.indexOf(inputString.charAt(i + 1)) << 12) | (i + 2 < inputString.length() && inputString.charAt(i + 2) != '=' ? base64Chars.indexOf(inputString.charAt(i + 2)) << 6 : 0) | (i + 3 < inputString.length() && inputString.charAt(i + 3) != '=' ? base64Chars.indexOf(inputString.charAt(i + 3)) : 0);
bos.write((b >> 16) & 0xFF);
bos.write((b >> 8) & 0xFF);
bos.write(b & 0xFF);if (inputString.charAt(i + 2) == '=') {
paddingCount++;
}if (inputString.charAt(i + 3) == '=') {
paddingCount++;
}
}byte[] result = bos.toByteArray();
if (paddingCount > 0) {
byte[] trimmedResult = new byte[result.length - paddingCount];
System.arraycopy(result, 0, trimmedResult, 0, trimmedResult.length);
return trimmedResult;
} else {
return result;
}
}
}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 E590BEE788B1E7A0B4E8A7A3E8AEBAE59D9B
%02X
是一个格式化字符串,其中 %
是转义符,02
是最小宽度,表示输出的字符串至少包含两个字符,不足两个字符时用 0
填充,X
表示输出的字符集为大写的十六进制数。(算是一个特征)复制代码 隐藏代码 // 将普通字符串转换为Hex字符串
public static String stringToHex(String input) {
StringBuilder output = new StringBuilder();
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
output.append(String.format("%02X", b));
}
return output.toString();
}
// 将Hex字符串转换为普通字符串
public static String hexToString(String input) {
byte[] bytes = new byte[input.length() / 2];
for (int i = 0; i < input.length(); i += 2) {
bytes[i / 2] = (byte) Integer.parseInt(input.substring(i, i + 2), 16);
}
return new String(bytes, StandardCharsets.UTF_8);
}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 \u543E\u7231\u7834\u89E3\u8BBA\u575B
复制代码 隐藏代码 // 将字符串转换为Unicode格式
public static String stringToUnicode(String input) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
output.append(String.format("\\u%04X", (int) input.charAt(i)));
}
return output.toString();
}
// 将Unicode格式的字符串转换为原始字符串
public static String unicodeToString(String input) {
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i += 6) {
String str = input.substring(i + 2, i + 6);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 byte[] byteArray = new byte[]{-27,-112,-66,-25,-120,-79,-25,-96,-76,-24,-89,-93,-24,-82,-70,-27,-99,-101};
复制代码 隐藏代码 public static void main(String[] args) {
//stringtobyte数组
String originalInput = "吾爱破解论坛";
byte[] bytes = originalInput.getBytes();
System.out.println(Arrays.toString(bytes));//byte数组tostring
byte[] byteArray = new byte[]{-27,-112,-66,-25,-120,-79,-25,-96,-76,-24,-89,-93,-24,-82,-70,-27,-99,-101};
String str = new String(byteArray);
System.out.println(str);}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 0bc50e2bf301b3f7c4309a9f4c9b19b2
复制代码 隐藏代码 public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update("吾爱破解论坛".getBytes());
byte[] res = md.digest();
System.out.println(byteToHexString(res));
}
public static String byteToHexString(byte[] by) {
StringBuilder SB = new StringBuilder();for (int k : by) {
int j = k;
if (k < 0) {
j = k + 256;
}if (j < 16) {
SB.append("0");
}SB.append(Integer.toHexString(j));
}
return SB.toString();
}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 VPPMeI+jgyAtCYeM+VvamDapwXp+n4Q/oUHULimyLA4=
复制代码 隐藏代码 import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class Aes {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; //加密模式
private static final String SECRET_KEY = "1234567wuaipojie"; //密钥public static void main(String[] args) throws Exception {
String originalMessage = "吾爱破解论坛";// Encrypt the message
byte[] encryptedMessage = encrypt(originalMessage);
System.out.println("加密结果: " + Base64.getEncoder().encodeToString(encryptedMessage));// Decrypt the message
String decryptedMessage = decrypt(encryptedMessage);
System.out.println("解密结果: " + decryptedMessage);
}private static byte[] encrypt(String message) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(message.getBytes());
}private static String decrypt(byte[] encryptedMessage) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(encryptedMessage));
}}
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 QJBizO9/gNXe+fmKsWwHrkXExXultVO2
复制代码 隐藏代码 import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class Des {
private static final String ALGORITHM = "DES";
private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding"; //加密模式
private static final String SECRET_KEY = "52pj2023"; //密钥public static void main(String[] args) throws Exception {
String originalMessage = "吾爱破解论坛";// Encrypt the message
byte[] encryptedMessage = encrypt(originalMessage);
System.out.println("加密结果: " + Base64.getEncoder().encodeToString(encryptedMessage));// Decrypt the message
String decryptedMessage = decrypt(encryptedMessage);
System.out.println("解密结果: " + decryptedMessage);
}private static byte[] encrypt(String message) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(message.getBytes());
}private static String decrypt(byte[] encryptedMessage) throws Exception {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(encryptedMessage));
}}
PS:RSA公钥加密的每次结果都不一样
复制代码 隐藏代码 吾爱破解论坛
复制代码 隐藏代码 bLZtNVaHFLeKnQDG79KzOuvPa1HwGvcKX5fJfWR09g0HsAzM7NX7pcOzDp46UL4vOP/n1/DKxYpxxA8CjfxDdR8mNb4I3eNDq7oRZzQmDYKK+98uMuDc0+3utMuhojSdTe3cC6bWy66xcyV9LgX0Cau0sIDzBk9c7by6VCwj7xs=
复制代码 隐藏代码 public class RSA {
public static final String KEY_ALGORITHM = "RSA";private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
// 1024 bits 的 RSA 密钥对,最大加密明文大小
private static final int MAX_ENCRYPT_BLOCK = 117;// 1024 bits 的 RSA 密钥对,最大解密密文大小
private static final int MAX_DECRYPT_BLOCK = 128;// 生成密钥对
public static Map<String, Object> initKey(int keysize) throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 设置密钥对的 bit 数,越大越安全
keyPairGen.initialize(keysize);
KeyPair keyPair = keyPairGen.generateKeyPair();// 获取公钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// 获取私钥
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}// 获取公钥字符串
public static String getPublicKeyStr(Map<String, Object> keyMap) {
// 获得 map 中的公钥对象,转为 key 对象
Key key = (Key) keyMap.get(PUBLIC_KEY);
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}// 获取私钥字符串
public static String getPrivateKeyStr(Map<String, Object> keyMap) {
// 获得 map 中的私钥对象,转为 key 对象
Key key = (Key) keyMap.get(PRIVATE_KEY);
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}// 获取公钥
public static PublicKey getPublicKey(String publicKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] publicKeyByte = Base64.getDecoder().decode(publicKeyString);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyByte);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
return keyFactory.generatePublic(keySpec);
}// 获取私钥
public static PrivateKey getPrivateKey(String privateKeyString) throws Exception {
byte[] privateKeyByte = Base64.getDecoder().decode(privateKeyString);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyByte);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
return keyFactory.generatePrivate(keySpec);
}/**
* BASE64 编码返回加密字符串
*
* home.php?mod=space&uid=952169 key 需要编码的字节数组
* home.php?mod=space&uid=155549 编码后的字符串
*/
public static String encryptBASE64(byte[] key) {
return new String(Base64.getEncoder().encode(key));
}/**
* BASE64 解码,返回字节数组
*
* @param key 待解码的字符串
* @return 解码后的字节数组
*/
public static byte[] decryptBASE64(String key) {
return Base64.getDecoder().decode(key);
}/**
* 公钥加密
*
* @param text 待加密的明文字符串
* @param publicKeyStr 公钥
* @return 加密后的密文
*/
public static String encrypt1(String text, String publicKeyStr) {
try {System.out.println("明文字符串为:"+text);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKeyStr));
byte[] tempBytes = cipher.doFinal(text.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(tempBytes);
} catch (Exception e) {
throw new RuntimeException("加密字符串[" + text + "]时遇到异常", e);
}
}/**
* 私钥解密
*
* @param secretText 待解密的密文字符串
* @param privateKeyStr 私钥
* @return 解密后的明文
*/
public static String decrypt1(String secretText, String privateKeyStr) {
try {
// 生成私钥
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyStr));
// 密文解码
byte[] secretTextDecoded = Base64.getDecoder().decode(secretText.getBytes("UTF-8"));
byte[] tempBytes = cipher.doFinal(secretTextDecoded);
return new String(tempBytes);
} catch (Exception e) {
throw new RuntimeException("解密字符串[" + secretText + "]时遇到异常", e);
}
}public static void main(String[] args) throws Exception {
Map<String, Object> keyMap;
String cipherText;
// 原始明文
String content = "吾爱破解论坛";// 生成密钥对
keyMap = initKey(1024);
String publicKey = getPublicKeyStr(keyMap);
System.out.println("公钥:"+publicKey);
String privateKey = getPrivateKeyStr(keyMap);
System.out.println("私钥:"+privateKey);// 加密
cipherText = encrypt1(content, publicKey);
System.out.println("加密后的密文:"+cipherText);// 解密
String plainText = decrypt1(cipherText, privateKey);
System.out.println("解密后明文:"+plainText);
}}
点击左下角阅读原文,进入 B站 在线观看视频教程,欢迎一键三连转发~
-官方论坛
www.52pojie.cn
--推荐给朋友
公众微信号:吾爱破解论坛
或搜微信号:pojie_52