「java文件加密解密文件」java实现文件加密解密

博主:adminadmin 2022-12-01 08:22:09 54

本篇文章给大家谈谈java文件加密解密文件,以及java实现文件加密解密对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用java实现文件(不只是txt文本)的整体加密解密?

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

/**

 * 文件加密解密

 * 加解密需要依靠以下四个属性,

static KeyPairGenerator keyPairGen;

static KeyPair keyPair;

static RSAPrivateKey privateKey;

static RSAPublicKey publicKey;

 * @author young

 *

 */

public class RSAEncrypt {

static KeyPairGenerator keyPairGen;

static KeyPair keyPair;

static RSAPrivateKey privateKey;

static RSAPublicKey publicKey;

static {

try {

// 实例类型

keyPairGen = KeyPairGenerator.getInstance("RSA");

// 初始化长度

keyPairGen.initialize(512);

// 声场KeyPair

keyPair = keyPairGen.generateKeyPair();

// Generate keys

privateKey = (RSAPrivateKey) keyPair.getPrivate();

publicKey = (RSAPublicKey) keyPair.getPublic();

} catch (NoSuchAlgorithmException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void main(String[] args) {

RSAEncrypt encrypt = new RSAEncrypt();

File file = new File(

"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf.txt");

File newFile = new File(

"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");

encrypt.encryptFile(encrypt, file, newFile);

File file1 = new File(

"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");

File newFile1 = new File(

"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf2.txt");

encrypt.decryptFile(encrypt, file1, newFile1);

}

/**

 * 加密文件

 * @param encrypt RSAEncrypt对象

 * @param file 源文件

 * @param newFile 目标文件

 */

public void encryptFile(RSAEncrypt encrypt, File file, File newFile) {

try {

InputStream is = new FileInputStream(file);

OutputStream os = new FileOutputStream(newFile);

byte[] bytes = new byte[53];

while (is.read(bytes)  0) {

byte[] e = encrypt.encrypt(RSAEncrypt.publicKey, bytes);

bytes = new byte[53];

os.write(e, 0, e.length);

}

os.close();

is.close();

System.out.println("write success");

} catch (Exception e) {

e.printStackTrace();

}

}

/**

 * 解密文件

 * @param encrypt RSAEncrypt对象

 * @param file

 * @param newFile

 */

public void decryptFile(RSAEncrypt encrypt, File file, File newFile) {

try {

InputStream is = new FileInputStream(file);

OutputStream os = new FileOutputStream(newFile);

byte[] bytes1 = new byte[64];

while (is.read(bytes1)  0) {

byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1);

bytes1 = new byte[64];

os.write(de, 0, de.length);

}

os.close();

is.close();

System.out.println("write success");

} catch (Exception e) {

e.printStackTrace();

}

}

/**

 * 加密实现

 * * Encrypt String. *

 * 

 * @return byte[] 加密后的字节数组

 */

protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {

if (publicKey != null) {

try {

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

return cipher.doFinal(obj);

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

/**

 * 解密实现

 * * Basic decrypt method *

 * 

 * @return byte[] 解密后的字节数组

 */

protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {

if (privateKey != null) {

try {

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.DECRYPT_MODE, privateKey);

return cipher.doFinal(obj);

} catch (Exception e) {

e.printStackTrace();

}

}

return null;

}

}

java简单的文件加密解密

这个应该是作业吧、我还是建议你自己做、而不是在这问了、我们帮你做完、你可以做、等到不会来、在拿出来分享、我们可以共同学习!

java对文件加密与解密。

给文件加密很简单,下载一个文件加密软件就可以了

文件夹加密超级大师是文件加密软件中非常不错的选择。

文件夹加密超级大师是专业的文件加密软件,文件加密后如果需要使用,只需要输入正确密码,

点击打开,使用完毕后,文件就自动回复到加密状态了。

非常好用,强烈推荐。

java RSA实现对文件加密解密

这个我不清楚。

对文件加密,我使用的是超级加密3000.

超级加密3000采用国际上成熟的加密算法和安全快速的加密方法,可以有效保障数据安全!

具体操作方法:

1下载安装超级加密3000。

2 然后在需要加密的文件上单击鼠标右键选择加密。

3 在弹出的文件加密窗口中设置文件加密密码就OK了。

超级加密3000的下载地址你可以在百度上搜索超级加密3000,第一个就是。

请问用java如何对文件进行加密解密?

package com.palic.pss.afcs.worldthrough.common.util;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import repack.com.thoughtworks.xstream.core.util.Base64Encoder;

/**

 * AES加密解密

 * @author EX-CHENQI004

 *

 */

public class AesUtils {

public static final String cKey= "assistant7654321";

  /** 

     * 加密--把加密后的byte数组先进行二进制转16进制在进行base64编码 

     * @param sSrc 

     * @param sKey 

     * @return 

     * @throws Exception 

     */  

    public static String encrypt(String sSrc, String sKey) throws Exception {  

        if (sKey == null) {  

            throw new IllegalArgumentException("Argument sKey is null.");  

        }  

        if (sKey.length() != 16) {  

            throw new IllegalArgumentException(  

                    "Argument sKey'length is not 16.");  

        }  

        byte[] raw = sKey.getBytes("ASCII");  

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  

  

        Cipher cipher = Cipher.getInstance("AES");  

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);  

  

        byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));  

        String tempStr = parseByte2HexStr(encrypted);  

  

        Base64Encoder encoder = new Base64Encoder();  

        return encoder.encode(tempStr.getBytes("UTF-8"));  

    }  

  

