-1

由于 byte[]-String 转换,我在加密和解密一些 AES 消息时遇到了一些麻烦……我发现 a[9]!=c[9] 非常有趣(在调试时看到了差异)

try {

        String encryptionKey = "1234567890123456";
        String plaintext = "1234567890123456";

        System.out.println("key:   " + encryptionKey);
        System.out.println("plain:   " + plaintext);
        byte[] a = aes.encrypt(plaintext, encryptionKey);
        String b = new String(a);
        byte[] c = b.getBytes();

        String decrypted = new String(aes.decrypt(c, encryptionKey));

        System.out.println("decrypt: " + decrypted);

    } catch (Exception e) {
      e.printStackTrace();
    } 
}


public byte[] encrypt(String plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes(), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes()));
    return cipher.doFinal(plainText.getBytes());
}
4

2 回答 2

0

实际上我想通过如下的 TCP 连接发送数据,但仍然有一些问题......我将服务器发送的字符串与客户端接收的字符串进行了比较,它们不匹配

服务器看起来像:

public class server {
static String aeskey;
static String secret;
public static void main(String[] args) throws Exception {

AES aes = new AES();

    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(1234);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 1234.");
        System.exit(1);
    }

    Socket clientSocket = null;
    try {
        clientSocket = serverSocket.accept();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    String int1,int2;

byte[] cipher = aes.encrypt("1234567890123456", "1234567890123456");
String s = new String(cipher , "ISO-8859-1");
out.println(s);

int1 = in.readLine();
System.out.println("From Client:"+int1);


    out.close();
    in.close();
    clientSocket.close();
    serverSocket.close();
}
}

客户端(在 Android 中尝试过)如下所示:

public class MainActivity extends Activity {

int cmd, ret;
public String IPAdress;
public String aeskey;
public String secret;
public Socket socket;
public PrintWriter out;
public BufferedReader in;
public AES aes = new AES();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button b1 = (Button)findViewById(R.id.button1);

    cmd = 0;
    b1.setText("Connect to server");

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(cmd == 0) ////setting-up the connection
            {
                int cs = connect_to_server();
                if(cs == 0)
                {
                    b1.setText("Connected");
                    try {
                        int dp  = do_protocol();
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    cmd = 1;
                    }
            }
            else 
            {
                ///to be edited later
            }
        }
    });

}

public int do_protocol() throws UnsupportedEncodingException
{
    int d = 0;
    BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    String num1 = null,num2 = null;

    aeskey=aes.generateRandomString(16); ///generez o cheie AES

    try {
        num1 = in.readLine();
    } catch (IOException e1) {
        d = 1;
        e1.printStackTrace();
    }

    byte[] cipher = null;
    cipher = num1.getBytes("ISO-8859-1");
    try {
        num2 = new String(aes.decrypt(cipher, aeskey));
    } catch (Exception e) {
        d = 2;
        e.printStackTrace();
    }
    out.println(num2);

    return d;
}

public int connect_to_server()
{
    ret = 0;
    try {
        socket = new Socket("192.168.1.144", 1234);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (UnknownHostException e) {
        Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
        ret = 1;
        } catch (IOException e) {
        Toast.makeText(this, "Unable to connect to server", Toast.LENGTH_LONG).show();
        ret = 2;
        }
    if(ret == 0)
        Toast.makeText(this, "Connected to server", Toast.LENGTH_LONG).show();
    return ret;
}
于 2013-04-27T03:42:13.770 回答
-1

这仅适用于byte[]包含对您的默认编码正确的字节。例如说它是windows-1251UTF-8 ,那么大多数 ASCII 0-127 字节都可以,但高于此的字节会被破坏或翻译成?

String b = new String(a);
byte[] c = b.getBytes();

试试吧

String b = new String(a, "ISO-8859-1");
byte[] c = b.getBytes("ISO-8859-1");

“ISO-8859-1”编码将 0-255 转换为 0-255,因此可以保留原始信息。

于 2013-04-26T15:44:26.073 回答