前言最近公司需要对本公司的一些下载文件进行加密解密需求,也就尝试去实现下,其实需要借助第三方的jar包:bcprov-jdk15on-155.jar,下载这个可以到网上搜或者下载本人的demo即可,注意:需要加密和解密的key是一致的才可以
前言
最近公司需要对本公司的一些下载文件进行加密解密需求,也就尝试去实现下,其实需要借助第三方的jar包:bcprov-jdk15on-155.jar,下载这个可以到网上搜或者下载本人的demo即可,注意:需要加密和解密的key是一致的才可以解密,不然就会解密失败。不多说,直接上代码。
效果图
代码:
实现加密解密逻辑代码
package com.vsoontech.p2p.sample; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlGorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.ShortBufferException; public enum AES { INSTANCE; private Key key; public String generateKey() throws NoSuchAlgorithmException { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); this.key = keygen.generateKey(); return "Algorithm FORMat Encoded:" + key.getAlgorithm() + " - " + key.getFormat() + " - " + new String(key.getEncoded()); } public void encrypt(InputStream in) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException { this.crypt(in, null, Cipher.ENCRYPT_MODE); } public String decrypt(InputStream in) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException { return this.crypt(in, Cipher.DECRYPT_MODE); } public void encrypt(InputStream in, OutputStream out) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException { this.crypt(in, out, Cipher.ENCRYPT_MODE); } public void decrypt(InputStream in, OutputStream out) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException { this.crypt(in, out, Cipher.DECRYPT_MODE); } public void crypt(InputStream in, OutputStream out, int mode) throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, this.key); int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { //只要输入数据块具有全长度(长度可被8整除),调用update方法 int outLength = cipher.update(inBytes, 0, blockSize, outBytes); if (out != null) out.write(outBytes, 0, outLength); } else { more = false; } } if (inLength > 0) //不具有全长度,调用doFinal方法 outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); if (out != null) { out.write(outBytes); out.flush(); } } public String crypt(InputStream in, int mode) throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, this.key); int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; StringBuilder sb = new StringBuilder(); while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { //只要输入数据块具有全长度(长度可被8整除),调用update方法 int outLength = cipher.update(inBytes, 0, blockSize, outBytes); } else { more = false; } } if (inLength > 0) //不具有全长度,调用doFinal方法 outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); sb.append(new String(outBytes)); return sb.toString(); } public void seTKEy(Key key) { this.key = key; } public Key getKey() { return key; } }
--结束END--
本文标题: android使用AES加密和解密文件实例代码
本文链接: https://www.lsjlt.com/news/223262.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0