    /** 

     *解密--先 进行base64解码,在进行16进制转为2进制然后再解码 

     * @param sSrc 

     * @param sKey 

     * @return 

     * @throws Exception 

     */  

    public static String decrypt(String sSrc, String sKey) throws Exception {  

  

        if (sKey == null) {  

            throw new IllegalArgumentException("499");  

        }  

        if (sKey.length() != 16) {  

            throw new IllegalArgumentException("498");  

        }  

  

        byte[] raw = sKey.getBytes("ASCII");  

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  

  

        Cipher cipher = Cipher.getInstance("AES");  

        cipher.init(Cipher.DECRYPT_MODE, skeySpec);  

  

        Base64Encoder encoder = new Base64Encoder();  

        byte[] encrypted1 = encoder.decode(sSrc);  

  

        String tempStr = new String(encrypted1, "utf-8");  

        encrypted1 = parseHexStr2Byte(tempStr);  

        byte[] original = cipher.doFinal(encrypted1);  

        String originalString = new String(original, "utf-8");  

        return originalString;  

    }  

  

    /** 

     * 将二进制转换成16进制 

     *  

     * @param buf 

     * @return 

     */  

    public static String parseByte2HexStr(byte buf[]) {  

        StringBuffer sb = new StringBuffer();  

        for (int i = 0; i  buf.length; i++) {  

            String hex = Integer.toHexString(buf[i]  0xFF);  

            if (hex.length() == 1) {  

                hex = '0' + hex;  

            }  

            sb.append(hex.toUpperCase());  

        }  

        return sb.toString();  

    }  

  

    /** 

     * 将16进制转换为二进制 

     *  

     * @param hexStr 

     * @return 

     */  

    public static byte[] parseHexStr2Byte(String hexStr) {  

        if (hexStr.length()  1)  

            return null;  

        byte[] result = new byte[hexStr.length() / 2];  

        for (int i = 0; i  hexStr.length() / 2; i++) {  

            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);  

            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),  

                    16);  

            result[i] = (byte) (high * 16 + low);  

        }  

        return result;  

    } 

    public static void main(String[] args) throws Exception {

/*

 * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定

 */

String cKey = "assistant7654321";

// 需要加密的字串

String cSrc = "123456";

// 加密

long lStart = System.currentTimeMillis();

String enString = encrypt(cSrc, cKey);

System.out.println("加密后的字串是:" + enString);

long lUseTime = System.currentTimeMillis() - lStart;

System.out.println("加密耗时:" + lUseTime + "毫秒");

// 解密

lStart = System.currentTimeMillis();

String DeString = decrypt(enString, cKey);

System.out.println("解密后的字串是:" + DeString);

lUseTime = System.currentTimeMillis() - lStart;

System.out.println("解密耗时:" + lUseTime + "毫秒");

}

}

java中如何实现对文件和字符串加密. 解密?

你好,加密的方式有很多中,如传统加密,后期的分组加密,序列流加密,这些是对称加密,现在有著名的非对称加密。

java的扩展包很好的实现了你需要的功能。这个包在java.security.*;当然了还有很多好的加密方法,在sun的第三方jar包中有。目前密码加密使用用的是MD5加密,这个是单向加密,不可以解密。要想实现加密和解密,那么就需要学习密码学的知识。

希望对你有所帮助。

关于java文件加密解密文件和java实现文件加密解密的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。