日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

delphi7aes加密解密与java互转_跨语言(java vs python vs nodejs)的RSA加解密问题探讨

發(fā)布時間:2023/12/20 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 delphi7aes加密解密与java互转_跨语言(java vs python vs nodejs)的RSA加解密问题探讨 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多次被問到這樣的問題:

java服務端的rsa加密操作已經完成,返回一個16進制的字符串給python平臺,但是在python進行私鑰解密的時候發(fā)現行不通。。。。

前端python加密,后端用java解密,解不出來

還有諸如nodejs

從理論上來說,rsa加密的基礎都是一樣的,不存在一個語言加密,另一個語言解密不出來的情況。那出問題的只能是我們使用的方法不對。

java vs python

聲明:java的加密解密模塊需要更加精細的算法細節(jié)指定

java的加密方式

javax.crypto.Cipher,定義的獲取方式

static Cipher getInstance(String transformation)Returns a Cipher object that implements the specified transformation.static Cipher getInstance(String transformation, Provider provider)Returns a Cipher object that implements the specified transformation.static Cipher getInstance(String transformation, String provider)Returns a Cipher object that implements the specified transformation.

有兩個重要參數:

1. transformation定義為

A transformation is a string that describes the operation (or set of operations) to be performed on the given input, to produce some output. A transformation always includes the name of a cryptographic algorithm (e.g., AES), and may be followed by a feedback mode and padding scheme.A transformation is of the form:"algorithm/mode/padding" or"algorithm"(in the latter case, provider-specific default values for the mode and padding scheme are used). For example, the following is a valid transformation: Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");

transformation有以下幾種:

Every implementation of the Java platform is required to support the following standard Cipher transformations with the keysizes in parentheses:AES/CBC/NoPadding (128)AES/CBC/PKCS5Padding (128)AES/ECB/NoPadding (128)AES/ECB/PKCS5Padding (128)DES/CBC/NoPadding (56)DES/CBC/PKCS5Padding (56)DES/ECB/NoPadding (56)DES/ECB/PKCS5Padding (56)DESede/CBC/NoPadding (168)DESede/CBC/PKCS5Padding (168)DESede/ECB/NoPadding (168)DESede/ECB/PKCS5Padding (168)RSA/ECB/PKCS1Padding (1024, 2048)RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)These transformations are described in the Cipher section of the Java Cryptography Architecture Standard Algorithm Name Documentation. Consult the release documentation for your implementation to see if any other transformations are supported.

2.provider

可以通過Security.getProviders()查看

