android– 使用ChaCha20加密和解密字符串

android– 使用ChaCha20加密和解密字符串,第1张

概述Iwanttodecryptandencryptastringusingchacha20BouncyCastleProvider正在使用chacha20技术.所以我把它包括在内.并尝试了代码,但无法工作.PBE.javapublicclassPBEextendsAppCompatActivity{privatestaticfinalStringsalt="Along,butconstantp

I want to decrypt and encrypt a string using chacha20

BouncyCastleProvIDer正在使用chacha20技术.所以我把它包括在内.并尝试了代码,但无法工作.

PBE.java

public class PBE extends AppCompatActivity {    private static final String salt = "A long, but constant phrase that will be used each time as the salt.";    private static final int iterations = 2000;    private static final int keyLength = 256;    private static final SecureRandom random = new SecureRandom();    @OverrIDe    protected voID onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.pbe);        try {            Security.insertProvIDerAt(new BouncyCastleProvIDer(), 1);            //Security.addProvIDer(new BouncyCastleProvIDer());            String passphrase = "The quick brown fox jumped over the lazy brown dog";            String plaintext = "Hello";            byte [] ciphertext = encrypt(passphrase, plaintext);            String recoveredplaintext = decrypt(passphrase, ciphertext);            TextVIEw decryptedTv = (TextVIEw) findVIEwByID(R.ID.tv_decrypt);            decryptedTv.setText(recoveredplaintext);            System.out.println(recoveredplaintext);        }catch (Exception e){            e.printstacktrace();        }    }    private static byte [] encrypt(String passphrase, String plaintext) throws Exception {        SecretKey key = generateKey(passphrase);        Cipher cipher = Cipher.getInstance("AES/CTR/NOpadding");//,new BouncyCastleProvIDer());        cipher.init(Cipher.ENCRYPT_MODE, key, generateIV(cipher), random);        return cipher.doFinal(plaintext.getBytes());    }    private static String decrypt(String passphrase, byte [] ciphertext) throws Exception {        SecretKey key = generateKey(passphrase);        Cipher cipher = Cipher.getInstance("AES/CTR/NOpadding");// , new BouncyCastleProvIDer());        cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher), random);        return new String(cipher.doFinal(ciphertext));    }    private static SecretKey generateKey(String passphrase) throws Exception {        PBEKeySpec keySpec = new PBEKeySpec(passphrase.tochararray(), salt.getBytes(), iterations, keyLength);        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");        return keyFactory.generateSecret(keySpec);    }    private static IvParameterSpec generateIV(Cipher cipher) throws Exception {        byte [] ivBytes = new byte[cipher.getBlockSize()];        random.nextBytes(ivBytes);        return new IvParameterSpec(ivBytes);    }}

但它没有给我正确的结果..

编辑和更新代码

public class ChaCha20Encryptor implements Encryptor {    private final byte randomIvBytes[] = {0, 1, 2, 3, 4, 5, 6, 7};    static {        Security.addProvIDer(new BouncyCastleProvIDer());    }    @OverrIDe    public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalIDKeyException,            InvalIDAlgorithmParameterException, InvalIDCipherTextException {        ChaChaEngine cipher = new ChaChaEngine();        CipherParameters cp = new KeyParameter(getMyKey(randomKeyBytes));        cipher.init(true, new ParametersWithIV(cp , randomIvBytes));        //cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));        byte[] result = new byte[data.length];        cipher.processBytes(data, 0, data.length, result, 0);        return result;    }    @OverrIDe    public byte[] decrypt(byte[] data, byte[] randomKeyBytes)            throws InvalIDKeyException, InvalIDAlgorithmParameterException, IOException,            IllegalStateException, InvalIDCipherTextException {        ChaChaEngine cipher = new ChaChaEngine();        CipherParameters cp = new KeyParameter(getMyKey(randomKeyBytes));        cipher.init(false, new ParametersWithIV(cp , randomIvBytes));        //cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));        byte[] result = new byte[data.length];        cipher.processBytes(data, 0, data.length, result, 0);        return result;    }    @OverrIDe    public int getKeyLength() {        return 32;    }    @OverrIDe    public String toString() {        return "ChaCha20()";    }    private static byte[] getMyKey(byte[] key){        try {            //byte[] key = encodekey.getBytes("UTF-8");            MessageDigest sha = MessageDigest.getInstance("SHA-1");            key = sha.digest(key);            key = Arrays.copyOf(key, 16); // use only first 128 bit        }        catch (NoSuchAlgorithmException e){            e.printstacktrace();        }        return key;    }}

Now I have only problem decrypting. It shows an error that key must be 128 or 256 bits. What am I doing wrong.

解决方法:

密码的输出由随机位组成(通常受到8位字节的实现限制).随机字节可能包含任何字符集中的无效字符.如果需要String,请将密文编码为base 64.

此外,您在解密时重新生成IV.加密/解密期间的IV应该匹配.

总结

以上是内存溢出为你收集整理的android – 使用ChaCha20加密和解密字符串全部内容,希望文章能够帮你解决android – 使用ChaCha20加密和解密字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1101304.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存