日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Delphi与JAVA 互通AES文件加解密源码(支持D6-XE10)

發布時間:2023/12/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delphi与JAVA 互通AES文件加解密源码(支持D6-XE10) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Delphi用的人越來越少 了,可用資源越來越少。有什么好的完整的代碼我都盡量拿出來跟大家分享,部分內容也是來自互聯網整理

Delphi代碼:

unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls,ElAES,math;typeTForm1 = class(TForm)Button1: TButton;Button2: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;typeTKeyBit = (kb128, kb192, kb256);TCRLFMode = (rlCRLF, rlLF, rlCR, rlNONE);varForm1: TForm1;implementation{$R *.dfm}function ChangCRLFType(Value: string; CRLFMode: TCRLFMode): string; varHasCRLF: Boolean; beginResult := Value;HasCRLF := Pos(#10, Result) > 0;if not HasCRLF thenbeginHasCRLF := Pos(#13, Result) > 0;end;if HasCRLF thenbeginResult := StringReplace(Result, #13#10, #10, [rfReplaceAll]);Result := StringReplace(Result, #10#13, #10, [rfReplaceAll]);Result := StringReplace(Result, #13, #10, [rfReplaceAll]);if CRLFMode = rlCRLF thenbeginResult := StringReplace(Result, #10, #13#10, [rfReplaceAll]);endelse if CRLFMode = rlCR thenbeginResult := StringReplace(Result, #10, #13, [rfReplaceAll]);endelse if CRLFMode = rlNONE thenbeginResult := StringReplace(Result, #10, '', [rfReplaceAll]);end;end; end;function BytesOf(const Val: AnsiString): TBytes; varLen: Integer; beginLen := Length(Val);SetLength(Result, Len);Move(Val[1], Result[0], Len); end;function StringOf(const buf:TBytes): AnsiString; beginSetLength(Result, Length(buf));CopyMemory(PAnsiChar(result), @buf[0], Length(buf)); end;{ -- 流加密函數 默認按照 128 位密匙解密 -- } function EncryptStream(Stream: TStream; OutStrm:TStream;Key: string;KeyBit: TKeyBit = kb128; KeepKeyCRLF: Boolean = False;KeyCRLFMode: TCRLFMode = rlCRLF): TStream; varCount: Int64;AESKey128: TAESKey128;AESKey192: TAESKey192;AESKey256: TAESKey256;SS: TStringStream;AnsiKey: TBytes; beginif KeepKeyCRLF thenbeginKey := ChangCRLFType(Key,KeyCRLFMode);end;// AnsiKey := TEncoding.ASCII.GetBytes(Key);AnsiKey := BytesOf(Key); //已轉為ansi編碼 // OutStrm := TStream.Create;Stream.Position := 0;Count := Stream.Size;//OutStrm.Write(Count, SizeOf(Count));try{ -- 128 位密匙最大長度為 16 個字符 -- }if KeyBit = kb128 thenbeginFillChar(AESKey128, SizeOf(AESKey128), 0);Move(PByte(AnsiKey)^, AESKey128, Min(SizeOf(AESKey128), Length(AnsiKey)));EncryptAESStreamECB(Stream, 0, AESKey128, OutStrm);end;{ -- 192 位密匙最大長度為 24 個字符 -- }if KeyBit = kb192 thenbeginFillChar(AESKey192, SizeOf(AESKey192), 0);Move(PByte(AnsiKey)^, AESKey192, Min(SizeOf(AESKey192), Length(AnsiKey)));EncryptAESStreamECB(Stream, 0, AESKey192, OutStrm);end;{ -- 256 位密匙最大長度為 32 個字符 -- }if KeyBit = kb256 thenbeginFillChar(AESKey256, SizeOf(AESKey256), 0);Move(PByte(AnsiKey)^, AESKey256, Min(SizeOf(AESKey256), Length(AnsiKey)));EncryptAESStreamECB(Stream, 0, AESKey256, OutStrm);end;Result := OutStrm;finally//FreeAndNil(OutStrm);end; end;{ -- 流解密函數 默認按照 128 位密匙解密 -- } function DecryptStream(Stream: TStream;OutStrm: TStream; Key: string; KeyBit: TKeyBit = kb128; KeepKeyCRLF: Boolean = False; KeyCRLFMode: TCRLFMode = rlCRLF ): TStream; varCount, OutPos: Int64;AESKey128: TAESKey128;AESKey192: TAESKey192;AESKey256: TAESKey256;SS: TStringStream;AnsiKey: TBytes; beginif KeepKeyCRLF thenbeginKey := ChangCRLFType(Key,KeyCRLFMode);end;// AnsiKey := TEncoding.ASCII.GetBytes(Key);AnsiKey := BytesOf(Key); //已轉為ansi編碼Stream.Position := 0;OutPos := OutStrm.Position;//Stream.ReadBuffer(Count, SizeOf(Count));count:=Stream.Size - Stream.Position;try{ -- 128 位密匙最大長度為 16 個字符 -- }if KeyBit = kb128 thenbeginFillChar(AESKey128, SizeOf(AESKey128), 0);Move(PByte(AnsiKey)^, AESKey128, Min(SizeOf(AESKey128), Length(AnsiKey)));//add by phx 2016-02-02Stream.Position := 0; // DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, AESKey128, OutStrm);DecryptAESStreamECB(Stream, count, AESKey128, OutStrm);end;{ -- 192 位密匙最大長度為 24 個字符 -- }if KeyBit = kb192 thenbeginFillChar(AESKey192, SizeOf(AESKey192), 0);Move(PByte(AnsiKey)^, AESKey192, Min(SizeOf(AESKey192), Length(AnsiKey)));DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, AESKey192,OutStrm);end;{ -- 256 位密匙最大長度為 32 個字符 -- }if KeyBit = kb256 thenbeginFillChar(AESKey256, SizeOf(AESKey256), 0);Move(PByte(AnsiKey)^, AESKey256, Min(SizeOf(AESKey256), Length(AnsiKey)));DecryptAESStreamECB(Stream, Stream.Size - Stream.Position, AESKey256,OutStrm);end;OutStrm.Size := OutPos + Count;OutStrm.Position := OutPos;Result := OutStrm;finally//FreeAndNil(OutStrm);end; end;procedure TForm1.Button1Click(Sender: TObject); varInStream,OutStream: TMemoryStream;password: string; beginpassword := 'fy22moe2cobs4ygs';InStream := TMemoryStream.Create;OutStream := TMemoryStream.Create;try//InStream := download_file_to_stream('http://localhost:8080/docs/1_encrypted.jpg', http_download_stream);InStream.LoadFromFile('E:\軟件\idea_setting_Eecrypted.zip');InStream.Position := 0;OutStream := DecryptStream(InStream, OutStream, password) as TMemoryStream;OutStream.Position := 0;OutStream.SaveToFile('D:\idea_setting_Decrypted.zip');finallyInStream.Free;OutStream.Free;end;end;procedure TForm1.Button2Click(Sender: TObject); varmy_stream: TMemoryStream;out_stream: TMemoryStream;password: string; beginpassword := 'test_password123';trymy_stream := TMemoryStream.Create;out_stream := TMemoryStream.Create;my_stream.LoadFromFile('d:\1.jpg');EncryptStream(my_stream, out_stream, password);out_stream.Position := 0;out_stream.SaveToFile('d:\1_encrypted.jpg');finallyout_stream.Free;my_stream.Free;end;end;end.

JAVA代碼:

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.Key;/*** @author wh445306* @version 1.0* @Description* @Date 2021-05-08 23:46*/public class AES {private static final String PASSWORD ="fy22moe2cobs4ygs";public static byte[] encrypt(byte[] Data) throws Exception {Key key = new SecretKeySpec(PASSWORD.getBytes(), "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.getBytes(), "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(); // }// String input_file="D:\\Delphi_Project\\ETC\\bill_20210430_230103001_1_2.zip"; // String output_file="D:\\Delphi_Project\\ETC\\bill_20210430_230103001_1_2_Decrypted.zip";String input_file="E:\\軟件\\idea_setting.zip";String output_file="E:\\軟件\\idea_setting_Eecrypted.zip";//String after_decrypt_file="d:/2.jpg";try {encodeFile(input_file,output_file);// decodeFile(input_file,output_file);} catch (Exception e) {e.printStackTrace();}} }

總結

以上是生活随笔為你收集整理的Delphi与JAVA 互通AES文件加解密源码(支持D6-XE10)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。