java - 如何使用三重 DES 加密照片/文件? (将上传至投递箱)

标签 java android encryption

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

由于我在网上进行了研究,只找到了这个代码(粘贴在底部),它只加密密码,但我想知道如何加密最终上传到保管箱的文件或照片。

那么有没有关于如何编写使用三重 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);

}   

}

最佳答案

文件读取取自here

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;
}

关于java - 如何使用三重 DES 加密照片/文件? (将上传至投递箱),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11898139/

相关文章:

java - 如何保护 TextEncryptor 在 spring boot 中使用的密码

c# - 使用未知 IV 解密 PKCS#5 填充 AES/ECB

java - 如何在不进行字母频率分析的情况下破解凯撒密码

java - 函数仅适用于硬编码值

java - 如何使用 Graphics 将 Java 数组打印成整齐的列?

android - 将事件监听器添加到 listView

android更改微调器中项目的文本颜色

java - 使用 Maven : Getting java. lang.ClassNotFoundException : org. springframework.web.bind.support.WebDataBinderFactory

java - Hibernate 和 Jersey 依赖冲突(javassist) - 谁能解释这是如何工作的?

android - Flutter Pub : Expected a key while parsing a block mapping. 路径: