如何在java中用Cipher类使用DES加密解密



在SO上还有其他类似的问题,但没有一个解决我的问题。所以,我问一个新的问题。

我有文本加密(字符串)和8字节密钥DES加密它(和16字节密钥EDE三重DES加密它)作为字节数组。

我如何使用Java的内置Cipher类加密,然后解密我的文本?

即使有人建议任何其他第三方代码,jar等,只要它支持这个特定的用例,也被接受。

我使用的一些代码:

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.xml.bind.DatatypeConverter;
        public class CryptoPropio {

            /**
             * Master password.
             */
            private static char[] PASSWORD;
            /** The Constant SALT. */
            private static final byte[] SALT = { (byte) 0xde, (byte) 0x33, (byte) 0x10,
                    (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, };
            /** The error des. */
            private static String errorDes = "Error al des/encriptar";
            /** The Constant ALGORITMO. */
            private static final String ALGORITMO = "PBEWithMD5AndDES";
            /**
             * Instantiates a new crypto propio.
             * 
             * @param pass
             *            the pass
             */
            public CryptoPropio(String pass) {
                super();
                PASSWORD = pass.toCharArray();
            }

            public String encrypt(String property) {
                SecretKeyFactory keyFactory;
                String result = null;
                try {
                    keyFactory = SecretKeyFactory.getInstance(ALGORITMO);
                    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
                    Cipher pbeCipher = Cipher.getInstance(ALGORITMO);
                    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT,
                            20));
                    result = base64Encode(
                                    pbeCipher.doFinal(property.getBytes("UTF-8")))
                                    .toString();
                } catch (InvalidKeyException | InvalidAlgorithmParameterException
                        | NoSuchAlgorithmException | InvalidKeySpecException
                        | NoSuchPaddingException | IllegalBlockSizeException
                        | BadPaddingException | UnsupportedEncodingException e) {
                  e.printStackTrace();
                }
                return result;
            }
            /**
             * Base64 encode.
             * 
             * @param bytes
             *            the bytes
             * @return the string
             */
            private String base64Encode(byte[] bytes) {
                return DatatypeConverter.printBase64Binary(bytes);
            }

            public String decrypt(String propert) {
                String property = propert;
                String result = null;
                try {
                    SecretKeyFactory keyFactory = SecretKeyFactory
                            .getInstance(ALGORITMO);
                    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
                    Cipher pbeCipher = Cipher.getInstance(ALGORITMO);
                    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT,
                            20));
                    result = new String(pbeCipher.doFinal(base64Decode(property)),
                            "UTF-8");
                } catch (InvalidKeyException | InvalidAlgorithmParameterException
                        | NoSuchAlgorithmException | InvalidKeySpecException
                        | NoSuchPaddingException | IllegalBlockSizeException
                        | BadPaddingException | UnsupportedEncodingException e) {
                   e.printStackTrace();
                }
                return result;
            }
            /**
             * Base64 decode.
             * 
             * @param property
             *            the property
             * @return the byte[]
             */
            private byte[] base64Decode(String property) {
                return DatatypeConverter.parseBase64Binary(property);
            }
        }

最新更新