摘要:加解密中必須考慮到的密鑰長(zhǎng)度明文長(zhǎng)度和密文長(zhǎng)度問題。而只要用到,那么就要占用實(shí)際的明文長(zhǎng)度,于是實(shí)際明文長(zhǎng)度需要減去字節(jié)長(zhǎng)度。所以如果要對(duì)任意長(zhǎng)度的數(shù)據(jù)進(jìn)行加密,就需要將數(shù)據(jù)分段后進(jìn)行逐一加密,并將結(jié)果進(jìn)行拼接。
RSA加解密中必須考慮到的密鑰長(zhǎng)度、明文長(zhǎng)度和密文長(zhǎng)度問題。明文長(zhǎng)度需要小于密鑰長(zhǎng)度,而密文長(zhǎng)度則等于密鑰長(zhǎng)度。因此當(dāng)加密內(nèi)容長(zhǎng)度大于密鑰長(zhǎng)度時(shí),有效的RSA加解密就需要對(duì)內(nèi)容進(jìn)行分段。
這是因?yàn)?,RSA算法本身要求加密內(nèi)容也就是明文長(zhǎng)度m必須0 這樣,對(duì)于1024長(zhǎng)度的密鑰。128字節(jié)(1024bits)-減去11字節(jié)正好是117字節(jié),但對(duì)于RSA加密來講,padding也是參與加密的,所以,依然按照1024bits去理解,但實(shí)際的明文只有117字節(jié)了。 所以如果要對(duì)任意長(zhǎng)度的數(shù)據(jù)進(jìn)行加密,就需要將數(shù)據(jù)分段后進(jìn)行逐一加密,并將結(jié)果進(jìn)行拼接。同樣,解碼也需要分段解碼,并將結(jié)果進(jìn)行拼接。 github地址: https://github.com/liamylian/...if (! function_exists("url_safe_base64_encode")) {
function url_safe_base64_encode ($data) {
return str_replace(array("+","/", "="),array("-","_", ""), base64_encode($data));
}
}
if (! function_exists("url_safe_base64_decode")) {
function url_safe_base64_decode ($data) {
$base_64 = str_replace(array("-","_"),array("+","/"), $data);
return base64_decode($base_64);
}
}
class XRsa
{
const CHAR_SET = "UTF-8";
const BASE_64_FORMAT = "UrlSafeNoPadding";
const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;
const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256;
protected $public_key;
protected $private_key;
protected $key_len;
public function __construct($pub_key, $pri_key = null)
{
$this->public_key = $pub_key;
$this->private_key = $pri_key;
$pub_id = openssl_get_publickey($this->public_key);
$this->key_len = openssl_pkey_get_details($pub_id)["bits"];
}
/*
* 創(chuàng)建密鑰對(duì)
*/
public static function createKeys($key_size = 2048)
{
$config = array(
"private_key_bits" => $key_size,
"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $private_key);
$public_key_detail = openssl_pkey_get_details($res);
$public_key = $public_key_detail["key"];
return [
"public_key" => $public_key,
"private_key" => $private_key,
];
}
/*
* 公鑰加密
*/
public function publicEncrypt($data)
{
$encrypted = "";
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len);
foreach ($parts as $part) {
$encrypted_temp = "";
openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
$encrypted .= $encrypted_temp;
}
return url_safe_base64_encode($encrypted);
}
/*
* 私鑰解密
*/
public function privateDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len);
foreach ($parts as $part) {
$decrypted_temp = "";
openssl_private_decrypt($part, $decrypted_temp,$this->private_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
}
/*
* 私鑰加密
*/
public function privateEncrypt($data)
{
$encrypted = "";
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len);
foreach ($parts as $part) {
$encrypted_temp = "";
openssl_private_encrypt($part, $encrypted_temp, $this->private_key);
$encrypted .= $encrypted_temp;
}
return url_safe_base64_encode($encrypted);
}
/*
* 公鑰解密
*/
public function publicDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len);
foreach ($parts as $part) {
$decrypted_temp = "";
openssl_public_decrypt($part, $decrypted_temp,$this->public_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
}
/*
* 數(shù)據(jù)加簽
*/
public function sign($data)
{
openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);
return url_safe_base64_encode($sign);
}
/*
* 數(shù)據(jù)簽名驗(yàn)證
*/
public function verify($data, $sign)
{
$pub_id = openssl_get_publickey($this->public_key);
$res = openssl_verify($data, url_safe_base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);
return $res;
}
}
Go實(shí)現(xiàn)
package xrsa
import (
"encoding/pem"
"encoding/base64"
"crypto/x509"
"crypto/rsa"
"crypto/rand"
"errors"
"crypto"
"io"
"bytes"
"encoding/asn1"
)
const (
CHAR_SET = "UTF-8"
BASE_64_FORMAT = "UrlSafeNoPadding"
RSA_ALGORITHM_KEY_TYPE = "PKCS8"
RSA_ALGORITHM_SIGN = crypto.SHA256
)
type XRsa struct {
publicKey *rsa.PublicKey
privateKey *rsa.PrivateKey
}
// 生成密鑰對(duì)
func CreateKeys(publicKeyWriter, privateKeyWriter io.Writer, keyLength int) error {
// 生成私鑰文件
privateKey, err := rsa.GenerateKey(rand.Reader, keyLength)
if err != nil {
return err
}
derStream := MarshalPKCS8PrivateKey(privateKey)
block := &pem.Block{
Type: "PRIVATE KEY",
Bytes: derStream,
}
err = pem.Encode(privateKeyWriter, block)
if err != nil {
return err
}
// 生成公鑰文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "PUBLIC KEY",
Bytes: derPkix,
}
err = pem.Encode(publicKeyWriter, block)
if err != nil {
return err
}
return nil
}
func NewXRsa(publicKey []byte, privateKey []byte) (*XRsa, error) {
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pub := pubInterface.(*rsa.PublicKey)
block, _ = pem.Decode(privateKey)
if block == nil {
return nil, errors.New("private key error!")
}
priv, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
pri, ok := priv.(*rsa.PrivateKey)
if ok {
return &XRsa {
publicKey: pub,
privateKey: pri,
}, nil
} else {
return nil, errors.New("private key not supported")
}
}
// 公鑰加密
func (r *XRsa) PublicEncrypt(data string) (string, error) {
partLen := r.publicKey.N.BitLen() / 8 - 11
chunks := split([]byte(data), partLen)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
bytes, err := rsa.EncryptPKCS1v15(rand.Reader, r.publicKey, chunk)
if err != nil {
return "", err
}
buffer.Write(bytes)
}
return base64.RawURLEncoding.EncodeToString(buffer.Bytes()), nil
}
// 私鑰解密
func (r *XRsa) PrivateDecrypt(encrypted string) (string, error) {
partLen := r.publicKey.N.BitLen() / 8
raw, err := base64.RawURLEncoding.DecodeString(encrypted)
chunks := split([]byte(raw), partLen)
buffer := bytes.NewBufferString("")
for _, chunk := range chunks {
decrypted, err := rsa.DecryptPKCS1v15(rand.Reader, r.privateKey, chunk)
if err != nil {
return "", err
}
buffer.Write(decrypted)
}
return buffer.String(), err
}
// 數(shù)據(jù)加簽
func (r *XRsa) Sign(data string) (string, error) {
h := RSA_ALGORITHM_SIGN.New()
h.Write([]byte(data))
hashed := h.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, r.privateKey, RSA_ALGORITHM_SIGN, hashed)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(sign), err
}
// 數(shù)據(jù)驗(yàn)簽
func (r *XRsa) Verify(data string, sign string) error {
h := RSA_ALGORITHM_SIGN.New()
h.Write([]byte(data))
hashed := h.Sum(nil)
decodedSign, err := base64.RawURLEncoding.DecodeString(sign)
if err != nil {
return err
}
return rsa.VerifyPKCS1v15(r.publicKey, RSA_ALGORITHM_SIGN, hashed, decodedSign)
}
func MarshalPKCS8PrivateKey(key *rsa.PrivateKey) []byte {
info := struct {
Version int
PrivateKeyAlgorithm []asn1.ObjectIdentifier
PrivateKey []byte
}{}
info.Version = 0
info.PrivateKeyAlgorithm = make([]asn1.ObjectIdentifier, 1)
info.PrivateKeyAlgorithm[0] = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
info.PrivateKey = x509.MarshalPKCS1PrivateKey(key)
k, _ := asn1.Marshal(info)
return k
}
func split(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
Java實(shí)現(xiàn)
package com.inspii;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class XRsa {
public static final String CHARSET = "UTF-8";
public static final String RSA_ALGORITHM = "RSA";
public static final String RSA_ALGORITHM_SIGN = "SHA256WithRSA";
private RSAPublicKey publicKey;
private RSAPrivateKey privateKey;
public XRsa(String publicKey, String privateKey)
{
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
//通過X509編碼的Key指令獲得公鑰對(duì)象
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
this.publicKey = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
//通過PKCS#8編碼的Key指令獲得私鑰對(duì)象
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
this.privateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
} catch (Exception e) {
throw new RuntimeException("不支持的密鑰", e);
}
}
public static Map
參考
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/25874.html
摘要:加解密中必須考慮到的密鑰長(zhǎng)度明文長(zhǎng)度和密文長(zhǎng)度問題。而只要用到,那么就要占用實(shí)際的明文長(zhǎng)度,于是實(shí)際明文長(zhǎng)度需要減去字節(jié)長(zhǎng)度。所以如果要對(duì)任意長(zhǎng)度的數(shù)據(jù)進(jìn)行加密,就需要將數(shù)據(jù)分段后進(jìn)行逐一加密,并將結(jié)果進(jìn)行拼接。 RSA加解密中必須考慮到的密鑰長(zhǎng)度、明文長(zhǎng)度和密文長(zhǎng)度問題。明文長(zhǎng)度需要小于密鑰長(zhǎng)度,而密文長(zhǎng)度則等于密鑰長(zhǎng)度。因此當(dāng)加密內(nèi)容長(zhǎng)度大于密鑰長(zhǎng)度時(shí),有效的RSA加解密就需要對(duì)內(nèi)...
摘要:我們只考慮能解密就行明文公鑰加密,私鑰解密密文明文公鑰私鑰使用生成格式的公鑰私鑰文件。直接使用該密鑰需要轉(zhuǎn)換為格式公鑰私鑰,密鑰字符串不需要。庫安裝會(huì)有環(huán)境問題,直接安裝成功,安裝失敗。 記錄一次項(xiàng)目使用RSA加解密 項(xiàng)目使用Java和Python在開發(fā),RSA加密解密互通代碼: Python代碼 # -*- coding: utf-8 -*- RSA加解密 import base...
摘要:安全總是很重要的,各個(gè)語言對(duì)于通用的加密算法都會(huì)有實(shí)現(xiàn)。對(duì)于和加密算法本身,請(qǐng)查閱相關(guān)資料在中,很多功能經(jīng)常是一個(gè)函數(shù)解決而中的卻不是。該文討論加密解密。一概要這是一個(gè)非對(duì)稱加密算法,一般通過公鑰加密,私鑰解密。 安全總是很重要的,各個(gè)語言對(duì)于通用的加密算法都會(huì)有實(shí)現(xiàn)。前段時(shí)間,用Go實(shí)現(xiàn)了RSA和DES的加密解密,在這分享一下。(對(duì)于RSA和DES加密算法本身,請(qǐng)查閱相關(guān)資料) 在P...
摘要:第二次訪問接口,客戶端需使用返回公鑰加密訪問參數(shù),之后將加密后的參數(shù)和一起發(fā)送給服務(wù)端進(jìn)行驗(yàn)證。關(guān)于函數(shù)的詳細(xì)介紹可以看上一位朋友的博客,鏈接在此簡(jiǎn)單總結(jié)用加密公鑰傳遞給用公鑰加密信息傳遞給解密 RSA非對(duì)稱加密,在網(wǎng)絡(luò)鑒權(quán)中使用很多。本人也是在PHP與C++交互鑒權(quán)時(shí)出現(xiàn)問題,在解決問題過程中發(fā)現(xiàn),網(wǎng)上對(duì)相關(guān)問題的描述很少,所在這里記錄下,希望對(duì)遇到相似問題的朋友有所幫助。 問題情景...
閱讀 3928·2021-11-24 09:38
閱讀 3110·2021-11-17 09:33
閱讀 3880·2021-11-10 11:48
閱讀 1248·2021-10-14 09:48
閱讀 3139·2019-08-30 13:14
閱讀 2559·2019-08-29 18:37
閱讀 3404·2019-08-29 12:38
閱讀 1423·2019-08-29 12:30