|
@@ -23,11 +23,28 @@ func EncryptWithRSA(publicKey *rsa.PublicKey, data []byte) ([]byte, error) {
|
|
|
// DecryptWithRSA 使用 RSA 私钥解密数据
|
|
|
func DecryptWithRSA(privateKey *rsa.PrivateKey, encrypted string) ([]byte, error) {
|
|
|
encKey, _ := base64.StdEncoding.DecodeString(encrypted)
|
|
|
- hash, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encKey)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ blockSize := privateKey.Size()
|
|
|
+ maxChunkSize := blockSize
|
|
|
+ var plaintext []byte
|
|
|
+ for len(encKey) > 0 {
|
|
|
+ chunkSize := maxChunkSize
|
|
|
+ if len(encKey) < chunkSize {
|
|
|
+ chunkSize = len(encKey)
|
|
|
+ }
|
|
|
+
|
|
|
+ chunk := encKey[:chunkSize]
|
|
|
+ encKey = encKey[chunkSize:]
|
|
|
+ decryptedChunk, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, chunk)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ plaintext = append(plaintext, decryptedChunk...)
|
|
|
}
|
|
|
- return hash, nil
|
|
|
+ //hash, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encKey)
|
|
|
+ //if err != nil {
|
|
|
+ // return nil, err
|
|
|
+ //}
|
|
|
+ return plaintext, nil
|
|
|
}
|
|
|
|
|
|
// ParsePrivateKeyFromPEM 解析RSA公钥
|