이상한 문자가된다 "™ A를> * : 상점> ¥ % ¢"그 후에UDP 자바 소켓 프로그래밍은, 데이터의 수신에 그것이 내가 나에게이 제공하는 RC4 암호 통해 문자열 통과
, 나는 그것을 전송 이상이 코드
socket.receive(packet);
test = packet.getData();
System.out.println(new String(test));
을
DatagramPacket C1C2 = new DatagramPacket(RC4cipherData, RC4cipherData.length, IPAddress,9876);
clientSocket.send(C1C2);
그래서 내가 호스트/서버 측면에서받은 후이 코드를 사용하여 사용하여 내가 얻을이 *> 난에 시도 RC4를 사용하여 암호를 해독했지만 실패했지만 usi 위의 암호문을 일반 텍스트로 되돌릴 수있었습니다. 너희들이
byte[] RC4cipherData = rc4.encryptMessage(RC4ptext, RK.toString());
그 이상한 문자로 설정하는 암호문을 원인은 무엇 설명한 사람이 수해야하는 경우
추가 데이터?
RC4 코드
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
* Implementation of RC4 stream cipher
*
* @author Iurii Sergiichuk
*/
public class RC4 {
private static final int SBOX_LENGTH = 256;
private static final int KEY_MIN_LENGTH = 5;
/**
* Key array
*/
private byte[] key = new byte[SBOX_LENGTH - 1];
/**
* Sbox
*/
private int[] sbox = new int[SBOX_LENGTH];
public RC4() {
reset();
}
public RC4(String key) throws InvalidKeyException {
this();
setKey(key);
}
private void reset() {
Arrays.fill(key, (byte) 0);
Arrays.fill(sbox, 0);
}
/**
* Encrypt given message String with given Charset and key
*
* @param message message to be encrypted
* @param charset charset of message
* @param key key
* @return encrypted message
* @throws InvalidKeyException if key length is smaller than 5 or bigger than 255
*/
public byte[] encryptMessage(String message, Charset charset, String key)
throws InvalidKeyException {
reset();
setKey(key);
byte[] crypt = crypt(message.getBytes());
reset();
return crypt;
}
/**
* Encrypt given message String with given Key and pre-defined UTF-8 charset
*
* @param message message to be encrypted
* @param key key
* @return encrypted message
* @throws InvalidKeyException if key length is smaller than 5 or bigger than 255
* @see StandardCharsets
*/
public byte[] encryptMessage(String message, String key)
throws InvalidKeyException {
return encryptMessage(message, StandardCharsets.UTF_8, key);
}
/**
* Decrypt given byte[] message array with given charset and key
*
* @param message message to be decrypted
* @param charset charset of message
* @param key key
* @return string in given charset
* @throws InvalidKeyException if key length is smaller than 5 or bigger than 255
*/
public String decryptMessage(byte[] message, Charset charset, String key)
throws InvalidKeyException {
reset();
setKey(key);
byte[] msg = crypt(message);
reset();
return new String(msg);
}
/**
* Decrypt given byte[] message array with given key and pre-defined UTF-8
* charset
*
* @param message message to be decrypted
* @param key key
* @return string in given charset
* @throws InvalidKeyException if key length is smaller than 5 or bigger than 255
* @see StandardCharsets
*/
public String decryptMessage(byte[] message, String key)
throws InvalidKeyException {
return decryptMessage(message, StandardCharsets.UTF_8, key);
}
/**
* Crypt given byte array. Be aware, that you must init key, before using
* crypt.
*
* @param msg array to be crypt
* @return crypted byte array
* @see <a href="http://en.wikipedia.org/wiki/RC4#Pseudo-random_generation_algorithm_.28PRGA.29">Pseudo-random generation algorithm</a>
*/
public byte[] crypt(final byte[] msg) {
sbox = initSBox(key);
byte[] code = new byte[msg.length];
int i = 0;
int j = 0;
for (int n = 0; n < msg.length; n++) {
i = (i + 1) % SBOX_LENGTH;
j = (j + sbox[i]) % SBOX_LENGTH;
swap(i, j, sbox);
int rand = sbox[(sbox[i] + sbox[j]) % SBOX_LENGTH];
code[n] = (byte) (rand^msg[n]);
}
return code;
}
/**
* Initialize SBOX with given key. Key-scheduling algorithm
*
* @param key key
* @return sbox int array
* @see <a href="http://en.wikipedia.org/wiki/RC4#Key-scheduling_algorithm_.28KSA.29">Wikipedia. Init sbox</a>
*/
private int[] initSBox(byte[] key) {
int[] sbox = new int[SBOX_LENGTH];
int j = 0;
for (int i = 0; i < SBOX_LENGTH; i++) {
sbox[i] = i;
}
for (int i = 0; i < SBOX_LENGTH; i++) {
j = (j + sbox[i] + (key[i % key.length]) & 0xFF) % SBOX_LENGTH;
swap(i, j, sbox);
}
return sbox;
}
private void swap(int i, int j, int[] sbox) {
int temp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = temp;
}
/**
* Setup key
*
* @param key key to be setup
* @throws InvalidKeyException if key length is smaller than 5 or bigger than 255
*/
public void setKey(String key) throws InvalidKeyException {
if (!(key.length() >= KEY_MIN_LENGTH && key.length() < SBOX_LENGTH)) {
throw new InvalidKeyException("Key length has to be between "
+ KEY_MIN_LENGTH + " and " + (SBOX_LENGTH - 1));
}
this.key = key.getBytes();
}
}
/**
* Exception made for recognise invalid keys
*
* @author Iurii Sergiichuk
*/
class InvalidKeyException extends RuntimeException {
private static final long serialVersionUID = -2412232436238451574L;
public InvalidKeyException(String message) {
super(message);
}
}
서버 코드
socket.receive(packet);
test = packet.getData();
// test = new byte[packet.getlength()];
System.out.println(new String(test));
byte[] RC4cipherData = rc4.encryptMessage(RC4ptext, RK.toString());
System.out.println("encryption: " + new String(RC4cipherData));
String RC4decryptData = rc4.decryptMessage(RC4cipherData, RK.toString());
System.out.println("decryption: " + RC4decryptData);
//time to send C1 and C2 encrypted to server/alice
String C1C2PText = encryptedRK.toString() + "%" + new String(RC4cipherData) + "%";
System.out.println("Test: " + C1C2PText);
byte [] C1C2data = C1C2PText.getBytes();
//System.out.println("Print conversion: " + C1C2data[0].);
// DatagramPacket C1C2 = new DatagramPacket(C1C2data, C1C2data.length, IPAddress,9876);
DatagramPacket C1C2 = new DatagramPacket(RC4cipherData, RC4cipherData.length, IPAddress,9876);
clientSocket.send(C1C2);
암호문은 단지 가공되지 않은 바이트이기 때문에 'new String (RC4cipherData)'는 'new String (test)'와 마찬가지로 의미가 없습니다. –