用delphi 103 编写android 程序代码实现明文和密文的相互转换。
以下是一种可能的实现:
unit Unit1;
interface
uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Edit, FMX.StdCtrls;
type TForm1 = class(TForm) edtPlainText: TEdit; edtCipherText: TEdit; btnEncrypt: TButton; btnDecrypt: TButton; lblPlainText: TLabel; lblCipherText: TLabel; procedure btnEncryptClick(Sender: TObject); procedure btnDecryptClick(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.fmx}
uses Androidapi.JNI.JavaTypes, Androidapi.Helpers, Androidapi.JNI.Crypto;
function Encrypt(text: string): string;
var
cipher: JCipher;
keySpec: JKeySpec;
keyFactory: JKeyFactory;
key: JSecretKey;
cipherText: TJavaArray
function Decrypt(cipherText: string): string;
var
cipher: JCipher;
keySpec: JKeySpec;
keyFactory: JKeyFactory;
key: JSecretKey;
plainText: TJavaArray
procedure TForm1.btnEncryptClick(Sender: TObject); begin edtCipherText.Text := Encrypt(edtPlainText.Text); end;
procedure TForm1.btnDecryptClick(Sender: TObject); begin edtPlainText.Text := Decrypt(edtCipherText.Text); end;
end.
这个例子使用AES算法加密和解密字符串,密钥为"MySecretKey"。注意,这个密钥是明文存储在代码中的,实际应用中应该更加安全地存储密钥
原文地址: https://www.cveoy.top/t/topic/hnal 著作权归作者所有。请勿转载和采集!