使用给定的加密算法(DES base64,secretKey,Cipher)进行Base64解密

时间:2018-08-21 09:39:28

标签: java encryption base64

当我通过以下代码对密码!QAZxdr5 进行加密时:

public static String encryptPassword(String msg) {
    try {
        KeySpec keySpec = new DESKeySpec(msg.getBytes());
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
        Cipher ecipher = Cipher.getInstance(key.getAlgorithm());
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        //Encode the string into bytes using utf-8
        byte[] utf8 = msg.getBytes("UTF8");
        //Encrypt
        byte[] enc = ecipher.doFinal(utf8);
        //Encode bytes to base64 to get a string
        return new String(Base64.getEncoder().encode(enc));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

我得到了输出: QQiu2a4NT9YfDAtmHjbk1A ==

现在,我尝试为此创建解密:

public static String decrypt(String msg) {
    try {
        KeySpec keySpec = new DESKeySpec(msg.getBytes());
        SecretKey key = 
        SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
        Cipher decipher = Cipher.getInstance(key.getAlgorithm());
        decipher.init(Cipher.DECRYPT_MODE, key);
        // Decode base64 to get bytes
        byte[] dec = Base64.getDecoder().decode(msg.getBytes("UTF-8"));
        //Decrypt
        byte[] utf8 = decipher.doFinal(dec);
        //Decode using utf-8
        return new String(utf8, "UTF8");
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}

但是,它不能正常工作,因为它返回null :(.。请您帮忙检查一下我错了吗?

1 个答案:

答案 0 :(得分:1)

通常,!QAZxdr5的Base64编码为IVFBWnhkcjU=,但是,您的代码使用密钥来编码额外的AFAI理解,这就是为什么获得QQiu2a4NT9YfDAtmHjbk1A==的原因。因此,decrypt()方法还需要知道已经生成的密钥,但是您不会知道。

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;


class EncryptDecryptTest {

    public static void main(String[] args) throws Exception {


        String key = "it's our key ~~~where is my +1 karma ??~~~ - soner";



        String ciphertext = encrypt(key, "!QAZxdr5");

        String decrypted = decrypt(key, ciphertext.trim());
        String encrypted = encrypt(key, decrypted.trim());

        if (ciphertext.contentEquals(encrypted.trim())) {
            System.out.println("decrypted!");
        } else {
            System.out.println("wrong key!");
        }
    }

    public static String encrypt(String key, String data)
            throws GeneralSecurityException {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(dataBytes));
    }

    public static String decrypt(String key, String data)
            throws GeneralSecurityException {
        byte[] dataBytes = Base64.getDecoder().decode(data);
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
        return new String(dataBytesDecrypted);
    }
}

作为旁注,

return new String(Base64.getEncoder().encode(enc));使用 array toString()方法的事实 应该为return Base64.getEncoder().encodeToString(enc);,以便获得预期的编码数据。