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

本文在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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

peihexian

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值