1
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class EncryptDecryptExample
{
    // "thisIsASecretKey";
    private static byte[] key = { 0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65,
            0x79 };

    public static void main(String[] args) throws Exception
    {
        //********************WORKS**********************
        String x = "Hello";
        System.out.println("Plain Text: " + x);
        String e = EncryptString(x);
        System.out.println("Encrypted: " + e);
        String d = decryptString(e);
        System.out.println("Deccypted: " + d);

        //********************WORKS**********************
        Byte b = 124;
        System.out.println("Plain Byte: "+b.toString());
        String eb = EncryptString(b.toString());
        System.out.println("Encrypted Byte: "+eb);
        String bd = decryptString(eb);
        System.out.println("Decrypted Byte: "+bd);

        //********************DOESNT*WORK*********************
        Byte[] bArray = {23, 42, 55};
        System.out.println("Plain Byte Array: "+bArray[0].toString()+","+bArray[1].toString()+","+bArray[2].toString());
        String eba = EncryptString(bArray.toString());
        System.out.println("Encrypted Byte Array: "+eba.toString());
        String deba = decryptString(eba.toString());
        System.out.println("Decrypted Byte Array: "+deba.getBytes()[0]);  //<--- Doesn't work
        //*********************************************
    }

    public static String EncryptString(String strToEncrypt) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    }

    public static String decryptString(String strToDecrypt) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    }

}

結果:

Plain Text: Hello
Encrypted: VKzhMlqHstjsGJHhbFS5wA==
Deccypted: Hello
Plain Byte: 124
Encrypted Byte: fuRAfj1yLXnEp+g25a1iYg==
Decrypted Byte: 124
Plain Byte Array: 23,42,55
Encrypted Byte Array: lURk/e+MIt6xa9s3wBnmKxyiuOmM/6JfwX6ujttNqWw=
Decrypted Byte Array: 91 <--- ?? why is this wrong

バイト配列からバイトを正しく取得できないのはなぜですか?

---->注

1つ、すべての強力な復号化と1つの暗号化方式が必要です。さまざまなメソッドシグネチャを使用してこれを実行できることを認識しています。しかし、私はオーバーロードされた関数は必要ありません。

4

3 に答える 3

2

配列のTotoStringメソッドは、配列の内容を文字列として提供しません。それは単にあなたにかなりあいまいな参照識別子を提供します。

あなたの問題は次の行にあります:

Byte[] bArray = {23, 42,   
String eba = EncryptString(bArray.toString());

Arrays.toString()メソッドを使用して、バイト配列を暗号化可能な文字列に変換することを検討してください。

String eba = EncryptString(Arrays.toString(bArray));

更新: @jlordoが以下で指摘しているように、同じエラーのバリエーションが以下の行に存在します。

System.out.println("Encrypted Byte Array: "+eba.toString());
String deba = decryptString(eba.toString());
于 2013-01-10T15:10:44.827 に答える
1

最後のブロックを変更することで、目的の結果を得ることができます。

...
    //*********************************************
    byte[] bArray = {23, 42, 55};
    String stringRepresentation = bArray[0] + "," + bArray[1] + "," + bArray[2];
    System.out.println("Plain Byte Array: " + Arrays.toString(bArray));
    String eba = EncryptByteArray(bArray);
    System.out.println("Encrypted Byte Array: "+eba);
    byte[] deba = decryptByteArray(eba);
    System.out.println("Decrypted Byte Array: "+Arrays.toString(deba));
    //*********************************************
}

public static String EncryptByteArray(byte[] array) throws Exception
{
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    String encryptedString = Base64.encodeBase64String(cipher.doFinal(array));
    return encryptedString;
}

public static byte[] decryptByteArray(String strToDecrypt) throws Exception
{
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
    SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(Base64.decodeBase64(strToDecrypt));
}
于 2013-01-10T15:14:37.640 に答える
1

crypt関数の入力と出力としてbyte[]を使用することをお勧めします。オーバーロードする必要はありません。byte []で動作する暗号化関数は、任意のデータ型を格納および取得できます。

私の知る限り、Arrays.toString(byte [])の単純な逆はありません。逆の場合は、多くの変更を加えずにプログラムを続行できます。しかし、ありません。

あなたはこのようなものが欲しいでしょう:

byte[] bytes = ...;
String stringRepresentationOfBytes = Arrays.toString(bytes);
byte[] stringRepresentationOfBytesConvertedBackToByteArray = 
    ByteArray.fromString(stringRepresentationOfBytes);

しかし、そのようなものはありませんByteArray.fromString(String)

crypt関数に出入りする基本的なデータ型としてbyte[]を使用することをお勧めします。

public class SOEncryptDecryptExampleBytes
{
    // "thisIsASecretKey";
    private static byte[] key = { 0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79 };

    private static final String STRING_ENCODING = "UTF-8";

    public static void main(String[] args) throws Exception
    {
        //*********************************************
        String x = "Hello";
        System.out.println("Plain Text: " + x);
        String e = encryptBytesAndBase64Encode(x.getBytes(STRING_ENCODING));
        System.out.println("Encrypted: " + e);
        byte[] d = base64decodeAndDecryptBytes(e);
        System.out.println("Decrypted: " + new String(d, STRING_ENCODING));

        //*********************************************
        byte b = 124;
        System.out.println("Plain Byte: " + b);
        String eb = encryptBytesAndBase64Encode(new byte[] { b });
        System.out.println("Encrypted Byte: " + eb);
        byte[] bd = base64decodeAndDecryptBytes(eb);
        System.out.println("Decrypted Byte: " + bd[0]);

        //*********************************************
        byte[] bArray = { 23, 42, 55 };
        System.out.println("Plain Byte Array: " + Arrays.toString(bArray));
        String eba = encryptBytesAndBase64Encode(bArray);
        System.out.println("Encrypted Byte Array: " + eba);
        byte[] deba = base64decodeAndDecryptBytes(eba);
        System.out.println("Decrypted Byte Array: " + Arrays.toString(deba));
        //*********************************************
    }

    /**
     * Transforms a byte[] into an encrypted byte[] and then uses base64 encodes the encrypted byte[]
     */
    public static String encryptBytesAndBase64Encode(byte[] bytes) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        String encryptedString = Base64.encodeBase64String(cipher.doFinal(bytes));
        return encryptedString;
    }

    /**
     * Base64 decodes a string into a byte[] and then decrypts those bytes into a decrypted byte[]
     */
    public static byte[] base64decodeAndDecryptBytes(String base64EncodedEncryptedBytes) throws Exception
    {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.decodeBase64(base64EncodedEncryptedBytes));
        return decryptedBytes;
    }
}
于 2013-01-10T15:27:22.910 に答える