欢迎光临散文网 会员登陆 & 注册

Android Studio 修改 QPython OC/3C 使支持 Javax.Crypto.Cipher 加解密

2021-05-01 14:25 作者:乘着船  | 我要投稿

适合软件平台:QPython 3C (Python 3.10),https://www.bilibili.com/read/cv13322251


新增11个SL4A函数,其中第1个cipherInit首次正确运行后,后面10个才会被导入。


  cipherInit( key, algorithm = "AES/CBC/PKCS5Padding", encodingFormat = "", initialVector = "" )

Cipher Initialize

key (String / Bytes)

algorithm (String Optional)

encodingFormat (String Optional)

initialVector (String / Bytes Optional)

if encodingFormat is UTF-8 or GBK etc., key and initialVector are Normal ( UTF-8 or GBK etc. ) String, if encodingFormat is omitted ( empty default ) , key and initialVector are Base64 String or Bytes .

algorithm support AES or DES etc.

if algorithm is AES, key need 16/24/32 bytes, if algorithm is DES, key need 8 bytes.

If initialVector used in AES/CBC algorithm, it need 16 bytes; if initialVector used in DES/CBC algorithm, it need 8 bytes.

If initialVector is omitted ( empty default ), then initialVector is set to be the same as key.


密码初始化( 密钥, 算法 = "AES/CBC/PKCS5Padding", 文字编码 = "", 初始向量 = "" )

密码初始化

密钥 (字符串 / 字节串)

算法 (字符串 可选参数)

文字编码 (字符串 可选参数)

初始向量 (字符串 / 字节串 可选参数)

如果 文字编码 是 UTF-8或GBK等, 那么 密钥 和 初始向量 是 普通 ( UTF-8或GBK等 ) 字符串, 如果 文字编码 被省略 ( 默认为空 ) , 那么 密钥 和 初始向量 是 Base64 字符串 或 字节串 .

算法 支持 AES或DES等

如果 算法 是 AES, 那么 密钥 需要 16/24/32 字节, 如果 算法 是 DES, 那么 密钥 需要 8 字节.

如果初始向量被用在 AES/CBC 算法上,需要 16 字节;如果初始向量被用在 DES/CBC 算法上,需要 8 字节.

如果 初始向量 被省略 ( 默认为空 ), 那么初始向量会被设置为和密钥一样.


  decryptBytes( srcBytes )

  Decrypt a Bytes to another Bytes,

  needs to run cipherInit first , and encodingFormat must omitted .

  srcBytes (Bytes)

  

  解密字节串( 源字节串 )

  解密 一个 字节串 到 另一个 字节串,

  需要先运行密码初始化 , 同时文字编码必须省略 .

  源字节串 (字节串)

  

  decryptFile( srcFile, dstFile )

  Decrypt a File to another File,

  needs to run cipherInit first .

  srcFile (String)

  dstFile (String)

  

  解密文件( 源文件, 目标文件 )

  解密 一个 文件 到 另一个 文件,

  需要先运行密码初始化 .

  源文件 (字符串)

  目标文件 (字符串)

  

  decryptFileToBytes( srcFile )

  Decrypt a File to another Bytes,

  needs to run cipherInit first , and encodingFormat must omitted .

  srcFile (String)

  

  解密文件到字节串( 源文件 )

  解密 一个 文件 到 另一个 字节串,

  需要先运行密码初始化 , 同时文字编码必须省略 .

  源文件 (字符串)

  

  decryptFileToString( srcFile )

  Decrypt a File to another Normal / Base64 String,

  needs to run cipherInit first .

  srcFile (String)

  

  解密文件到字符串( 源文件 )

  解密 一个 文件 到 另一个 普通 / Base64 字符串,

  需要先运行密码初始化 .

  源文件 (字符串)

  

  decryptString( srcString )

  Decrypt a Base64 String to another Normal / Base64 String,

  needs to run cipherInit first .

  srcString (String)

  

  解密字符串( 源字符串 )

  解密 一个 Base64 字符串 到 另一个 普通 / Base64 字符串,

  需要先运行密码初始化 .

  源字符串 (字符串)

  

  encryptBytes( srcBytes )

  Encrypt a Bytes to another Bytes ,

  needs to run cipherInit first , and encodingFormat must omitted .

  srcBytes (Bytes)

  

  加密字节串( 源字节串 )

  加密 一个 字节串 到 另一个 字节串 ,

  需要先运行密码初始化 , 同时文字编码必须省略 .

  源字节串 (字节串)

  

  encryptBytesToFile( srcBytes, dstFile )

  Encrypt a Bytes to another File ,

  needs to run cipherInit first , and encodingFormat must omitted .

  srcBytes (Bytes)

  dstFile (String)

  

  加密字节串到文件( 源字节串, 目标文件 )

  加密 一个 字节串 到 另一个 文件 ,

  需要先运行密码初始化 , 同时文字编码必须省略 .

  源字节串 (字节串)

  目标文件 (字符串)

  

  encryptFile( srcFile, dstFile )

  Encrypt a File to another File,

  needs to run cipherInit first .

  srcFile (String)

  dstFile (String)

  

  加密文件( 源文件, 目标文件 )

  加密 一个 文件 到 另一个 文件,

  需要先运行密码初始化 .

  源文件 (字符串)

  目标文件 (字符串)

  

  encryptString( srcString )

  Encrypt a Normal / Base64 String to another Base64 String ,

  needs to run cipherInit first .

  srcString (String)

  

  加密字符串( 源字符串 )

  加密 一个 普通 / Base64 字符串 到 另一个 Base64 字符串 ,

  需要先运行密码初始化 .

  源字符串 (字符串)

  

  encryptStringToFile( srcString, dstFile )

  Encrypt a Normal / Base64 String to another File ,

  needs to run cipherInit first .

  srcString (String)

  dstFile (String)

  

  加密字符串到文件( 源字符串, 目标文件 )

  加密 一个 普通 / Base64 字符串 到 另一个 文件 ,

  需要先运行密码初始化 .

  源字符串 (字符串)

  目标文件 (字符串)


 示例:


encryptBytes / decryptBytes
encryptString / decryptString
encryptBytesToFile / decryptFileToBytes
encryptStringToFile / decryptFileToString
encryptFile / decryptFile

  

原理:使用了 Javax.Crypto.Cipher 模块和 SL4A 服务。

支持的加密算法,请查看 Javax.Crypto.Cipher 类 说明,本文不再赘述:https://blog.csdn.net/u011502243/article/details/90749984


从 QPython OP 引出一个新小分支 QPython OC/3C ( QPython 修改版 ) 。

如果需要 QPython 3C 源代码/Apk ,请点击以下链接:

作者:乘着船@Bilibili

更多文章+下载链接:https://www.bilibili.com/read/readlist/rl321663


Android Studio 修改 QPython OC/3C 使支持 Javax.Crypto.Cipher 加解密的评论 (共 条)

分享到微博请遵守国家法律