1

我想知道如何加密最终将上传到保管箱的文件或照片。

由于我在网上进行了研究,并且仅设法找到仅加密密码的此代码(粘贴在底部),但我想知道如何加密最终将上传到保管箱中的文件或照片。

那么是否有关于如何编写使用三重 DES 加密文件的 java 编程(将在 Eclipse 软件上使用)的任何参考或帮助或指南?非常感谢。

package com.kushal.utils;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESEncryption {

private static final String UNICODE_FORMAT = "UTF8";
public static final String DES_ENCRYPTION_SCHEME = "DES";
private KeySpec myKeySpec;
private SecretKeyFactory mySecretKeyFactory;
private Cipher cipher;
byte[] keyAsBytes;
private String myEncryptionKey;
private String myEncryptionScheme;
SecretKey key;

public DESEncryption() throws Exception
{
    myEncryptionKey = "ThisIsSecretEncryptionKey";
    myEncryptionScheme = DES_ENCRYPTION_SCHEME;
    keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    myKeySpec = new DESKeySpec(keyAsBytes);
    mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = mySecretKeyFactory.generateSecret(myKeySpec);
}

/**
 * Method To Encrypt The String
 */
public String encrypt(String unencryptedString) {
    String encryptedString = null;
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
        byte[] encryptedText = cipher.doFinal(plainText);
        BASE64Encoder base64encoder = new BASE64Encoder();
        encryptedString = base64encoder.encode(encryptedText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedString;
}
/**
 * Method To Decrypt An Ecrypted String
 */
public String decrypt(String encryptedString) {
    String decryptedText=null;
    try {
        cipher.init(Cipher.DECRYPT_MODE, key);
        BASE64Decoder base64decoder = new BASE64Decoder();
        byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
        byte[] plainText = cipher.doFinal(encryptedText);
        decryptedText= bytes2String(plainText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decryptedText;
}
/**
 * Returns String From An Array Of Bytes
 */
private static String bytes2String(byte[] bytes) {
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        stringBuffer.append((char) bytes[i]);
    }
    return stringBuffer.toString();
}

/**
 * Testing the DES Encryption And Decryption Technique
 */
public static void main(String args []) throws Exception
{
    DESEncryption myEncryptor= new DESEncryption();

    String stringToEncrypt="Sanjaal.com";
    String encrypted=myEncryptor.encrypt(stringToEncrypt);
    String decrypted=myEncryptor.decrypt(encrypted);

    System.out.println("String To Encrypt: "+stringToEncrypt);
    System.out.println("Encrypted Value :" + encrypted);
    System.out.println("Decrypted Value :"+decrypted);

}   

}
4

2 回答 2

0

一个文件可能被读取为一串二进制数字(很多 1 和 0)。您可以读取文件,然后使用与密码相同的方式使用 DES 加密字符串,将结果输出到新文件。

解密将以相同的方式工作,读取加密文件,解密并输出到未加密的文件。

于 2012-08-10T08:43:05.353 回答
0

文件读取取自这里

public static byte[] encryptFile(String pFilePath, byte[] pKey) throws GeneralSecurityException, IOException
{
    File file = new File(pFilePath);
    long length = file.length();
    InputStream is = new FileInputStream(file);

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Close the input stream and return bytes
    is.close();

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {

        throw new IOException("Could not completely read file "+file.getName());
    }

    SecretKeyFactory lDESedeKeyFactory = SecretKeyFactory.getInstance("DESede");
    SecretKey kA = lDESedeKeyFactory.generateSecret(new DESedeKeySpec(pKey));

    IvParameterSpec lIVSpec = new IvParameterSpec(new byte[8]);
    Cipher desedeCBCCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

    desedeCBCCipher.init(Cipher.ENCRYPT_MODE, kA, lIVSpec);
    byte[] encrypted = desedeCBCCipher.doFinal(bytes);

    return encrypted;
}
于 2012-08-10T09:47:43.090 回答