入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

java delphi aes 加密与解密文件兼容算法

创建时间:2016-02-03 投稿人: 浏览次数:2882

本文在oracle jdk 1.8, delphi xe3下面测试加密与解密模式都成功通过。


java端加密与解密算法代码

package com.shit;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {

	private static final byte[] PASSWORD=new byte[] { "t", "e", "s", "t", "_", "p", "a", "s", "s", "w", "o", "r", "d","1", "2", "3" }; 
	
	public static byte[] encrypt(byte[] Data) throws Exception {
		Key key = new SecretKeySpec(PASSWORD, "AES");
		// Cipher cipher =Cipher.getInstance("AES/ECB/PKCS5Padding");
		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] encVal = cipher.doFinal(Data);
		return encVal;
	}

	public static byte[] decrypt(byte[] encryptedData) throws Exception {
		Key key = new SecretKeySpec(PASSWORD, "AES");
		 Cipher chiper = Cipher.getInstance("AES");
		 chiper.init(Cipher.DECRYPT_MODE, key);
		byte[] decValue = chiper.doFinal(encryptedData);
		return decValue;
	}

	public static void encodeFile(String sourceFile,String outputFile) throws Exception {
		File file = new File(sourceFile);
		FileInputStream in = new FileInputStream(file);
		try{
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] tmpbuf = new byte[1024];
			int count = 0;
			while ((count = in.read(tmpbuf)) != -1) {
				bout.write(tmpbuf, 0, count);
				tmpbuf = new byte[1024];
			}
			byte[] orgData = bout.toByteArray();
			byte[] raw = encrypt(orgData);
			file = new File(outputFile);
			FileOutputStream out=null;
			try {
				out= new FileOutputStream(file);
				out.write(raw);
			} finally {
				out.close();
			}
		}finally{
			in.close();
		}
	}

	public static void decodeFile(String sourceFile,String outputFile) throws Exception{
		File file = new File(sourceFile);
		FileInputStream fis = new FileInputStream(file);
		try {
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] tmpbuf = new byte[1024];
			int count = 0;
			while ((count = fis.read(tmpbuf)) != -1) {
				bout.write(tmpbuf, 0, count);
				tmpbuf = new byte[1024];
			}
			byte[] orgData = bout.toByteArray();
			byte[] raws = decrypt(orgData);

			file = new File(outputFile);
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(file);
				fos.write(raws);
			} finally {
				fos.close();
			}
		} finally {
			fis.close();
		}
	}

	public static void main(String[] args) {
		String input_file="d:/1.jpg";
		String output_file="d:/1_encrypted.jpg";
		String after_decrypt_file="d:/2.jpg";
		try {
			encodeFile(input_file,output_file);
			decodeFile(output_file,after_decrypt_file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}

delphi xe3这边的加密及解密关键代码


function download_file_to_stream(const url: string; outputStream: TMemoryStream): TMemoryStream;
var
  h: TIdhttp;
begin
  h := Tidhttp.Create(nil);
  try
    try
      h.get(url, outputStream);
    except
    end;
    result := outputStream;
  finally
    h.Free;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  http_download_stream: TMemoryStream;
  output_stream: TMemoryStream;
  password:String;
begin
  password:="test_password123";
  http_download_stream := TMemoryStream.Create;
  output_stream := TMemoryStream.Create;
  try
    http_download_stream := download_file_to_stream("http://localhost:8080/docs/1_encrypted.jpg", http_download_stream);
    http_download_stream.Position := 0;
    output_stream := DecryptStream(http_download_stream, output_stream,password) as TMemoryStream;
    output_stream.Position := 0;
    output_stream.SaveToFile("d:/1_de.jpg");
  finally
    http_download_stream.Free;
    output_stream.Free;
  end;
end;

delphi这一边的aes算法我测试过dcrypt2,lockbox,cryptobboxvcl等这些,结论就是以上这几个全都不能用,java里面默认的aes加密实现是128bit的AES/ECB/PKCS5Padding这种模式的,在缺省加密模式下面涉及不到IV向量这些东西,就直接Cipher.getInstance("AES");就是AES/ECB/PKCS5Padding这种加密模式了,delphi这一边最终还是通过AES.pas和EIAES.pas这两个组件实现的,但是我下载到的源码有问题,无法完成与java之间的直接互相加解密转换,对于该代码进行了一定的修改以后才最终可以实现与java的互相加解密互换。


代码已经上传到csdn下载,具体源码下载地址:http://download.csdn.net/detail/peihexian/9426621


阅读更多
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。