鸿蒙仓颉语言【cryptocj 库】RSA 提供 RSA 生成公私钥,加解密,签名验证
·
7 RSA 提供 RSA 生成公私钥,加解密,签名验证
7.1 RSA
public class RSA {
/*
* 生成私钥
*
* 参数 bits - 生成私钥位数,一般为 1024、2048 比较安全
* 返回值 RSAPrivateKey - 私钥
*/
public static func generateKey(bits: Int32): RSAPrivateKey
/*
* 根据参数设置私钥
*
* 参数 n - 模数
* 参数 e - 公钥指数
* 参数 d - 私钥指数
* 参数 p - 素数
* 参数 q - 素数
* 参数 dmp = (e^-1) % (p - 1) 可使用 bignum 方法 bnModInverse
* 参数 dmq = (e^-1) % (q - 1) 可使用 bignum 方法 bnModInverse
* 参数 iqmp - = (q^-1) % p 可使用 bignum 方法 bnModInverse
* 返回值 RSAPrivateKey - 私钥
*/
public static func setKey(n: Array<UInt8>,
e: Array<UInt8>,
d: Array<UInt8>,
p: Array<UInt8>,
q: Array<UInt8>,
dmp: Array<UInt8>,
dmq: Array<UInt8>,
iqmp: Array<UInt8>): RSAPrivateKey
/*
* 通过密钥数组设置私钥
*
* 参数 key - 私钥数组
* 返回值 RSAPrivateKey - 私钥
*/
public static func setPrivateKey(key: Array<UInt8>): RSAPrivateKey
/*
* 通过密钥数组设置公钥
*
* 参数 key - 公钥数组
* 返回值 RSAPublicKey - 公钥
*/
public static func setPublicKey(key: Array<UInt8>): RSAPublicKey
/*
* 通过读取密钥文件设置私钥
*
* 参数 f - 私钥文件路径
* 参数 password - 私钥文件密码(可选择)
* 返回值 RSAPrivateKey - 私钥
*/
public static func readPriKeyfromFile(f: String, password!: String = ""): RSAPrivateKey
/*
* 通过读取公钥文件设置公钥
*
* 参数 f - 公钥文件路径
* 返回值 RSAPublicKey - 私钥
*/
public static func readPubKeyfromFile(f: String): RSAPublicKey
/*
* 公钥加密
*
* 参数 inside - 待加密数据
* 1. 小数据 (inside.size < padding.getFlen(rsa.getSize())) 直接加密
* 2. 大数据 (inside.size > padding.getFlen(rsa.getSize())) 分段加密
* padding.getFlen(len) 单次加密数据的最大长度
*
* 参数 padding - 填充模式,
* 可选: RSA_PKCS1_PADDING/RSA_PKCS1_OAEP_PADDING/RSA_NO_PADDING
* 参数 rsa - 公钥
* 返回值 Array<UInt8> - 公钥加密后结果
*/
public static func publicKeyEncrypt(inside: Array<UInt8>, padding: PADDING, rsa: RSAPublicKey): Array<UInt8>
/*
* 私钥解密
*
* 参数 inside - 待解密数据
* 1. 小数据 (inside.size < rsa.getSize()) 直接解密
* 2. 大数据 (inside.size > rsa.getSize()) 分段解密
* len = rsa.getSize() 单次解密数据的最大长度
*
* 参数 padding - 填充模式需和加密填充模式一致
* 参数 rsa - 私钥
* 返回值 Array<UInt8> - 私钥解密后结果
*/
public static func privateKeyDecrypt(inside: Array<UInt8>, padding: PADDING, rsa: RSAPrivateKey): Array<UInt8>
/*
* 私钥加密
* 1. 小数据 (inside.size < padding.getFlen(rsa.getSize())) 直接加密
* 2. 大数据 (inside.size > padding.getFlen(rsa.getSize())) 分段加密
* padding.getFlen(len) 单次加密数据的最大长度
*
* 参数 inside - 待加密数据
* 参数 padding - 填充模式,可选 RSA_PKCS1_PADDING/RSA_X931_PADDING/RSA_NO_PADDING
* 参数 rsa - 私钥
* 返回值 Array<UInt8> - 私钥加密后结果
*/
public static func privateKeyEncrypt(inside: Array<UInt8>, padding: PADDING, rsa: RSAPrivateKey): Array<UInt8>
/*
* 公钥解密
* 1. 小数据 (inside.size < rsa.getSize()) 直接解密
* 2. 大数据 (inside.size > rsa.getSize()) 分段解密
* len = rsa.getSize() 单次解密数据的最大长度
*
* 参数 inside - 待解密数据
* 参数 padding - 填充模式需和加密填充模式一致
* 参数 rsa - 公钥
* 返回值 Array<UInt8> - 公钥解密后结果
*/
public static func publicKeyDecrypt(inside: Array<UInt8>, padding: PADDING, rsa: RSAPublicKey): Array<UInt8>
/*
* 私钥签名
*
* 参数 types - 签名类型
* 参数 data - 待签名的摘要数据
* 参数 rsa - 私钥
* 返回值 Array<UInt8> - 签名后数据
*/
public static func sign(types: DigestType, data: Array<UInt8>, rsa: RSAPrivateKey): Array<UInt8>
/*
* 公钥验签
*
* 参数 types - 签名类型
* 参数 data - 待签名的摘要数据
* 参数 sigRet - 签名后的数据
* 参数 rsa - 公钥
* 返回值 Unit - 签名成功返回 Unit
* 失败报错: rsa verify error
*/
public static func verify(types: DigestType, data: Array<UInt8>, sigRet: Array<UInt8>, rsa: RSAPublicKey): Unit
}
7.2 RSAPrivateKey
public class RSAPrivateKey {
/*
* 获取私钥数组
*
* 返回值 Array<UInt8>
*/
public func getPrivateKey(): Array<UInt8>
/*
* 获取公钥数组
*
* 返回值 Array<UInt8>
*/
public func getPublicKey(): Array<UInt8>
/*
* 获取参数 n
*
* 返回值 Array<UInt8>
*/
public func getN(): Array<UInt8>
/*
* 获取参数 e
*
* 返回值 Array<UInt8>
*/
public func getE(): Array<UInt8>
/*
* 获取参数 d
*
* 返回值 Array<UInt8>
*/
public func getD(): Array<UInt8>
/*
* 获取参数 p
*
* 返回值 Array<UInt8>
*/
public func getP(): Array<UInt8>
/*
* 获取参数 q
*
* 返回值 Array<UInt8>
*/
public func getQ(): Array<UInt8>
/*
* 获取参数 dmp
*
* 返回值 Array<UInt8>
*/
public func getDmp(): Array<UInt8>
/*
* 获取参数 dmq
*
* 返回值 Array<UInt8>
*/
public func getDmq(): Array<UInt8>
/*
* 获取参数 iqmp
*
* 返回值 Array<UInt8>
*/
public func getIqmp(): Array<UInt8>
/*
* 获取私钥长度
*
* 返回值 Int32
*/
public func getSize(): Int32
/*
* 将私钥写入文件
*
* 参数 f - 文件路径
* 参数 enc - 加密方式
* 参数 password - 文件密码
* 返回值 Unit
*/
public func writePriKeyToFile(f: String, enc!: CIPHER = CIPHER(0), password!: String = ""): Unit
/*
* 将公钥写入文件
*
* 参数 f - 文件路径
* 返回值 Unit
*/
public func writePubKeyToFile(f: String): Unit
/*
* 释放私钥
*
* 返回值 Unit
*/
public func free(): Unit
}
7.3 RSAPublicKey
public class RSAPublicKey {
/*
* 获取公钥数组
*
* 返回值 Array<UInt8>
*/
public func getPublicKey(): Array<UInt8>
/*
* 获取参数 n
*
* 返回值 Array<UInt8>
*/
public func getN(): Array<UInt8>
/*
* 获取参数 e
*
* 返回值 Array<UInt8>
*/
public func getE(): Array<UInt8>
/*
* 获取公钥长度
*
* 返回值 Int32
*/
public func getSize(): Int32
/*
* 将公钥写入文件
*
* 参数 f - 文件路径
* 返回值 Unit
*/
public func writePubKeyToFile(f: String): Unit
/*
* 释放公钥
*
* 返回值 Unit
*/
public func free(): Unit
}
7.4 enum PADDING
public enum PADDING {
| RSA_PKCS1_PADDING
| RSA_NO_PADDING
| RSA_PKCS1_OAEP_PADDING
| RSA_X931_PADDING
/*
* 获取填充方式的单次加密数据的最大长度
* RSA_PKCS1_PADDING = rsaSize - 11
* RSA_NO_PADDING = rsaSize
* RSA_PKCS1_OAEP_PADDING = rsaSize - 42
* RSA_X931_PADDING = rsaSize - 2
*
* 参数 flen - 密钥长度 getSize
* 返回值 Int32
*/
public func getFlen(flen: Int32): Int32
}
7.5 示例
RSA 生成公私钥并将公私钥写入文件
from cryptocj import rsacj.*
from cryptocj import symmetrycj.*
from std import os.posix.*
from std import fs.*
main() {
var path: String = getcwd()
var rsa: RSAPrivateKey = RSA.generateKey(1024)
let n: Array<UInt8> = rsa.getN()
let e: Array<UInt8> = rsa.getE()
let d: Array<UInt8> = rsa.getD()
let p: Array<UInt8> = rsa.getP()
let q: Array<UInt8> = rsa.getQ()
let dmp: Array<UInt8> = rsa.getDmp()
let dmq: Array<UInt8> = rsa.getDmq()
let iqmp: Array<UInt8> = rsa.getIqmp()
println("n: ${n}")
println("e: ${e}")
println("d: ${d}")
println("p: ${p}")
println("q: ${q}")
println("dmp: ${dmp}")
println("dmq: ${dmq}")
println("iqmp: ${iqmp}")
let priKey: Array<UInt8> = rsa.getPrivateKey()
println("priKey: ${priKey}")
let pubKey: Array<UInt8> = rsa.getPublicKey()
println("pubKey: ${pubKey}")
rsa.writePubKeyToFile("${path}/pub.pem")
rsa.writePriKeyToFile("${path}/pri.pem", enc: DES.desEde3Ofb, password: "12345678")
rsa.free()
return 0
}
运行结果如下:
n: [201, 170, 87, 27, 225, 48, 127, 67, 179, 58, 250, 95, 194, 169, 229, 70, 210, 134, 108, 87, 137, 8, 189, 0, 220, 231, 125, 118, 156, 11, 171, 158, 150, 238, 146, 112, 98, 16, 191, 53, 116, 56, 163, 141, 119, 192, 236, 61, 224, 53, 50, 157, 233, 219, 217, 86, 175, 199, 74, 188, 123, 138, 146, 146, 128, 26, 239, 96, 132, 177, 121, 74, 189, 25, 171, 164, 39, 6, 86, 102, 110, 166, 132, 142, 238, 82, 247, 51, 205, 219, 168, 205, 63, 15, 21, 6, 123, 85, 114, 218, 147, 224, 44, 58, 145, 99, 149, 156, 151, 91, 7, 236, 88, 177, 99, 106, 133, 168, 116, 233, 215, 156, 216, 7, 64, 172, 65, 177]
e: [1, 0, 1]
d: [41, 255, 31, 68, 223, 16, 138, 112, 62, 210, 249, 113, 23, 200, 233, 198, 101, 241, 56, 126, 194, 58, 223, 161, 94, 95, 150, 241, 44, 110, 1, 191, 117, 15, 25, 207, 237, 156, 47, 53, 162, 142, 105, 0, 252, 255, 96, 51, 66, 141, 209, 140, 177, 54, 231, 123, 245, 255, 58, 193, 37, 185, 37, 236, 157, 52, 233, 12, 51, 52, 174, 46, 108, 183, 107, 209, 7, 193, 252, 214, 19, 173, 88, 196, 104, 70, 174, 226, 202, 147, 178, 89, 237, 82, 145, 106, 206, 152, 136, 79, 197, 96, 65, 195, 4, 201, 17, 231, 121, 164, 137, 128, 251, 206, 10, 220, 56, 254, 48, 108, 155, 151, 242, 130, 245, 192, 35, 65]
p: [252, 187, 115, 58, 42, 204, 64, 233, 116, 20, 25, 117, 231, 202, 1, 62, 48, 9, 7, 31, 53, 223, 81, 34, 218, 35, 210, 76, 171, 129, 223, 140, 4, 15, 158, 152, 11, 12, 196, 191, 160, 133, 120, 235, 41, 179, 19, 84, 197, 225, 187, 161, 3, 215, 169, 192, 166, 188, 215, 176, 68, 150, 114, 111]
q: [204, 69, 219, 144, 246, 138, 66, 57, 195, 245, 206, 11, 170, 158, 252, 43, 214, 28, 52, 198, 111, 80, 179, 100, 206, 174, 151, 133, 175, 58, 102, 114, 92, 204, 192, 152, 182, 36, 154, 35, 123, 229, 48, 125, 151, 114, 121, 189, 150, 187, 31, 87, 99, 232, 160, 87, 123, 209, 161, 153, 39, 216, 29, 223]
dmp: [247, 225, 134, 239, 219, 64, 64, 190, 121, 10, 169, 186, 58, 32, 168, 61, 109, 113, 189, 89, 128, 70, 75, 87, 42, 171, 141, 189, 131, 78, 59, 139, 34, 4, 139, 225, 198, 156, 188, 112, 211, 41, 118, 98, 248, 160, 90, 106, 128, 55, 154, 7, 32, 52, 171, 155, 120, 46, 170, 66, 137, 211, 33, 227]
dmq: [189, 94, 41, 141, 58, 176, 179, 114, 63, 178, 216, 191, 193, 253, 191, 2, 152, 107, 239, 148, 225, 206, 235, 111, 145, 151, 19, 74, 208, 70, 16, 129, 6, 124, 158, 51, 51, 239, 188, 161, 22, 147, 66, 159, 187, 77, 89, 132, 163, 126, 119, 48, 153, 163, 39, 57, 210, 196, 100, 205, 201, 189, 52, 251]
iqmp: [215, 198, 223, 87, 86, 123, 138, 11, 27, 88, 250, 111, 229, 229, 168, 255, 90, 147, 46, 45, 81, 63, 255, 163, 101, 20, 98, 73, 56, 117, 153, 168, 65, 94, 125, 70, 72, 103, 76, 30, 101, 34, 61, 245, 45, 127, 176, 132, 173, 165, 48, 118, 49, 142, 126, 81, 248, 147, 195, 234, 132, 198, 203, 138]
priKey: [48, 130, 2, 94, 2, 1, 0, 2, 129, 129, 0, 201, 170, 87, 27, 225, 48, 127, 67, 179, 58, 250, 95, 194, 169, 229, 70, 210, 134, 108, 87, 137, 8, 189, 0, 220, 231, 125, 118, 156, 11, 171, 158, 150, 238, 146, 112, 98, 16, 191, 53, 116, 56, 163, 141, 119, 192, 236, 61, 224, 53, 50, 157, 233, 219, 217, 86, 175, 199, 74, 188, 123, 138, 146, 146, 128, 26, 239, 96, 132, 177, 121, 74, 189, 25, 171, 164, 39, 6, 86, 102, 110, 166, 132, 142, 238, 82, 247, 51, 205, 219, 168, 205, 63, 15, 21, 6, 123, 85, 114, 218, 147, 224, 44, 58, 145, 99, 149, 156, 151, 91, 7, 236, 88, 177, 99, 106, 133, 168, 116, 233, 215, 156, 216, 7, 64, 172, 65, 177, 2, 3, 1, 0, 1, 2, 129, 128, 41, 255, 31, 68, 223, 16, 138, 112, 62, 210, 249, 113, 23, 200, 233, 198, 101, 241, 56, 126, 194, 58, 223, 161, 94, 95, 150, 241, 44, 110, 1, 191, 117, 15, 25, 207, 237, 156, 47, 53, 162, 142, 105, 0, 252, 255, 96, 51, 66, 141, 209, 140, 177, 54, 231, 123, 245, 255, 58, 193, 37, 185, 37, 236, 157, 52, 233, 12, 51, 52, 174, 46, 108, 183, 107, 209, 7, 193, 252, 214, 19, 173, 88, 196, 104, 70, 174, 226, 202, 147, 178, 89, 237, 82, 145, 106, 206, 152, 136, 79, 197, 96, 65, 195, 4, 201, 17, 231, 121, 164, 137, 128, 251, 206, 10, 220, 56, 254, 48, 108, 155, 151, 242, 130, 245, 192, 35, 65, 2, 65, 0, 252, 187, 115, 58, 42, 204, 64, 233, 116, 20, 25, 117, 231, 202, 1, 62, 48, 9, 7, 31, 53, 223, 81, 34, 218, 35, 210, 76, 171, 129, 223, 140, 4, 15, 158, 152, 11, 12, 196, 191, 160, 133, 120, 235, 41, 179, 19, 84, 197, 225, 187, 161, 3, 215, 169, 192, 166, 188, 215, 176, 68, 150, 114, 111, 2, 65, 0, 204, 69, 219, 144, 246, 138, 66, 57, 195, 245, 206, 11, 170, 158, 252, 43, 214, 28, 52, 198, 111, 80, 179, 100, 206, 174, 151, 133, 175, 58, 102, 114, 92, 204, 192, 152, 182, 36, 154, 35, 123, 229, 48, 125, 151, 114, 121, 189, 150, 187, 31, 87, 99, 232, 160, 87, 123, 209, 161, 153, 39, 216, 29, 223, 2, 65, 0, 247, 225, 134, 239, 219, 64, 64, 190, 121, 10, 169, 186, 58, 32, 168, 61, 109, 113, 189, 89, 128, 70, 75, 87, 42, 171, 141, 189, 131, 78, 59, 139, 34, 4, 139, 225, 198, 156, 188, 112, 211, 41, 118, 98, 248, 160, 90, 106, 128, 55, 154, 7, 32, 52, 171, 155, 120, 46, 170, 66, 137, 211, 33, 227, 2, 65, 0, 189, 94, 41, 141, 58, 176, 179, 114, 63, 178, 216, 191, 193, 253, 191, 2, 152, 107, 239, 148, 225, 206, 235, 111, 145, 151, 19, 74, 208, 70, 16, 129, 6, 124, 158, 51, 51, 239, 188, 161, 22, 147, 66, 159, 187, 77, 89, 132, 163, 126, 119, 48, 153, 163, 39, 57, 210, 196, 100, 205, 201, 189, 52, 251, 2, 65, 0, 215, 198, 223, 87, 86, 123, 138, 11, 27, 88, 250, 111, 229, 229, 168, 255, 90, 147, 46, 45, 81, 63, 255, 163, 101, 20, 98, 73, 56, 117, 153, 168, 65, 94, 125, 70, 72, 103, 76, 30, 101, 34, 61, 245, 45, 127, 176, 132, 173, 165, 48, 118, 49, 142, 126, 81, 248, 147, 195, 234, 132, 198, 203, 138]
pubKey: [48, 129, 137, 2, 129, 129, 0, 201, 170, 87, 27, 225, 48, 127, 67, 179, 58, 250, 95, 194, 169, 229, 70, 210, 134, 108, 87, 137, 8, 189, 0, 220, 231, 125, 118, 156, 11, 171, 158, 150, 238, 146, 112, 98, 16, 191, 53, 116, 56, 163, 141, 119, 192, 236, 61, 224, 53, 50, 157, 233, 219, 217, 86, 175, 199, 74, 188, 123, 138, 146, 146, 128, 26, 239, 96, 132, 177, 121, 74, 189, 25, 171, 164, 39, 6, 86, 102, 110, 166, 132, 142, 238, 82, 247, 51, 205, 219, 168, 205, 63, 15, 21, 6, 123, 85, 114, 218, 147, 224, 44, 58, 145, 99, 149, 156, 151, 91, 7, 236, 88, 177, 99, 106, 133, 168, 116, 233, 215, 156, 216, 7, 64, 172, 65, 177, 2, 3, 1, 0, 1]
RSA 公钥加密私钥解密(小数据)
私钥加密公钥解密与此类似
from cryptocj import rsacj.*
main() {
let pri: Array<UInt8> =[
48, 130, 2, 92, 2, 1, 0, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229,
234, 42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166, 15,
82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244, 165, 174,
58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149, 176, 92, 33,
156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116, 225, 208, 37, 178,
3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199, 229, 147, 13, 178, 196,
8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169, 120, 28, 24, 246, 110, 142,
105, 46, 98, 32, 17, 129, 145, 210, 65, 249, 227, 33, 2, 27, 185, 2, 1, 3, 2,
129, 129, 0, 145, 175, 237, 44, 70, 41, 73, 238, 156, 28, 48, 96, 28, 166, 196,
91, 157, 2, 196, 74, 82, 13, 57, 109, 25, 95, 140, 0, 233, 33, 190, 13, 211,
237, 24, 254, 138, 2, 229, 137, 193, 248, 110, 116, 38, 214, 240, 145, 161, 254,
73, 13, 100, 61, 27, 171, 72, 99, 202, 232, 22, 104, 93, 4, 19, 180, 157, 18,
228, 155, 153, 225, 246, 60, 235, 75, 187, 2, 2, 255, 115, 128, 28, 79, 78, 97,
199, 118, 59, 147, 156, 179, 156, 247, 100, 58, 113, 150, 54, 241, 47, 87, 167,
234, 125, 167, 98, 196, 209, 93, 194, 226, 158, 219, 159, 154, 222, 102, 166,
15, 188, 165, 153, 231, 45, 45, 87, 163, 2, 65, 0, 245, 208, 73, 78, 10, 130,
179, 5, 235, 9, 16, 72, 29, 166, 20, 75, 47, 253, 159, 231, 159, 246, 28, 211,
225, 230, 35, 170, 47, 140, 56, 61, 32, 74, 122, 254, 129, 73, 180, 45, 166,
58, 224, 146, 11, 234, 172, 64, 116, 30, 137, 139, 220, 147, 150, 131, 11, 55,
129, 90, 133, 8, 122, 127, 2, 65, 0, 227, 150, 44, 124, 104, 225, 79, 70, 158,
16, 112, 150, 111, 136, 234, 96, 200, 143, 71, 174, 206, 127, 230, 102, 203,
230, 57, 45, 248, 14, 100, 47, 61, 149, 156, 162, 111, 13, 196, 238, 122, 4,
180, 190, 214, 33, 165, 218, 44, 1, 53, 109, 245, 227, 241, 247, 44, 18, 17,
173, 216, 53, 157, 199, 2, 65, 0, 163, 224, 48, 222, 177, 172, 119, 89, 71,
91, 96, 48, 19, 196, 13, 135, 117, 83, 191, 239, 191, 249, 104, 141, 65, 68,
23, 198, 202, 93, 122, 211, 106, 220, 81, 255, 0, 219, 205, 115, 196, 39, 64,
97, 93, 71, 29, 128, 77, 105, 177, 7, 232, 98, 100, 87, 92, 207, 171, 145, 174,
5, 166, 255, 2, 65, 0, 151, 185, 114, 253, 155, 64, 223, 132, 105, 96, 75, 14,
245, 5, 241, 149, 219, 10, 47, 201, 222, 255, 238, 239, 50, 153, 123, 115, 250,
180, 66, 202, 41, 14, 104, 108, 74, 9, 45, 244, 81, 88, 120, 127, 57, 107, 195,
230, 200, 0, 206, 73, 78, 151, 246, 164, 200, 12, 11, 201, 58, 206, 105, 47, 2,
64, 32, 24, 155, 16, 110, 103, 52, 8, 123, 44, 18, 132, 54, 25, 9, 146, 15, 185,
184, 118, 107, 124, 196, 24, 212, 51, 32, 166, 34, 207, 174, 172, 186, 221, 2,
120, 97, 18, 178, 75, 8, 195, 250, 60, 252, 152, 100, 136, 131, 145, 157, 197,
50, 152, 241, 65, 93, 124, 235, 239, 241, 122, 91, 114]
let pub: Array<UInt8> = [
48, 129, 135, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229, 234,
42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166,
15, 82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244,
165, 174, 58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149,
176, 92, 33, 156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116,
225, 208, 37, 178, 3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199,
229, 147, 13, 178, 196, 8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169,
120, 28, 24, 246, 110, 142, 105, 46, 98, 32, 17, 129, 145, 210, 65, 249,
227, 33, 2, 27, 185, 2, 1, 3
]
var prik: RSAPrivateKey = RSA.setPrivateKey(pri)
var pubk: RSAPublicKey = RSA.setPublicKey(pub)
let datas: Array<UInt8> = "sffsaffsafscxaffsaffsaffsaffsaffsaffsafaffsaffsffsaffsffsaff".toUtf8Array()
rsaPubEnc(datas, prik, pubk, RSA_PKCS1_PADDING)
//rsaPubEnc(datas, prik, pubk, RSA_PKCS1_OAEP_PADDING)
//rsaPubEnc(datas, prik, pubk, RSA_NO_PADDING)
return 0
}
func rsaPubEnc(datas: Array<UInt8>, prik: RSAPrivateKey, pubk: RSAPublicKey, padding: PADDING): Int32 {
let encryptDatas: Array<UInt8> = RSA.publicKeyEncrypt(datas, padding, pubk)
println("原数据:${datas}")
println("加密后数据:${encryptDatas}")
println("加密后数据长度:${encryptDatas.size}")
let decryptDatas: Array<UInt8> = RSA.privateKeyDecrypt(encryptDatas, padding, prik)
println("解密后数据:${decryptDatas}")
if(datas != decryptDatas) {
return -1
}
return 0
}
运行结果如下:
原数据:[115, 102, 102, 115, 97, 102, 102, 115, 97, 102, 115, 99, 120, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 97, 102, 102, 115, 97, 102, 102, 115, 102, 102, 115, 97, 102, 102, 115, 102, 102, 115, 97, 102, 102]
加密后数据:[24, 119, 174, 170, 46, 243, 161, 26, 65, 149, 1, 19, 119, 75, 82, 56, 81, 134, 72, 241, 45, 140, 27, 238, 251, 236, 227, 208, 79, 4, 52, 176, 228, 193, 224, 168, 241, 226, 160, 124, 7, 44, 205, 111, 70, 156, 76, 148, 8, 32, 133, 230, 119, 212, 205, 213, 77, 1, 50, 62, 65, 123, 201, 34, 134, 244, 25, 18, 67, 240, 224, 20, 107, 62, 253, 98, 140, 189, 132, 173, 115, 232, 29, 162, 75, 87, 95, 151, 195, 112, 140, 115, 46, 77, 236, 122, 172, 88, 148, 88, 75, 86, 69, 93, 207, 145, 164, 219, 107, 231, 98, 193, 84, 88, 230, 223, 227, 255, 77, 211, 248, 58, 92, 154, 231, 93, 21, 92]
加密后数据长度:128
解密后数据:[115, 102, 102, 115, 97, 102, 102, 115, 97, 102, 115, 99, 120, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 102, 115, 97, 102, 97, 102, 102, 115, 97, 102, 102, 115, 102, 102, 115, 97, 102, 102, 115, 102, 102, 115, 97, 102, 102]
RSA 公钥加密私钥解密(大数据)
私钥加密公钥解密与此类似
from cryptocj import rsacj.*
from std import collection.*
main() {
let pri: Array<UInt8> =[
48, 130, 2, 92, 2, 1, 0, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229,
234, 42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166, 15,
82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244, 165, 174,
58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149, 176, 92, 33,
156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116, 225, 208, 37, 178,
3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199, 229, 147, 13, 178, 196,
8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169, 120, 28, 24, 246, 110, 142,
105, 46, 98, 32, 17, 129, 145, 210, 65, 249, 227, 33, 2, 27, 185, 2, 1, 3, 2,
129, 129, 0, 145, 175, 237, 44, 70, 41, 73, 238, 156, 28, 48, 96, 28, 166, 196,
91, 157, 2, 196, 74, 82, 13, 57, 109, 25, 95, 140, 0, 233, 33, 190, 13, 211,
237, 24, 254, 138, 2, 229, 137, 193, 248, 110, 116, 38, 214, 240, 145, 161, 254,
73, 13, 100, 61, 27, 171, 72, 99, 202, 232, 22, 104, 93, 4, 19, 180, 157, 18,
228, 155, 153, 225, 246, 60, 235, 75, 187, 2, 2, 255, 115, 128, 28, 79, 78, 97,
199, 118, 59, 147, 156, 179, 156, 247, 100, 58, 113, 150, 54, 241, 47, 87, 167,
234, 125, 167, 98, 196, 209, 93, 194, 226, 158, 219, 159, 154, 222, 102, 166,
15, 188, 165, 153, 231, 45, 45, 87, 163, 2, 65, 0, 245, 208, 73, 78, 10, 130,
179, 5, 235, 9, 16, 72, 29, 166, 20, 75, 47, 253, 159, 231, 159, 246, 28, 211,
225, 230, 35, 170, 47, 140, 56, 61, 32, 74, 122, 254, 129, 73, 180, 45, 166,
58, 224, 146, 11, 234, 172, 64, 116, 30, 137, 139, 220, 147, 150, 131, 11, 55,
129, 90, 133, 8, 122, 127, 2, 65, 0, 227, 150, 44, 124, 104, 225, 79, 70, 158,
16, 112, 150, 111, 136, 234, 96, 200, 143, 71, 174, 206, 127, 230, 102, 203,
230, 57, 45, 248, 14, 100, 47, 61, 149, 156, 162, 111, 13, 196, 238, 122, 4,
180, 190, 214, 33, 165, 218, 44, 1, 53, 109, 245, 227, 241, 247, 44, 18, 17,
173, 216, 53, 157, 199, 2, 65, 0, 163, 224, 48, 222, 177, 172, 119, 89, 71,
91, 96, 48, 19, 196, 13, 135, 117, 83, 191, 239, 191, 249, 104, 141, 65, 68,
23, 198, 202, 93, 122, 211, 106, 220, 81, 255, 0, 219, 205, 115, 196, 39, 64,
97, 93, 71, 29, 128, 77, 105, 177, 7, 232, 98, 100, 87, 92, 207, 171, 145, 174,
5, 166, 255, 2, 65, 0, 151, 185, 114, 253, 155, 64, 223, 132, 105, 96, 75, 14,
245, 5, 241, 149, 219, 10, 47, 201, 222, 255, 238, 239, 50, 153, 123, 115, 250,
180, 66, 202, 41, 14, 104, 108, 74, 9, 45, 244, 81, 88, 120, 127, 57, 107, 195,
230, 200, 0, 206, 73, 78, 151, 246, 164, 200, 12, 11, 201, 58, 206, 105, 47, 2,
64, 32, 24, 155, 16, 110, 103, 52, 8, 123, 44, 18, 132, 54, 25, 9, 146, 15, 185,
184, 118, 107, 124, 196, 24, 212, 51, 32, 166, 34, 207, 174, 172, 186, 221, 2,
120, 97, 18, 178, 75, 8, 195, 250, 60, 252, 152, 100, 136, 131, 145, 157, 197,
50, 152, 241, 65, 93, 124, 235, 239, 241, 122, 91, 114]
let pub: Array<UInt8> = [
48, 129, 135, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229, 234,
42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166,
15, 82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244,
165, 174, 58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149,
176, 92, 33, 156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116,
225, 208, 37, 178, 3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199,
229, 147, 13, 178, 196, 8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169,
120, 28, 24, 246, 110, 142, 105, 46, 98, 32, 17, 129, 145, 210, 65, 249,
227, 33, 2, 27, 185, 2, 1, 3
]
var prik: RSAPrivateKey = RSA.setPrivateKey(pri)
var pubk: RSAPublicKey = RSA.setPublicKey(pub)
let datas: Array<UInt8> = """
The EVP interface supports the ability to perform authenticated encryption and decryption,
as well as the option to attach unencrypted, associated data to the message.
Such Authenticated-Encryption with Associated-Data (AEAD) schemes provide confidentiality by encrypting the data,
and also provide authenticity assurances by creating a MAC tag over the encrypted data.
The MAC tag will ensure the data is not accidentally altered or maliciously tampered during transmission and storage.
""".toUtf8Array()
diffPadding(datas, prik, pubk, RSA_PKCS1_PADDING)
//diffPadding(datas, prik, pubk, RSA_PKCS1_OAEP_PADDING)
//diffPadding(datas, prik, pubk, RSA_NO_PADDING)
return 0
}
func diffPadding(datas: Array<UInt8>, prik: RSAPrivateKey, pubk: RSAPublicKey, padding: PADDING): Int32 {
let encryptDatas: Array<UInt8> = pubKeyEncrypt(datas, pubk, padding)
println("原数据:${datas}")
println("加密后数据:${encryptDatas}")
println("加密后数据长度:${encryptDatas.size}")
let decryptDatas: Array<UInt8> = priKeyDecrypt(encryptDatas, prik, padding)
println("解密后数据:${decryptDatas}")
if(datas != decryptDatas) {
return -1
}
return 0
}
// 数据量大,需要进行分段加解密
//公钥加密
func pubKeyEncrypt(datas: Array<UInt8>, pubk: RSAPublicKey, padding: PADDING): Array<UInt8> {
let result: ArrayList<UInt8> = ArrayList<UInt8>()
var pos: Int64 = 0
let len: Int32 = pubk.getSize() // 获取RSA单次可以处理的数据的最大长度
let block_len = padding.getFlen(len) // 单次加密数据的最大长度
var sub_str: Array<UInt8>
while(pos < datas.size) {
if (pos + Int64(block_len) > datas.size) {
sub_str = datas.slice(pos, datas.size - pos)
} else {
sub_str = datas.slice(pos, Int64(block_len))
}
result.appendAll(RSA.publicKeyEncrypt(sub_str, padding, pubk))
pos += Int64(block_len)
}
return result.toArray()
}
func priKeyDecrypt(datas: Array<UInt8>, prik: RSAPrivateKey, padding: PADDING): Array<UInt8> {
let result: ArrayList<UInt8> = ArrayList<UInt8>()
var pos: Int64 = 0
let len: Int32 = prik.getSize() // 获取RSA单次可以处理的数据的最大长度,由于加密后长度始终为len的倍数
var sub_str: Array<UInt8>
while(pos < datas.size) {
sub_str = datas.slice(pos, Int64(len))
result.appendAll(RSA.privateKeyDecrypt(sub_str, padding, prik))
pos += Int64(len)
}
return result.toArray()
}
运行结果如下:
原数据:[32, 32, 32, 32, 84, 104, 101, 32, 69, 86, 80, 32, 105, 110, 116, 101, 114, 102, 97, 99, 101, 32, 115, 117, 112, 112, 111, 114, 116, 115, 32, 116, 104, 101, 32, 97, 98, 105, 108, 105, 116, 121, 32, 116, 111, 32, 112, 101, 114, 102, 111, 114, 109, 32, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 101, 100, 32, 101, 110, 99, 114, 121, 112, 116, 105, 111, 110, 32, 97, 110, 100, 32, 100, 101, 99, 114, 121, 112, 116, 105, 111, 110, 44, 32, 10, 32, 32, 32, 32, 97, 115, 32, 119, 101, 108, 108, 32, 97, 115, 32, 116, 104, 101, 32, 111, 112, 116, 105, 111, 110, 32, 116, 111, 32, 97, 116, 116, 97, 99, 104, 32, 117, 110, 101, 110, 99, 114, 121, 112, 116, 101, 100, 44, 32, 97, 115, 115, 111, 99, 105, 97, 116, 101, 100, 32, 100, 97, 116, 97, 32, 116, 111, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 46, 32, 10, 32, 32, 32, 32, 83, 117, 99, 104, 32, 65, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 101, 100, 45, 69, 110, 99, 114, 121, 112, 116, 105, 111, 110, 32, 119, 105, 116, 104, 32, 65, 115, 115, 111, 99, 105, 97, 116, 101, 100, 45, 68, 97, 116, 97, 32, 40, 65, 69, 65, 68, 41, 32, 115, 99, 104, 101, 109, 101, 115, 32, 112, 114, 111, 118, 105, 100, 101, 32, 99, 111, 110, 102, 105, 100, 101, 110, 116, 105, 97, 108, 105, 116, 121, 32, 98, 121, 32, 101, 110, 99, 114, 121, 112, 116, 105, 110, 103, 32, 116, 104, 101, 32, 100, 97, 116, 97, 44, 32, 10, 32, 32, 32, 32, 97, 110, 100, 32, 97, 108, 115, 111, 32, 112, 114, 111, 118, 105, 100, 101, 32, 97, 117, 116, 104, 101, 110, 116, 105, 99, 105, 116, 121, 32, 97, 115, 115, 117, 114, 97, 110, 99, 101, 115, 32, 98, 121, 32, 99, 114, 101, 97, 116, 105, 110, 103, 32, 97, 32, 77, 65, 67, 32, 116, 97, 103, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 101, 110, 99, 114, 121, 112, 116, 101, 100, 32, 100, 97, 116, 97, 46, 32, 10, 32, 32, 32, 32, 84, 104, 101, 32, 77, 65, 67, 32, 116, 97, 103, 32, 119, 105, 108, 108, 32, 101, 110, 115, 117, 114, 101, 32, 116, 104, 101, 32, 100, 97, 116, 97, 32, 105, 115, 32, 110, 111, 116, 32, 97, 99, 99, 105, 100, 101, 110, 116, 97, 108, 108, 121, 32, 97, 108, 116, 101, 114, 101, 100, 32, 111, 114, 32, 109, 97, 108, 105, 99, 105, 111, 117, 115, 108, 121, 32, 116, 97, 109, 112, 101, 114, 101, 100, 32, 100, 117, 114, 105, 110, 103, 32, 116, 114, 97, 110, 115, 109, 105, 115, 115, 105, 111, 110, 32, 97, 110, 100, 32, 115, 116, 111, 114, 97, 103, 101, 46, 10, 32, 32, 32, 32]
加密后数据:[12, 174, 221, 86, 192, 201, 108, 63, 97, 239, 200, 129, 36, 186, 231, 135, 60, 85, 147, 71, 5, 24, 177, 250, 48, 68, 114, 180, 142, 68, 106, 129, 178, 37, 213, 109, 75, 167, 82, 233, 76, 138, 123, 84, 48, 9, 196, 37, 154, 6, 233, 58, 123, 81, 135, 54, 243, 27, 85, 104, 201, 36, 127, 246, 23, 123, 221, 0, 220, 147, 170, 128, 170, 238, 226, 108, 225, 152, 125, 60, 16, 218, 142, 24, 161, 67, 244, 186, 169, 173, 162, 219, 155, 99, 220, 130, 237, 28, 180, 32, 178, 79, 215, 127, 232, 191, 118, 186, 48, 90, 126, 165, 189, 148, 78, 130, 203, 246, 164, 54, 35, 77, 24, 39, 219, 19, 191, 30, 53, 229, 74, 232, 93, 53, 24, 40, 168, 25, 141, 174, 32, 233, 255, 174, 25, 211, 137, 191, 51, 0, 11, 250, 20, 52, 219, 0, 247, 185, 138, 50, 227, 151, 132, 80, 251, 105, 225, 224, 30, 255, 35, 40, 192, 5, 38, 92, 18, 6, 83, 150, 70, 155, 248, 221, 213, 71, 113, 199, 9, 111, 103, 32, 42, 38, 193, 81, 158, 71, 74, 11, 183, 207, 52, 4, 220, 41, 148, 210, 126, 252, 192, 11, 2, 19, 77, 252, 220, 215, 170, 125, 146, 246, 195, 101, 82, 17, 92, 143, 243, 201, 174, 0, 141, 54, 4, 68, 175, 49, 248, 178, 81, 33, 192, 94, 244, 112, 179, 241, 175, 207, 152, 176, 122, 186, 53, 140, 79, 209, 208, 122, 97, 96, 107, 175, 140, 1, 231, 32, 241, 153, 117, 176, 173, 147, 218, 181, 128, 169, 108, 186, 164, 6, 199, 80, 44, 41, 47, 155, 239, 243, 161, 22, 70, 247, 156, 206, 107, 103, 147, 110, 87, 233, 4, 130, 198, 179, 167, 252, 145, 20, 81, 172, 179, 161, 84, 226, 139, 171, 247, 45, 9, 134, 118, 105, 9, 131, 83, 114, 54, 118, 16, 120, 130, 143, 160, 242, 130, 222, 92, 97, 185, 167, 50, 59, 59, 120, 127, 85, 137, 223, 41, 184, 229, 30, 99, 71, 41, 202, 11, 112, 115, 11, 69, 93, 148, 113, 116, 64, 42, 138, 109, 114, 60, 73, 212, 246, 124, 115, 240, 231, 146, 126, 15, 186, 108, 25, 198, 210, 38, 51, 68, 91, 160, 61, 92, 26, 174, 195, 15, 135, 70, 244, 151, 121, 3, 86, 168, 61, 162, 212, 95, 89, 182, 174, 207, 122, 30, 244, 157, 207, 198, 187, 5, 129, 58, 4, 143, 133, 199, 249, 28, 49, 89, 220, 253, 9, 250, 176, 192, 228, 168, 62, 146, 43, 249, 124, 233, 126, 7, 38, 28, 98, 151, 223, 75, 148, 116, 243, 33, 217, 48, 219, 29, 157, 53, 80, 12, 23, 245, 96, 55, 153, 47, 33, 63, 84, 231, 77, 229, 226, 104, 90, 247, 220, 116, 33, 109, 244, 165, 254, 137, 139, 217, 152, 246, 104, 101, 160, 69, 138, 129, 194, 151, 93, 203, 105, 204, 28, 224, 201, 199, 197, 148, 174, 97, 11, 84, 124, 208, 162, 249, 246, 129, 247, 146, 159, 12, 225, 5, 32, 101, 80, 181, 222, 29, 237, 7, 44, 89, 191, 159, 121, 137, 229, 51, 164, 57, 187, 132, 246, 200, 27, 163, 229, 127, 198, 211, 0, 95, 180, 205, 99, 48, 75, 79, 212, 198, 212, 157, 30, 17, 206, 202, 104, 74, 112, 86, 41, 57, 74, 82, 199, 27, 61, 61, 63, 64, 227, 6, 124, 226, 237, 61, 253, 51, 27, 179, 202, 2, 232, 221, 250, 26, 94, 42, 61, 94, 18, 52, 23, 185, 121, 16, 246, 142, 99, 57, 15, 129, 26, 161, 62, 235, 191, 135, 194, 200, 59, 169, 10, 238, 23, 189, 235, 199, 74, 236, 72, 39, 169]
加密后数据长度:640
解密后数据:[32, 32, 32, 32, 84, 104, 101, 32, 69, 86, 80, 32, 105, 110, 116, 101, 114, 102, 97, 99, 101, 32, 115, 117, 112, 112, 111, 114, 116, 115, 32, 116, 104, 101, 32, 97, 98, 105, 108, 105, 116, 121, 32, 116, 111, 32, 112, 101, 114, 102, 111, 114, 109, 32, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 101, 100, 32, 101, 110, 99, 114, 121, 112, 116, 105, 111, 110, 32, 97, 110, 100, 32, 100, 101, 99, 114, 121, 112, 116, 105, 111, 110, 44, 32, 10, 32, 32, 32, 32, 97, 115, 32, 119, 101, 108, 108, 32, 97, 115, 32, 116, 104, 101, 32, 111, 112, 116, 105, 111, 110, 32, 116, 111, 32, 97, 116, 116, 97, 99, 104, 32, 117, 110, 101, 110, 99, 114, 121, 112, 116, 101, 100, 44, 32, 97, 115, 115, 111, 99, 105, 97, 116, 101, 100, 32, 100, 97, 116, 97, 32, 116, 111, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 46, 32, 10, 32, 32, 32, 32, 83, 117, 99, 104, 32, 65, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 101, 100, 45, 69, 110, 99, 114, 121, 112, 116, 105, 111, 110, 32, 119, 105, 116, 104, 32, 65, 115, 115, 111, 99, 105, 97, 116, 101, 100, 45, 68, 97, 116, 97, 32, 40, 65, 69, 65, 68, 41, 32, 115, 99, 104, 101, 109, 101, 115, 32, 112, 114, 111, 118, 105, 100, 101, 32, 99, 111, 110, 102, 105, 100, 101, 110, 116, 105, 97, 108, 105, 116, 121, 32, 98, 121, 32, 101, 110, 99, 114, 121, 112, 116, 105, 110, 103, 32, 116, 104, 101, 32, 100, 97, 116, 97, 44, 32, 10, 32, 32, 32, 32, 97, 110, 100, 32, 97, 108, 115, 111, 32, 112, 114, 111, 118, 105, 100, 101, 32, 97, 117, 116, 104, 101, 110, 116, 105, 99, 105, 116, 121, 32, 97, 115, 115, 117, 114, 97, 110, 99, 101, 115, 32, 98, 121, 32, 99, 114, 101, 97, 116, 105, 110, 103, 32, 97, 32, 77, 65, 67, 32, 116, 97, 103, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 101, 110, 99, 114, 121, 112, 116, 101, 100, 32, 100, 97, 116, 97, 46, 32, 10, 32, 32, 32, 32, 84, 104, 101, 32, 77, 65, 67, 32, 116, 97, 103, 32, 119, 105, 108, 108, 32, 101, 110, 115, 117, 114, 101, 32, 116, 104, 101, 32, 100, 97, 116, 97, 32, 105, 115, 32, 110, 111, 116, 32, 97, 99, 99, 105, 100, 101, 110, 116, 97, 108, 108, 121, 32, 97, 108, 116, 101, 114, 101, 100, 32, 111, 114, 32, 109, 97, 108, 105, 99, 105, 111, 117, 115, 108, 121, 32, 116, 97, 109, 112, 101, 114, 101, 100, 32, 100, 117, 114, 105, 110, 103, 32, 116, 114, 97, 110, 115, 109, 105, 115, 115, 105, 111, 110, 32, 97, 110, 100, 32, 115, 116, 111, 114, 97, 103, 101, 46, 10, 32, 32, 32, 32]
RSA 签名验证
from cryptocj import rsacj.*
from cryptocj import digestcj.*
main() {
let pri: Array<UInt8> =[
48, 130, 2, 92, 2, 1, 0, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229,
234, 42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166, 15,
82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244, 165, 174,
58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149, 176, 92, 33,
156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116, 225, 208, 37, 178,
3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199, 229, 147, 13, 178, 196,
8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169, 120, 28, 24, 246, 110, 142,
105, 46, 98, 32, 17, 129, 145, 210, 65, 249, 227, 33, 2, 27, 185, 2, 1, 3, 2,
129, 129, 0, 145, 175, 237, 44, 70, 41, 73, 238, 156, 28, 48, 96, 28, 166, 196,
91, 157, 2, 196, 74, 82, 13, 57, 109, 25, 95, 140, 0, 233, 33, 190, 13, 211,
237, 24, 254, 138, 2, 229, 137, 193, 248, 110, 116, 38, 214, 240, 145, 161, 254,
73, 13, 100, 61, 27, 171, 72, 99, 202, 232, 22, 104, 93, 4, 19, 180, 157, 18,
228, 155, 153, 225, 246, 60, 235, 75, 187, 2, 2, 255, 115, 128, 28, 79, 78, 97,
199, 118, 59, 147, 156, 179, 156, 247, 100, 58, 113, 150, 54, 241, 47, 87, 167,
234, 125, 167, 98, 196, 209, 93, 194, 226, 158, 219, 159, 154, 222, 102, 166,
15, 188, 165, 153, 231, 45, 45, 87, 163, 2, 65, 0, 245, 208, 73, 78, 10, 130,
179, 5, 235, 9, 16, 72, 29, 166, 20, 75, 47, 253, 159, 231, 159, 246, 28, 211,
225, 230, 35, 170, 47, 140, 56, 61, 32, 74, 122, 254, 129, 73, 180, 45, 166,
58, 224, 146, 11, 234, 172, 64, 116, 30, 137, 139, 220, 147, 150, 131, 11, 55,
129, 90, 133, 8, 122, 127, 2, 65, 0, 227, 150, 44, 124, 104, 225, 79, 70, 158,
16, 112, 150, 111, 136, 234, 96, 200, 143, 71, 174, 206, 127, 230, 102, 203,
230, 57, 45, 248, 14, 100, 47, 61, 149, 156, 162, 111, 13, 196, 238, 122, 4,
180, 190, 214, 33, 165, 218, 44, 1, 53, 109, 245, 227, 241, 247, 44, 18, 17,
173, 216, 53, 157, 199, 2, 65, 0, 163, 224, 48, 222, 177, 172, 119, 89, 71,
91, 96, 48, 19, 196, 13, 135, 117, 83, 191, 239, 191, 249, 104, 141, 65, 68,
23, 198, 202, 93, 122, 211, 106, 220, 81, 255, 0, 219, 205, 115, 196, 39, 64,
97, 93, 71, 29, 128, 77, 105, 177, 7, 232, 98, 100, 87, 92, 207, 171, 145, 174,
5, 166, 255, 2, 65, 0, 151, 185, 114, 253, 155, 64, 223, 132, 105, 96, 75, 14,
245, 5, 241, 149, 219, 10, 47, 201, 222, 255, 238, 239, 50, 153, 123, 115, 250,
180, 66, 202, 41, 14, 104, 108, 74, 9, 45, 244, 81, 88, 120, 127, 57, 107, 195,
230, 200, 0, 206, 73, 78, 151, 246, 164, 200, 12, 11, 201, 58, 206, 105, 47, 2,
64, 32, 24, 155, 16, 110, 103, 52, 8, 123, 44, 18, 132, 54, 25, 9, 146, 15, 185,
184, 118, 107, 124, 196, 24, 212, 51, 32, 166, 34, 207, 174, 172, 186, 221, 2,
120, 97, 18, 178, 75, 8, 195, 250, 60, 252, 152, 100, 136, 131, 145, 157, 197,
50, 152, 241, 65, 93, 124, 235, 239, 241, 122, 91, 114]
let pub: Array<UInt8> = [
48, 129, 135, 2, 129, 129, 0, 218, 135, 227, 194, 105, 61, 238, 229, 234,
42, 72, 144, 42, 250, 38, 137, 107, 132, 38, 111, 123, 19, 214, 35, 166,
15, 82, 1, 93, 178, 157, 20, 189, 227, 165, 125, 207, 4, 88, 78, 162, 244,
165, 174, 58, 66, 104, 218, 114, 253, 109, 148, 22, 91, 169, 128, 236, 149,
176, 92, 33, 156, 139, 135, 246, 245, 97, 102, 202, 77, 105, 31, 122, 116,
225, 208, 37, 178, 3, 43, 37, 205, 18, 13, 100, 8, 174, 108, 7, 41, 199,
229, 147, 13, 178, 196, 8, 65, 106, 10, 183, 90, 244, 251, 220, 186, 169,
120, 28, 24, 246, 110, 142, 105, 46, 98, 32, 17, 129, 145, 210, 65, 249,
227, 33, 2, 27, 185, 2, 1, 3
]
var prik: RSAPrivateKey = RSA.setPrivateKey(pri)
var pubk: RSAPublicKey = RSA.setPublicKey(pub)
let datas: Array<UInt8> = "1.2.0:08:00:27:2c:88:08".toUtf8Array()
let ret: Array<UInt8> = Digest.digest(DigestType.sha224, datas)
let sigRet: Array<UInt8> = RSA.sign(DigestType.sha224, ret, prik)
RSA.verify(DigestType.sha224, ret, sigRet, pubk)
return 0
}
运行结果如下:
0
8 其他接口
/*
* 将 数组转为 bits 位的二维数组
*
* 参数 arr - 需要转换数组
* 参数 bits - 位
* 返回值 ArrayList<Array<UInt8>> - 转换后的数组
*/
public func arrayTo2Array(arr: Array<UInt8>, bits: Int64): ArrayList<Array<UInt8>>
/*
* cryptocj 错误处理
*/
public class CryptoException <: Exception {
/*
* 无参构造
*/
public init()
/*
* 构造函数
*
* 参数 messages - 报错信息
*/
public init(messages: String)
}
最后呢
很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。

针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。
- 《鸿蒙 (OpenHarmony)开发学习视频》
- 《鸿蒙生态应用开发V2.0白皮书》
- 《鸿蒙 (OpenHarmony)开发基础到实战手册》
- OpenHarmony北向、南向开发环境搭建
- 《鸿蒙开发基础》
- 《鸿蒙开发进阶》
- 《鸿蒙开发实战》

更多鸿蒙开发应用知识已更新gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md参考前往。
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿
更多推荐
所有评论(0)