1

我将 Triple DES 用于我的加密/解密目的,但不知何故它给了我上述异常,我尝试了相关答案中提到的其他方法,但我被卡住了。我是密码学和相应的 java 库的新手。

private static byte[] Key = new byte[] {
        0x42, 0x45, 0x49, 0x30, 0x12, 0x22, 0x35, 0x48, 0x33, 0x24, 0x28, 0x51,
        0x48, 0x24, 0x30, 0x21, 0x44, 0x31, 0x14, 0x19, 0x45, 0x34, 0x47, 0x25 };

Cipher c;

public EncryptionHelper() throws Exception {
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 32);
    SecretKey key = new SecretKeySpec(Key, 0, Key.length, "DESede");
    c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
}

public String Encrypt(String S) throws Exception {
    byte[] base64EncryptedText = S.getBytes("UTF-8");
    byte EncryptedText[] = c.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(EncryptedText);
}

public String Decrypt(String S) throws Exception {
    Cipher c2 = null;
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 24);
    SecretKey key = new SecretKeySpec(Key,0, Key.length, "DESede");
    c2 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c2.init(Cipher.DECRYPT_MODE, key);
    byte[] base64EncryptedText = Base64.getEncoder().encode(S.getBytes());
    byte[] textDecrypted = c2.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(textDecrypted, "UTF-8");
}

编辑:

最后通过解决方案,我只是放错了组件,也指定了核心逻辑。

public class EncryptionHelper {

private static byte[] Key = new byte[] {
    0x42, 0x45, 0x49, 0x30, 0x12, 0x22, 0x35, 0x48, 0x33, 0x24, 0x28, 0x51,
    0x48, 0x24, 0x30, 0x21, 0x44, 0x31, 0x14, 0x19, 0x45, 0x34, 0x47, 0x25 };

static Cipher c;

public EncryptionHelper() throws Exception {
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 32);
    SecretKey key = new SecretKeySpec(Key, 0, Key.length, "DESede");
    c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, key);
}

public static String Encrypt(String S) throws Exception {
    byte[] base64EncryptedText = S.getBytes("UTF-8");
    byte EncryptedText[] = c.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(Base64.getEncoder().encode(EncryptedText));
}

// LOGIC:
// for encryption: string -> utf-8 byte array,
        // encrypt and return a base 64 encoded string
// for decryption: String -> base64 -> decode base 64 array,
        // decrypt and return utf-8 string

public static String Decrypt(String S) throws Exception {
    Cipher c2 = null;
    // byte[] key_hash = (Key).toString().getBytes("UTF-8");
    // key_hash = Arrays.copyOf(key_hash, 24);
    SecretKey key = new SecretKeySpec(Key, "DESede");
    c2 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    c2.init(Cipher.DECRYPT_MODE, key);
    byte[] base64EncryptedText = Base64.getDecoder().decode(S.getBytes());
    byte[] textDecrypted = c2.doFinal(base64EncryptedText, 0, base64EncryptedText.length);
    return new String(textDecrypted, "UTF-8");
}
4

2 回答 2

1

Despite the names on your variables you have failed to base64-encode the result of encryption in your Encrypt method. Therefore, when you convert it to String you get garbage, and when you base64 decode that garbage in your Decrypt method you get garbage2.

于 2017-06-12T11:49:19.623 回答
0

你是base64解码然后解密,但你不是加密然后base64编码。

于 2017-06-12T12:33:01.800 回答