我目前在做Sipdroid的加解密,现在我已经成功进行了加密操作,经过测试可以达到加密效果。但是我在几个地方都尝试了解密,要么通讯双方没有声音,要么通讯双方有嘈杂的噪音。 我想就如何解密这个东西寻求帮助。 首先,我为 RC4 加密编写了一个名为 RC4encrypt.class 的类:
public class RC4encrypt {
public static byte[] encryptWithRC4(byte[] content, String key) throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
KeyGenerator keyGenerator = KeyGenerator.getInstance("RC4");
keyGenerator.init(secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(content);
}
public static byte[] decryptWithRC4(byte[] cipherText, String key) throws Exception {
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
KeyGenerator keyGenerator = KeyGenerator.getInstance("RC4");
keyGenerator.init(secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(cipherText);
}
}
然后我给 RtpSocket.class 写了一个函数 send_Encrypt:
private byte[] send_Encrypt(byte[] data ,int header_len){
byte [] header_byte = new byte[data.length];
byte [] body_byte = new byte[data.length - header_len];
for(int i = 0 ;i<header_len-1;i++){
header_byte[i] = data[i];
}
for(int i =0 ;i<(data.length - header_len)-1;i++){
body_byte[i] = data[header_len+i];
}
try {
body_byte = RC4encrypt.encryptWithRC4(body_byte,"gfhfdhgdfgh");
} catch (Exception e) {
e.printStackTrace();
}
for(int i = 0 ;i<body_byte.length-1;i++){
header_byte[header_len+i] = body_byte[i];
}
return header_byte;
}
之后我对 RtpSocket.class 中的 send 函数做了一些修改:
public void send(RtpPacket rtpp) throws IOException {
byte[] data = rtpp.packet;
int len = rtpp.getHeaderLength();
data = send_Encrypt(data,len);
datagram.setData(data);
datagram.setLength(rtpp.packet.length);
datagram.setAddress(r_addr);
datagram.setPort(r_port);
socket.send(datagram);
}
我用修改后的程序和原程序通话,一侧声音是噪音,另一侧可以听到正常通话,说明加密成功,但是我用同样的方法修改receive功能时就可以了无法实现。那么有人可以指导我吗?