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 に答える