0
public class SymmetricCipherTest {
    private static final String DEFAULT_ENCRYPTION_ALGORITHM = "PBEWithMD5AndTripleDES";
    public final String ENCODE_INDICATOR_START = "ENC(";
    public final String ENCODE_INDICATOR_END = ")";
    public final String APP_ENCRYPTION_KEY_FILE = "application/.encryption.key";
    public static final int INTERATION = 15;
    private static final byte[] SALT = { (byte) 0xd7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
            (byte) 0x99 };

//  private static SymmetricCipherTest instance = initApplicaitonKey();
    private static Base64 base64 = new Base64();
    private static Cipher encrypter;
    private static Cipher decrypter;
//  private final Base64 base64 = new Base64();

    private static final int KEYLENGTH = 256;
    public final String ERROR_KEY_GENERATION = "Encryption key generation failed. Please verify the logs.";
    public static void main(String[] args) throws InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        String applicationKey="abcdefghijklmnopqrstu";
        String password="HellowWorld";
        try{
            SecretKeyFactory kf = SecretKeyFactory.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
            PBEKeySpec keySpec = new PBEKeySpec(applicationKey.toCharArray());
            SecretKey key = kf.generateSecret(keySpec);
            Cipher ciph = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);

            PBEParameterSpec params = new PBEParameterSpec(SALT, INTERATION);
            ciph.init(Cipher.ENCRYPT_MODE, key, params);
            encrypter=ciph;
            String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));
            System.out.println(encriptedString);

            Cipher ciph1 = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
            ciph1.init(Cipher.DECRYPT_MODE, key, params);
            decrypter=ciph;
            String decryiptedString=new String(base64.decode(decrypter.doFinal(encriptedString.getBytes())));
            System.out.println(decryiptedString);

        }catch(NoSuchAlgorithmException e){
            System.out.println("No such algorithm");
        }

    }
}

私が実行したい私のクラスです。しかし、私がこれを実行すると、次のような Out put 例外が発生します。

EEb9pXx45M1WV16D4b9EmA==
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -64
    at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
    at org.apache.commons.codec.binary.Base64.discardNonBase64(Base64.java:478)
    at org.apache.commons.codec.binary.Base64.decodeBase64(Base64.java:374)
    at org.apache.commons.codec.binary.Base64.decode(Base64.java:220)
    at com.vxl.appanalytix.webapp.listener.SymmetricCipherTest.main(SymmetricCipherTest.java:54)

SymmetricCipherTest の 54 行目は「String decryiptedString=new String(base64.decode(decrypter.doFinal(encriptedString.getBytes())));」です。このトピックに非常に慣れていないため、なぜこの例外が発生するのかわかりません。

アップデート:

public class SymmetricCipherTest {
    private static final String DEFAULT_ENCRYPTION_ALGORITHM = "PBEWithMD5AndTripleDES";
    public final String ENCODE_INDICATOR_START = "ENC(";
    public final String ENCODE_INDICATOR_END = ")";
    public final String APP_ENCRYPTION_KEY_FILE = "application/.encryption.key";
    public static final int INTERATION = 15;
    private static final byte[] SALT = { (byte) 0xd7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee,
            (byte) 0x99 };

//  private static SymmetricCipherTest instance = initApplicaitonKey();
    private static Base64 base64 = new Base64();
    private static Cipher encrypter;
    private static Cipher decrypter;
//  private final Base64 base64 = new Base64();

    public final String ERROR_KEY_GENERATION = "Encryption key generation failed. Please verify the logs.";
    public static void main(String[] args) throws InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, DecoderException, NoSuchAlgorithmException {
        String applicationKey="abcdefghijklmnopqrstu";
        String password="HellowWorld";
            encrypter=getCipherObject(applicationKey);
            String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));
            System.out.println(encriptedString);

            decrypter=getCipherObject(applicationKey);
             String decryiptedString=new String(decrypter.doFinal(base64.decode(encriptedString.getBytes())));
            System.out.println(decryiptedString);

    }

    public static Cipher getCipherObject(String applicationKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException{
        SecretKeyFactory kf = SecretKeyFactory.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);
        PBEKeySpec keySpec = new PBEKeySpec(applicationKey.toCharArray());
        SecretKey key = kf.generateSecret(keySpec);
        Cipher ciph = Cipher.getInstance(DEFAULT_ENCRYPTION_ALGORITHM);

        PBEParameterSpec params = new PBEParameterSpec(SALT, INTERATION);
        ciph.init(Cipher.ENCRYPT_MODE, key, params);
        return ciph;
    }
}
4

1 に答える 1

1

以下で暗号化した後、base64 でエンコードします。

String encriptedString=new String(base64.encode(encrypter.doFinal(password.getBytes())));

したがって、復号化する前にbase64デコードを行う必要があります..現在、base64でエンコードされた文字列を復号化しようとしています。次のようなものを試してください:

  String decryiptedString=new String(decrypter.doFinal(base64.decode(encriptedString.getBytes()));

また、ライン

decrypter=ciph;

する必要があります

decrypter=ciph1;

それ以外の場合は、暗号化 + 復号化ではなく、2 回暗号化されます。

于 2013-09-03T09:50:33.120 に答える