0

AES、JCE、および 256 ビット キー用の無制限強度ポリシー ファイルと、このOracle/Sun ガイドについてよく読んだ後、これを実装しました。

アップデート

sun から提供されたコードを組み合わせて、Java クラス AESencrp.java を作成しました。

/**
 *
 * @author MUDASSIR
 */

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class AESencrp {

     private static final String ALGO = "AES";
//    private static final byte[] keyValue = 
//        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
//'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static String encrypt(String Data, byte[] keyValue) throws Exception {
        Key key = generateKey(keyValue);
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
        Key key = generateKey(keyValue);
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decodedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey(byte[] keyValue) throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
}

}

これは問題なく動作しますが、プロジェクトをビルドするたびに、
「Base64encoder はプロプライエタリ ソフトウェアであり、将来のリリースで削除する必要があります」という警告が表示されます。

base64 エンコーダーを削除し、代わりに asHex メソッドを使用すると (ここの太陽ガイドで提供されているように)、BadPadding 例外が発生しました。
スレッド「メイン」での例外 javax.crypto.BadPaddingException: 指定された最終ブロックが適切に埋め込まれていません

これはbase64エンコーダーなしの私のコードです

/**
 *
 * @author MUDASSIR
 */

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class AESencrp2 {

     private static final String ALGO = "AES";
//    private static final byte[] keyValue = 
//        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
//'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

     public static String asHex(byte buf[]) {
        StringBuilder strbuf = new StringBuilder(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 static String encrypt(String Data, byte[] keyValue) throws Exception {
        Key key = generateKey(keyValue);
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedValue = c.doFinal(Data.getBytes());
        return asHex(encryptedValue);
    }

    public static String decrypt(String encryptedData, byte[] keyValue) throws Exception {
        Key key = generateKey(keyValue);
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decValue = c.doFinal(encryptedData.getBytes());
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey(byte[] keyValue) throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
}

}

これは私がコードを試すメインです

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

        byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

        String password = "This is the data that is going to be encrypted";

        String passwordEnc = AESencrp.encrypt(password, keyValue);
        //String passwordEnc = AESencrp2.encrypt(password, keyValue);

        String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);
        //String passwordDec = AESencrp.decrypt(passwordEnc, keyValue);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
} 

問題、

  1. 1 - base64 エンコーダーを使用する場合、その独自仕様です。
  2. 2 - Sun が提供する asHex メソッドを使用すると、不適切なパディング例外が発生します。

助けてください。

4

1 に答える 1

2

この問題を解決するには、独自の Base64 エンコード/デコード ルーチンを使用できます。

于 2012-07-04T11:25:25.847 に答える