2010-11-26 14 views
0

私はJavaアプレットでAES 256をテストしています。私はコード内にバイト[]をデコードすればうまく動作します。私はバイナリ形式のバイトをエンコードされた文字列のテキストボックスに表示します。その文字列を取り出してデコードすると、例外Given final block not properly paddedが発生します。どうしたの?AES256 in javaエラー

String originalString = new String(original); 

あなたが不透明なバイナリデータを取得し、それが有効なテキスト文字列であるかのようにそれを解釈しようとしている:

私のコードは、これが問題である


public class TestApplet extends Applet { 
Label lblKey = new Label("Key"); 
TextField inputLineKey = new TextField(15); 
Label lblString = new Label("Value"); 
TextField inputLineString = new TextField(15); 
Label lblStringEncoded = new Label("Encoded Value"); 
TextField inputLineStringEncoded = new TextField(15); 
Label lblStringDecoded = new Label("Decoded Value"); 
TextField inputLineStringDecoded = new TextField(15); 
Button encodeButton = new Button("Test Encrypt"); 
Button decodeButton = new Button("Test Decrypt"); 

public TestApplet() { 
    add(inputLineKey); 
    add(lblKey); 
    add(inputLineString); 
    add(lblString); 
    add(inputLineStringEncoded); 
    add(lblStringEncoded); 
    add(inputLineStringDecoded); 
    add(lblStringDecoded); 
    add(encodeButton); 
    add(decodeButton); 
    // inputLine.addActionListener(new MyActionListener()); 
} 

/** 
    * Turns array of bytes into string 
    * 
    * @param buf 
    *   Array of bytes to convert to hex string 
    * @return Generated hex string 
    */ 
public static String asHex(byte buf[]) { 
    StringBuffer strbuf = new StringBuffer(buf.length * 2); 
    int i; 

    for (i = 0; i < buf.length; i++) { 
    if (((int) buf[i] & 0xff) < 0x10) 
    strbuf.append("0"); 

    strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); 
    } 

    return strbuf.toString(); 
} 

public boolean action(Event e, Object args) { 

    // so do something! 

    // /////////////////////// 
    try { 
    String message = "This is just an example"; 

    // Get the KeyGenerator 

    KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
    kgen.init(128); // 192 and 256 bits may not be available 

    // Generate the secret key specs. 
    SecretKey skey = kgen.generateKey(); 
    byte[] raw = skey.getEncoded(); 

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 

    // Instantiate the cipher 

    Cipher cipher = Cipher.getInstance("AES"); 

    if (e.target == encodeButton) { // User has clicked on encrypt 
      // button 
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 

    byte[] encrypted = cipher.doFinal((inputLineString.getText() 
     .length() == 0 ? message : inputLineString.getText()) 
     .getBytes()); 
    // System.out.println("encrypted string: " + asHex(encrypted)); 

    cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
    byte[] original = cipher.doFinal(encrypted); 
    String originalString = new String(original); 
    // System.out.println("Original string: " + 
    // originalString + " " + asHex(original)); 

    // Create a BigInteger using the byte array 
    BigInteger bi = new BigInteger(encrypted); 

    inputLineStringEncoded.setText(bi.toString(2)); // (new String(encrypted)); 
    inputLineStringDecoded.setText(originalString); 
    } 

    if (e.target == decodeButton) { // User has clicked on decrypt 
      // button 
    // cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 
    // 
    // byte[] encrypted = cipher.doFinal((inputLineString.getText() 
    // .length() == 0 ? message : inputLineString.getText()) 
    // .getBytes()); 
    // // System.out.println("encrypted string: " + asHex(encrypted)); 

    cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
    // Parse binary string 
    BigInteger bi = new BigInteger(inputLineStringEncoded 
     .getText(), 2); 

    byte[] original = cipher.doFinal(bi.toByteArray()); 
    String originalString = new String(original); 
    // System.out.println("Original string: " + 
    // originalString + " " + asHex(original)); 
    inputLineString.setText(originalString); 
    inputLineStringDecoded.setText(originalString); 
    } 

    } catch (Exception exc) { 
    inputLineStringEncoded.setText(exc.getMessage()); 
    } 
    return true; // Yes, we do need this! 
} 

class MyActionListener implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 
    } 
} 
} 
________________________________ 
+0

あなたがあなたの復号化ルーチンに直接暗号化した後に取得されたバイト配列を渡してみましたか?たぶんあなたは、BigDecimalとバイナリの文字列表現を介して行く途中で何かを失っている... –

答えて

3

に従います。それはほとんど間違いないでしょう。さらに、システムのデフォルトのエンコーディングを使用して変換していますが、これはほぼではありません。は良いアイデアです。

テキスト内の任意のバイナリデータを表現するには、base64を使用してエンコードおよびデコードすることをお勧めします。代わりに、16進数を使用してください。すでにバイナリデータを16進数に変換する方法があるので、それを使用したいかもしれません。 base64よりも若干長くなっていますが、扱いやすいかもしれません。

1

byte[] encryptedは、16進数のエンコードやbase64などの適切なTEXT形式でエンコードする必要があります。解読するには、テキスト表現から再びbyte[]に渡します。

問題は、これらの線の周りにある:

byte[] original = cipher.doFinal(bi.toByteArray()); 
String originalString = new String(original); 

あなたは、例えば、上記のエンコーディングのいずれかを使用して、それを文字列にエンコードする必要があり
BigInteger bi = new BigInteger(encrypted); 

。コモンズコーデックを見てください。

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html