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

为什么我不能正确地从字节数组中检索我的字节?

---->注意

我想要一种,所有强大的解密和一种加密方法。我知道我可以使用不同的方法签名来做到这一点。但我不想要一个重载的函数。

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[] 作为 crypt 函数的输入和输出。你不需要超载。使用 byte[] 的 Crypt 函数将能够存储和检索任何数据类型。

据我所知,Arrays.toString(byte[]) 没有简单的逆。如果有相反的情况,您可以继续执行您的程序而无需进行很多修改。但是,没有。

你会想要这样的东西:

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

但是没有这样的东西ByteArray.fromString(String)

我建议使用 byte[] 作为进出 crypt 函数的基本数据类型:

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 回答
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 回答