0

私は

String X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781

私はまず X.getBytes(); によって文字列 X をバイト [] にします。そして、これを使用してRC4暗号化を行います..

public static byte[] RC4(byte[] x,byte[] keyBytes)   
{   
    byte[] e = null; 

    try   
    {   
        SecureRandom sr = new SecureRandom(keyBytes);
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(sr);
        SecretKey sk = kg.generateKey();  
        Cipher enCipher = Cipher.getInstance("RC4");   
        enCipher.init(Cipher.ENCRYPT_MODE,sk);   
        e = enCipher.doFinal(plaintext);              
    }   
    catch(Exception ex)   
    {   
        ex.printStackTrace();   
    }   
    return e;   
} 

編集**私が使用した暗号化の後、cipher = encrpyted.toString(); これは私に値を返しますcipher = [B@a1c582

その後、RC4 を使用して復号化を実行し、toString 関数を実行して、上記の文字列 X の元の値を取得しようとしましたが、役に立ちませんでした..

私がやったこと..編集**

//the reason i send using string is due to me having a client server architecture restriction on buffered writer, and this is the receiving side

            message = stdIn.readLine();
            System.out.println("Message received : " + message);

            byte [] kc = key.getBytes();
            byte [] decrypt = message.getBytes();
            byte [] decryptC = EncryptionScheme.decrypt(decrypt, kc);
            X = new String(decryptC);
            System.out.println("String X = " + X);

Message received : [B@a1c582

String X = ����,��

これを解決する方法はありますか?の文字列を取得したいString X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781

decryption algorithm

    public static byte[] decrypt(byte[] ciphertext,byte[] keyBytes)   
    {   
        byte de[] = null;   
        try   
        {   
            SecureRandom sr = new SecureRandom(keyBytes);
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            kg.init(sr);
            SecretKey sk = kg.generateKey();    
            Cipher deCipher = Cipher.getInstance("RC4");   
            deCipher.init(Cipher.DECRYPT_MODE,sk);   
            de = deCipher.doFinal(ciphertext);   
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }    
        return de;   

    }  
4

2 に答える 2

0

より良い解決策が見つからない場合は、次のことができます

    StringBuilder s = new StringBuilder();
    for(byte b : arr) {
        s.append(Integer.toBinaryString(0x100 | 0xFF & b).substring(1, 9));
    }
于 2015-08-02T04:33:29.003 に答える
0

私が使用した暗号化の後cipher = encrpyted.toString()

これは無効です。これで得られるObject.toString()のは、オブジェクト クラス ( byte[]、 としてエンコードされた[B) とそのSystem.identityHashCode(). それがあなたの投稿から引き出すことができる唯一の結論であるネットワーク経由で送信されたものである場合、そもそも暗号文が含まれていないため、正しく復号化できない可能性があります.

Stringはバイナリ データのコンテナーではないため、base-64 または base-16 または base-36 エンコーディングの暗号文を送信し、受信側でデコードしてから復号化する必要があります。

于 2015-08-02T05:22:09.290 に答える