hexToBytes

@interface NSString (NSStringHexToBytes)
-(NSData*) hexToBytes ;
@end






@implementation NSString (NSStringHexToBytes)
-(NSData*) hexToBytes {
  NSMutableData* data = [NSMutableData data];
  int idx;
  for (idx = 0; idx+2 <= self.length; idx+=2) {
    NSRange range = NSMakeRange(idx, 2);
    NSString* hexStr = [self substringWithRange:range];
    NSScanner* scanner = [NSScanner scannerWithString:hexStr];
    unsigned int intValue;
    [scanner scanHexInt:&intValue];
    [data appendBytes:&intValue length:1];
  }
  return data;
}

@end



/// example
unsigned char bytes[] = { 0x11, 0x56, 0xFF, 0xCD, 0x34, 0x30, 0xAA, 0x22 };
NSData* expectedData = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSLog(@"data %@", [@"1156FFCD3430AA22" hexToBytes]);
NSLog(@"expectedData isEqual:%d", [expectedData isEqual:[@"1156FFCD3430AA22" hexToBytes]]);

import MessageDigest; import NoSuchAlgorithmException; public class FinalShellDecryptor { public static void main(String[] args) { String encryptedPassword = "f3b4094d6e7e3e1c2c5a0c5f5b494d5f5f070f47494e5e4f4d4f4d010e0f4d5e"; String key = "finalShellKey"; for (int i = 0; i <= 9999; i++) { String password = String.format("%04d", i); String decryptedPassword = decryptFinalShell(encryptedPassword, key, password); if (decryptedPassword != null) { System.out.println("Decrypted password: " + decryptedPassword); break; } } } public static String decryptFinalShell(String encryptedPassword, String key, String password) { try { byte[] encryptedBytes = hexToBytes(encryptedPassword); byte[] keyBytes = hashSHA256(key.getBytes()); byte[] decryptedBytes = new byte[encryptedBytes.length]; for (int i = 0; i < encryptedBytes.length; i++) { decryptedBytes[i] = (byte) (encryptedBytes[i] ^ keyBytes[i % keyBytes.length]); } String decryptedPassword = new String(decryptedBytes); if (hashSHA256(password.getBytes()).equals(hashSHA256(decryptedPassword.getBytes()))) { return decryptedPassword; } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static byte[] hexToBytes(String hex) { int length = hex.length(); byte[] bytes = new byte[length / 2]; for (int i = 0; i < length; i += 2) { bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16)); } return bytes; } public static byte[] hashSHA256(byte[] bytes) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-256"); return digest.digest(bytes); } }
03-18
const GMCrypt = require('gm-crypt').sm4; /** * SM4 解密函数(ECB模式 + PKCS5填充) * @param {string} base64Str - Base64编码的密文 * @param {string} key - 16字节密钥(HEX或字符串格式) * @returns {string|Uint8Array} - 解密后的数据(自动检测文本或二进制) */ export function decrypt(base64Str, key = 'HTHTqQq314159@26') { // SM4 配置(ECB模式 + PKCS5填充) const sm4Config = { key: key, mode: 'ecb', cipherType: 'base64', padding: 'pkcs5' }; const sm4 = new GMCrypt(sm4Config); try { // 1. 执行SM4解密(返回HEX字符串) const decryptedHex = sm4.decrypt(base64Str); // 2. 转换为字节数组 const bytes = hexToBytes(decryptedHex); // 3. 安全处理:过滤无效Unicode代码点 const safeBytes = bytes.filter(byte => byte <= 0x10FFFF); // 4. 尝试解码为UTF-8文本 try { return bytesToUtf8(safeBytes); } catch (textError) { // 解码失败则返回原始HEX数据 console.warn('解密结果非UTF-8文本,返回原始HEX数据'); return decryptedHex; } } catch (decryptError) { console.error('解密失败:', decryptError.message); throw new Error('SM4解密过程出错'); } } // HEX转字节数组 function hexToBytes(hex) { const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { bytes[i / 2] = parseInt(hex.substr(i, 2), 16); } return bytes; } // 字节数组转UTF-8字符串 function bytesToUtf8(bytes) { const decoder = new TextDecoder('utf-8'); return decoder.decode(bytes); }这段代码 和我本地的这个段代码 合并一下 // utils/sm4.ts import { sm4 } from 'sm-crypto'; // 配置 const sm4Config = { key: new TextEncoder().encode('b367d464d97caee1'), mode: 'ecb', cipherType: 'base64' }; // const sm4 = new SM4(sm4Config); export function encrypt(text) { // 返回 Base64,mode 和 gm-crypt 一致 return sm4.encrypt(text, sm4Config.key, { mode: 'ecb', cipherType: 'base64' }); } export function decrypt(base64Str) { // SM4 配置(ECB模式 + PKCS5填充) const sm4Config = { key: key, mode: 'ecb', cipherType: 'base64', padding: 'pkcs5' }; const sm4 = new GMCrypt(sm4Config); try { // 1. 执行SM4解密(返回HEX字符串) const decryptedHex = sm4.decrypt(base64Str); // 2. 转换为字节数组 const bytes = hexToBytes(decryptedHex); // 3. 安全处理:过滤无效Unicode代码点 const safeBytes = bytes.filter(byte => byte <= 0x10FFFF); // 4. 尝试解码为UTF-8文本 try { return bytesToUtf8(safeBytes); } catch (textError) { // 解码失败则返回原始HEX数据 console.warn('解密结果非UTF-8文本,返回原始HEX数据'); return decryptedHex; } } catch (decryptError) { console.error('解密失败:', decryptError.message); throw new Error('SM4解密过程出错'); } } // 工具函数:hex 转 utf8 字符串 function hexToUtf8(hex) { const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { bytes[i / 2] = parseInt(hex.substr(i, 2), 16); } const decoder = new TextDecoder('utf-8'); return decoder.decode(bytes); }
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值