java.security.Provider [] providers=Security.getProviders(); for(int i=0;i

具體的provider如下:

SUNSunRsaSignSunECSunJSSESunJCESunJGSSSunSASLXMLDSigSunPCSCSunMSCAPI

python的加密方式需要到具體的代碼里面了,如

from crypto.PublicKey import RSAfrom crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5# from Crypto.Signature import PKCS1_v1_5 as Signature_pkcs1_v1_5def rsaEncrypt(message): key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYLCumWz6MGHmAMLIaPt3SItIhMYHuyLn48muQz2xKj9PVqETGfjq/GTxHE3wfvGCEs/JXY1rV4uysUuAS/xwZuyJ9j+sB599lzmpxdhIWu/jGMR0h86nnpNUcssYwR3Bww3oU5+dYEtGpfOytMyh3eJeUZiNNBXqH+IaSYfU3hwIDAQAB' key1=base64.b64decode(key) rsaKey=RSA.importKey(key1) cipher=Cipher_pkcs1_v1_5.new(rsaKey) temp=cipher.encrypt(message) return binascii.b2a_hex(temp)if __name__ == '__main__': rsaEncrypt(13950346593)

進入encypt方法中:

def encrypt(self, message): """Produce the PKCS#1 v1.5 encryption of a message. This function is named ``RSAES-PKCS1-V1_5-ENCRYPT``, and it is specified in `section 7.2.1 of RFC8017 `_. :param message: The message to encrypt, also known as plaintext. It can be of variable length, but not longer than the RSA modulus (in bytes) minus 11. :type message: bytes/bytearray/memoryview :Returns: A byte string, the ciphertext in which the message is encrypted. It is as long as the RSA modulus (in bytes). :Raises ValueError: If the RSA key length is not sufficiently long to deal with the given message. """

發(fā)現其支持的是

PKCS#1 v1.5 encryption

對應java的模式是:

RSA/ECB/PKCS1Padding (1024, 2048)

IvParameterSpec

This class specifies an initialization vector (IV). Examples which use IVs are ciphers in feedback mode, e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation.

java和nodejs

用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具類加密解密正常。但是用node加密玩的java解密不了。原因:node默認的是

DEFAULT_ENCRYPTION_SCHEME = 'pkcs1_oaep' 而java中默認的是pkcs1。

node-rsa源碼:https://github.com/rzcoder/node-rsa/blob/ea5c17d9351c857c0594d7921c596ff5636882f1/src/NodeRSA.js

var DEFAULT_ENCRYPTION_SCHEME = 'pkcs1_oaep';

node-rsa官方文檔:https://www.npmjs.com/package/node-rsa

Options

You can specify some options by second/third constructor argument, or over key.setOptions()method.

environment — working environment (default autodetect):'browser' — will run pure js implementation of RSA algorithms.'node' for nodejs >= 0.10.x or io.js >= 1.x — provide some native methods like sign/verify and encrypt/decrypt.encryptionScheme — padding scheme for encrypt/decrypt. Can be 'pkcs1_oaep' or 'pkcs1'. Default 'pkcs1_oaep'.signingScheme — scheme used for signing and verifying. Can be 'pkcs1' or 'pss' or 'scheme-hash' format string (eg 'pss-sha1'). Default 'pkcs1-sha256', or, if chosen pss: 'pss-sha1'.

Notice: This lib supporting next hash algorithms: 'md5', 'ripemd160', 'sha1', 'sha256', 'sha512' in browser and node environment and additional 'md4', 'sha', 'sha224', 'sha384' in node only.

所以要保持一致:

import NodeRSA from 'node-rsa';const rsa_encrypt = (data) => { let key = new NodeRSA('-----BEGIN PUBLIC KEY-----' + 'MIGfMA0。。。。。。。AQAB' + '-----END PUBLIC KEY-----'); // key.generateKeyPair(1024); key.setOptions({encryptionScheme: 'pkcs1'}) let encryptKey = key.encrypt(data, 'base64') return encryptKey;}

后臺:

public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception { byte[] keyBytes = Base64Utils.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段解密 while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; }

參考文獻:

【1】https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html#getInstance(java.lang.String)

【2】https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher

【3】https://docs.oracle.com/javase/7/docs/api/javax/crypto/spec/IvParameterSpec.html

【4】https://blog.csdn.net/mshootingstar/article/details/56496719

總結

以上是生活随笔為你收集整理的delphi7aes加密解密与java互转_跨语言(java vs python vs nodejs)的RSA加解密问题探讨的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩av三区 | 欧美 日韩 国产 中文 | 亚洲av无码国产精品久久 | 亚洲四区在线 | 牛牛精品一区 | 免费在线观看日韩av | 丁香七月婷婷 | 超碰91在线观看 | 久久综合久色欧美综合狠狠 | 免费精品一区 | 骚虎视频最新网址 | 久久久精品一区二区三区 | 91av一区二区三区 | 日本一区二区三区视频在线 | 污网站免费| 一级特黄色 | 久久久丁香 | 国产成人无码久久久精品天美传媒 | 国产精品国产三级国产专区51区 | 国产美女福利视频 | 香港三日本三级少妇66 | 亚洲 小说区 图片区 | 亚洲美女色 | 免费国产羞羞网站视频 | www日本在线| 欧美日韩高清在线观看 | 爽爽影院在线免费观看 | 中文字幕一区二区三区夫目前犯 | 九九九色 | 久久亚洲av无码精品色午夜麻豆 | 国产精品久久久久久久一区探花 | 91日韩欧美 | 情侣作爱视频网站 | av中文字幕在线播放 | xxxx日本黄色| 一区二区三区中文视频 | 催眠调教艳妇成肉便小说 | 国产美女精品一区二区三区 | 国产113页| 六月婷婷中文字幕 | 天天艹av| 精人妻无码一区二区三区 | 国产激情视频在线观看 | 国产传媒一区二区三区 | 天天影视综合 | 欧美三级网站在线观看 | 色视频国产 | caobi视频| 无码人妻丰满熟妇精品区 | 麻豆人妻少妇精品无码专区 | 日日爽天天| 天堂久久精品忘忧草 | 苏晴忘穿内裤坐公交车被揉到视频 | jvid在线 | 精品久久久久久无码人妻 | 久草影视在线观看 | 老司机福利av | 特级西西人体444www | 怡春院一区二区 | 黑森林av导航 | 日本japanese极品少妇 | 国产美女免费视频 | 精品无码人妻一区二区免费蜜桃 | 国产精品黄色网 | 精品国产综合区久久久久久 | 成人性生交大免费看 | 免费日本在线 | 亚洲欧美自拍另类 | www.黄色av| 激情四射网站 | 亚洲欧洲日韩在线 | 日韩视频一区 | 欧美无砖区 | 香蕉视频在线观看免费 | 日韩精品视频免费看 | 欧美色图校园春色 | 熟女高潮一区二区三区视频 | 亚洲精品香蕉 | 老公吃小头头视频免费观看 | 欧美男人天堂网 | 奇米影视久久久 | 成人av电影在线播放 | 97在线视频免费观看 | 国产黄a| 精品国偷自产国产一区 | 国外成人性视频免费 | 双性人bbww欧美双性 | va在线播放| www.一区二区 | 黄色裸体网站 | 游戏涩涩免费网站 | 特级黄色一级片 | 99人妻碰碰碰久久久久禁片 | 三级无遮挡| www国产精品内射熟女 | 久久色在线观看 | 成人爽a毛片一区二区免费 日本高清免费看 | 中文字幕无码av波多野吉衣 | 欧美成在线视频 |