简体   繁体   English

适用于Commons Codec中的encodeBuffer方法的替换

[英]Suitable replacement for decodeBuffer Method in Commons Codec

I have a piece of code which encrypts and decrypts using AES algorithm, which was using sun.misc.* packages. 我有一段代码使用AES算法进行加密和解密,该算法使用sun.misc。*软件包。

Later i came to know it is wrong to use those set of packages which made me follow the advice of using Apache's Commons Codec which is effective. 后来我才知道使用这些软件包集是错误的,这使我听从使用有效的Apache Commons Codec的建议。

The previous code was as below : 先前的代码如下:

import java.security.*;
import java.security.spec.InvalidKeySpecException;
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) throws Exception {
    Key key = generateKey();
    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) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
}



}

As suggested I have removed sun.misc and did the following alteration. 按照建议,我删除了sun.misc并进行了以下更改。

After replacing BASE64Encoder class with Base64 in Apache's commons codec: 在Apache的通用编解码器中用Base64替换BASE64Encoder类之后:

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

I am not able to find a suitable replacement for decryption as i am struck at line: 我无法找到合适的解密替代品,因为我在网上被撞到了:

byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);

I am not finding any method which does the job of decodeBuffer(String encryptedData) and returns me a byte array of decodedValue. 我没有找到任何能做的解码缓冲区(字符串encryptedData)的工作,并向我返回一个字节数组的decodedValue的方法。

OK, Then what about this? 好,那呢?

Using apache Base64 util class is similar to that of sun.misc.Base64Encoder. 使用Apache Base64 util类类似于sun.misc.Base64Encoder。 Apache Base64 class provides many overload methods that will relieve you of conversion between bytes and String. Apache Base64类提供了许多重载方法,这些方法可以减轻字节和字符串之间的转换负担。

byte[] encodedBytes  = Base64.encodeBase64(testString.getBytes());

String decodedString = new String(Base64.decodeBase64(encodedBytes));
public class EncryptionDecrption {

    private static final String ALGO = "AES";
    private static final byte[] keyValue = new byte[]{'T', 'h', 'e', 'R', 'o', 'o', 'K', 'n', 'a', 't','E','n', 'i', 'r','i','n'};

    public EncryptionDecrption(){

    }

    public static String setEncryptedString(String data) throws Exception {
        Key key = getKey();
        Cipher cipher = Cipher.getInstance(ALGO);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedValue = cipher.doFinal(data.getBytes("UTF-8"));

        return Base64.encodeToString(encryptedValue, Base64.DEFAULT);
    }

    public static String getDecryptedValue(String data) throws Exception {

        if(data != null) {
            Key key = getKey();
            Cipher cipher = Cipher.getInstance(ALGO);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decodebyte = Base64.decode(data.getBytes("UTF-8"), Base64.DEFAULT);
            byte[] decValue = cipher.doFinal(decodebyte);

            return new String(decValue);
        }

        return null;
    }

    private static Key getKey() throws Exception {
        return new SecretKeySpec(keyValue, ALGO);
    }
}

it works for me in Android. 它适用于Android。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM