惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
S
Schneier on Security
The Hacker News
The Hacker News
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
I
Intezer
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
O
OpenAI News
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
阮一峰的网络日志
阮一峰的网络日志
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Latest news
Latest news
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
罗磊的独立博客
WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security

kikcat

电商系统的高并发库存扣减 How to Make a Multiprocessor Computer That Correctly Executes Multiprocess Progranm Java多态原理 - JVM的静态分派和动态分派 解决黑苹果Monterey蓝牙睡眠后不工作问题 恒星的演变过程 EIP55-以太坊账户地址校验算法 手把手使用Java实现一个Socks5代理 理解socks5协议的工作过程和协议细节 理解Java中的Bridge Method
shadowsocks rc4-md5算法介绍
kikcat · 2021-12-08 · via kikcat

shadowsocks协议早期使用RC4加密算法用于加密数据,不过因为每次数据都适用同一个密钥流加密,存在很大的安全隐患,后面更新了RC4-MD5算法。

即便关于RC4-MD5协议的资料很难找到,而且RC4-MD5算法现在也不在安全,但还是有必要介绍一遍,因为它简单,很便于理解,在后期也会展示RC4算法的缺陷。

RC4-MD5本质上还是RC4对数据进行加密。MD5的意思是对key进行MD5运算,得到RC4的key,最终会被RC4算法用于生成密钥流。简单来说,用户的密码会经过下面步骤转换为RC4的keystream

  1. k1 = toBytes(password)
  2. k2 = ssKey(k1)
  3. rc4-md5-key = md5(toBytes(k2,iv))

上面步骤中,第一步把用户输入的密码转换为字节数组,第二步通过ss函数生成通用的ssKey,第三步生成RC4-MD5算法所需的key,过程就是把sskey和一个随机iv按顺序放到一个字节数组,在使用md5进行运算,得到一个最终的结果,这个Key就是RC4算法将要使用的Key。

sskey的生成算法如下

private static byte[] generateSSKey(String password) {
  byte[] keys = new byte[32];
  byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
  byte[] hash = Hash.md5(bytes);
  byte[] tmp = new byte[bytes.length + hash.length];
  System.arraycopy(hash, 0, keys, 0, hash.length);

  for (int i=hash.length;i<keys.length;i+=hash.length) {
    System.arraycopy(hash, 0, tmp, 0, hash.length);
    System.arraycopy(bytes, 0, tmp, hash.length, bytes.length);
    hash = Hash.md5(tmp);
    System.arraycopy(hash, 0, keys, i, hash.length);
  }

  return keys;
}

rc4-md5-key生成算法如下

public static byte[] rc4md5Key(String password,byte[] iv) {
  byte[] ssKeys = generateSSKey(password);
  byte[] keys = new byte[ssKeys.length + iv.length];
  System.arraycopy(ssKeys,0,keys,0,ssKeys.length);
  System.arraycopy(iv,0,keys,ssKeys.length,iv.length);

  return Hash.md5(keys);
}

当我们得到Key后,就可以使用它来对数据进行加密

public byte[] encrypt(byte[] keys,byte[] plaintext) throw Exception{
	Cipher encoder = Cipher.getInstance("RC4");
  encoder.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keys, "RC4"));
  return encoder.update(content.getBytes());
}

当我们得到密文后,还需要把IV传给ss-server,ss-server会使用同样的算法计算出rc4-md5-key,从而对数据进行解密,组装方式如下:

public void combine() {
  String plaintext = "123456";
  String password = "654321";
  byte[] iv = randomIV();
  
  byte[] key = rc4md5Key(password,iv);
  byte[] ciphertext = encrypt(key,plaintext.getBytes());
  ByteArrayOutputStream os = new ByteArrayOutputStream(key.length + ciphertext.length);
  os.writeBytes(key);
  os.writeBytes(ciphertext);
  
  byte[] data = os.toByteArray();
}

最终我们得到的data就是需要发送给服务端的数据。

虽然RC4-MD5算法早就可以被识别了,不过它足够简单,可以帮助我们理解为什么流加密很脆弱,作为一种入门的算法是不二之选。后续的文章将会继续介绍RC4算法的原理,以及对它的攻击方式。