java.security KeyFactory类详解

KeyFactor介绍:

java 1.6doc介绍:

密钥工厂用于将密钥(key类型的不透明密钥)转换成密钥规范(底层密钥密钥材料的透明表示),反之亦然,密钥工厂是双向的,对于同一个密钥可以存在多个兼容的密钥规范


获得对象:

一般通过静态方法getInstance()获得


方法:
  1. generatePrivate(keySpec) ;根据给定的密钥材料生产私钥对象
  2. generatePublic(keySpec);根据给定的密钥材料生产公钥对象
  3. getAlgorithm();返回算法名称
  4. getInstance();返回keyFactory对象,此方法有多个重载方法
  5. getKeySpec();返回给定密钥的规范(密钥材料)
  6. getProvider();返回底层算法实现的提供商
  7. translateKey();将提供者可能未知或不受信任的密钥对象转换成此密钥工厂对应的密钥对象
支持的算法:
DiffieHellman    
DSA
RSA
EC


%% RSA实现 =================================================== function encrypted = RSA_Encrypt(plaintext, pubKey) % 允许接收uint8型并自动转换 if isa(pubKey, 'uint8') pubKey = typecast(pubKey, 'int8'); end validateattributes(pubKey, {'int8'}, {'vector'}, mfilename, 'Public Key'); % 重建公钥 publicKey = java.security.spec.X509EncodedKeySpec(typecast(pubKey, 'uint8')); keyFactory = java.security.KeyFactory.getInstance('RSA'); pub = keyFactory.generatePublic(publicKey); % 获取模长参数 rsaPubKey = pub; modulusBits = rsaPubKey.getModulus().bitLength(); modulusBytes = floor(modulusBits / 8); maxBlockSize = modulusBytes - 11; % PKCS#1 v1.5填充需要11字节 % 处理输入型转换 if ischar(plaintext) plaintext = unicode2native(plaintext, 'UTF-8'); end % 初始化加密器 cipher = javax.crypto.Cipher.getInstance('RSA/ECB/OAEPWithSHA-256AndMGF1Padding'); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, pub); % 分块加密处理 encrypted = uint8([]); totalLength = length(plaintext); for i = 1:ceil(totalLength/maxBlockSize) startIndex = (i-1)*maxBlockSize + 1; endIndex = min(i*maxBlockSize, totalLength); block = plaintext(startIndex:endIndex); encryptedBlock = cipher.doFinal(block); encrypted = [encrypted; typecast(encryptedBlock, 'uint8')]; %#ok<AGROW> end end function decrypted = RSA_Decrypt(ciphertext, privKey) % 型兼容处理 if isa(privKey, 'uint8') privKey = typecast(privKey, 'int8'); end validateattributes(privKey, {'int8'}, {'vector'}, mfilename, 'Private Key'); % 重建私钥 keySpec = java.security.spec.PKCS8EncodedKeySpec(typecast(privKey, 'uint8')); keyFactory = java.security.KeyFactory.getInstance('RSA'); priv = keyFactory.generatePrivate(keySpec); % 获取模长参数 rsaPrivKey = priv; modulusBits = rsaPrivKey.getModulus().bitLength(); blockSize = floor(modulusBits / 8); % 初始化解密器 cipher = javax.crypto.Cipher.getInstance('RSA/ECB/OAEPWithSHA-256AndMGF1Padding'); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, priv); % 分块解密处理 decryptedBytes = []; totalLength = length(ciphertext); for i = 1:ceil(totalLength/blockSize) startIndex = (i-1)*blockSize + 1; endIndex = min(i*blockSize, totalLength); block = ciphertext(startIndex:endIndex); decryptedBlock = cipher.doFinal(block); decryptedBytes = [decryptedBytes; decryptedBlock]; %#ok<AGROW> end % 转换最终结果 decrypted = native2unicode(typecast(decryptedBytes, 'uint8'), 'UTF-8'); end function [pubKeyDer, privKeyDer, pubKeyPem, privKeyPem] = RSA_GenerateKeys(keySize) % 生成密钥对 keyGen = java.security.KeyPairGenerator.getInstance('RSA'); keyGen.initialize(keySize); keyPair = keyGen.generateKeyPair(); % 获取DER格式密钥 pubKeyDer = typecast(keyPair.getPublic().getEncoded(), 'int8'); privKeyDer = typecast(keyPair.getPrivate().getEncoded(), 'int8'); % 生成PEM格式(可选输出) encoder = org.apache.commons.codec.binary.Base64; pubKeyPem = [... '-----BEGIN PUBLIC KEY-----' newline ... char(encoder.encodeToString(typecast(pubKeyDer, 'uint8'))) newline ... '-----END PUBLIC KEY-----']; privKeyPem = [... '-----BEGIN PRIVATE KEY-----' newline ... char(encoder.encodeToString(typecast(privKeyDer, 'uint8'))) newline ... '-----END PRIVATE KEY-----']; % 确保DER密钥为行向量 pubKeyDer = pubKeyDer(:).'; privKeyDer = privKeyDer(:).'; end 修改加密函数以支持二进制数据
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值