Java 使用海绵城堡的android查查加密

Java 使用海绵城堡的android查查加密,java,android,string,encryption,byte,Java,Android,String,Encryption,Byte,我只想用ChaCha20对Android中的文本进行加密和解密 我已经找到了一些例子,但找不到一个解决方案,以正确的方式实现它。我想要ChaCha加密。有人能指导我如何实现它吗。我试过多次修改这些代码 SecurityUtil public class SecurityUtil extends Properties { public String decrypt(String name, String keyString) throws Exception {

我只想用
ChaCha20
Android
中的文本进行加密和解密

我已经找到了一些例子,但找不到一个
解决方案
,以正确的方式实现它。我想要
ChaCha
加密
。有人能指导我如何实现它吗。我试过多次修改这些代码

SecurityUtil

public class SecurityUtil extends Properties {

    public String decrypt(String name, String keyString)
            throws Exception {
        BlowfishEngine engine = new BlowfishEngine();
        PaddedBufferedBlockCipher cipher =
                new PaddedBufferedBlockCipher(engine);
        StringBuffer result = new StringBuffer();
        KeyParameter key = new KeyParameter(keyString.getBytes());
        cipher.init(false, key);
        byte out[] = Base64.decode(name);
        byte out2[] = new byte[cipher.getOutputSize(out.length)];
        int len2 = cipher.processBytes(out, 0, out.length, out2, 0);
        cipher.doFinal(out2, len2);
        String s2 = new String(out2);
        for (int i = 0; i < s2.length(); i++) {
            char c = s2.charAt(i);
            if (c != 0) {
                result.append(c);
            }
        }

        return result.toString();
    }
    public String encrypt(String value, String keyString)
            throws Exception {
        BlowfishEngine engine = new BlowfishEngine();
        PaddedBufferedBlockCipher cipher =
                new PaddedBufferedBlockCipher(engine);
        KeyParameter key = new KeyParameter(keyString.getBytes());
        cipher.init(true, key);
        byte in[] = value.getBytes();
        byte out[] = new byte[cipher.getOutputSize(in.length)];
        int len1 = cipher.processBytes(in, 0, in.length, out, 0);
        try {
            cipher.doFinal(out, len1);
        } catch (CryptoException e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
        String s = new String(Base64.encode(out));
        return s;
    }
}
有没有一种简单的方法可以使用chacha对文本进行加密和解密

你也许可以适应
public class AESWrapper {

    public byte[] encrypt(byte[] plainData, byte[] key) throws AESEncryptionFailedException {

        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        ParametersWithRandom params = new ParametersWithRandom(new KeyParameter(key));
        paddedBufferedBlockCipher.init(true, params);
        byte[] cipherText = new byte[paddedBufferedBlockCipher.getOutputSize(plainData.length)];

        int val = paddedBufferedBlockCipher.processBytes(plainData, 0, plainData.length, cipherText, 0);

        try {
            paddedBufferedBlockCipher.doFinal(cipherText, val);
        } catch (DataLengthException e) {
            throw new AESEncryptionFailedException("AES Encryption failed due to data length mismatch");
        } catch (IllegalStateException e) {
            throw new AESEncryptionFailedException("AES Encryption failed");
        } catch (InvalidCipherTextException e) {
            throw new AESEncryptionFailedException("AES Encryption failed due to invalid cipher text");
        }

        return cipherText;

    }

    public byte[] decrypt(byte[] cipherText, byte[] key) throws AESDecryptionFailedException {

        PaddedBufferedBlockCipher paddedBufferedBlockCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
        ParametersWithRandom params = new ParametersWithRandom(new KeyParameter(key));
        paddedBufferedBlockCipher.init(false, params);
        byte[] plainText = new byte[paddedBufferedBlockCipher.getOutputSize(cipherText.length)];

        int val = paddedBufferedBlockCipher.processBytes(cipherText, 0, cipherText.length, plainText, 0);
        try {
            int len = paddedBufferedBlockCipher.doFinal(plainText, val);
            byte[] decryptedData = new byte[val + len];
            System.arraycopy(plainText, 0, decryptedData, 0, len + val);
            return decryptedData;

        } catch (DataLengthException e) {
            throw new AESDecryptionFailedException("AES Decryption failed due to data length mismatch");
        } catch (IllegalStateException e) {
            throw new AESDecryptionFailedException("AES Decryption failed");
        } catch (InvalidCipherTextException e) {
            throw new AESDecryptionFailedException("AES Decryption failed due to invalid cipher text");
        }
    }
}