0

您好,我构建了这 2 种方法,加密工作正常,但解密出错,因为密码需要一个字节,我想从字符串加密

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

public class Test {

    private byte[] encrypted;

    private String encryptedtext;
    private String decrypted;



    public String Encrypt (String pInput) {


      try {

         String Input = pInput;
         String key = "Bar12345Bar12345Bar12345Bar12345"; 

         // Erstelle key and cipher
         SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance("AES");

         // Verschlüsselung
         cipher.init(Cipher.ENCRYPT_MODE, aesKey);
         byte[] encrypted = cipher.doFinal(Input.getBytes());
         encryptedtext = new String(encrypted);
         System.err.println("encrypted:" + encryptedtext);


      }catch(Exception e) {
         e.printStackTrace();
      }

        return encrypted;
    }



    public String Decrypt (String pInput) {


       try {

           String Input = pInput; 

           String key = "Bar12345Bar12345Bar12345Bar12345"; 

           // Erstelle key and cipher
           SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
           Cipher cipher = Cipher.getInstance("AES");

           // Entschlüsselung
           cipher.init(Cipher.DECRYPT_MODE, aesKey);
           decrypted = new String(cipher.doFinal(encryptedtext)); // HERE IS THE PROBLEM IT WANT BYTE BUT I WANT TO ENCRYPT FROM A STRING
           System.err.println("decrypted: " + decrypted);

        }catch(Exception e) {
           e.printStackTrace();
        }
        return pInput;
      }

}
4

2 回答 2

3

字节数组不能直接转成字符串,反方向也不能。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class stackoverflow_test {
    private byte[] encrypted;

    private String encryptedtext;
    private String decrypted;

    public String Encrypt(String pInput) {

        try {

            String Input = pInput;
            String key = "Bar12345Bar12345Bar12345Bar12345";

            SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encrypted = cipher.doFinal(Input.getBytes());
            //encryptedtext = new String(encrypted);
            encryptedtext = DatatypeConverter.printBase64Binary(encrypted);
            System.err.println("encrypted:" + encryptedtext);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return encryptedtext;
    }

    public String Decrypt(String pInput) {

        try {

            String Input = pInput;

            String key = "Bar12345Bar12345Bar12345Bar12345";

            SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(Cipher.DECRYPT_MODE, aesKey);
            encrypted = DatatypeConverter.parseBase64Binary(encryptedtext);
            decrypted = new String(cipher.doFinal(encrypted)); 
            System.err.println("decrypted: " + decrypted);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pInput;
    }

    public static void main(String[] ag){
        stackoverflow_test test = new stackoverflow_test();
        String a = test.Encrypt("Byte cannot directly convert to string");
        String b = test.Decrypt(a);
    }
}

结果

encrypted:UmH+3eUagjrRDblxSStArnaktoxTLX+7qvPdwiTO7VggYmYtuXu/Ygww8ZG5SrDz
decrypted: Byte cannot directly convert to string
于 2014-07-26T07:22:19.840 回答
1

您可以使用Cipher加密和解密一个String.

public class CryptUtil {
    private static final String ALGORITHM = "Blowfish";
    private static final String MODE = "Blowfish/CBC/PKCS5Padding";
    private static final String IV = "abcdefgh";

    public static  String encrypt(String secretKey, String value ) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(MODE);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes()));
        byte[] values = cipher.doFinal(value.getBytes());
        return Base64.encodeToString(values, Base64.DEFAULT);
    }

    public static  String decrypt(String secretKey, String value) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        byte[] values = Base64.decode(value, Base64.DEFAULT);
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(MODE);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(IV.getBytes()));
        return new String(cipher.doFinal(values));
    }
}
于 2019-01-23T00:56:11.010 回答