【发布时间】:2015-08-14 16:46:21
【问题描述】:
这是在python中加密数据的代码
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from base64 import b64decode
import base64
mode = AES.MODE_CBC
key_bytes="HTj9bAAAMg9XxK6uLs4JGg==" # random 128 bit key generated
iv_bytes = "SECRETKEY"
cipher = AES.new(key_bytes, mode, iv_bytes)
def pad(text):
byteNum = len(text)
packingLength = 8 - byteNum % 8
appendage = chr(packingLength) * packingLength
data=text + appendage
return data
plain_text="some text to encrypt"
data = pad(plain_text)
encrypted_bytes = cipher.encrypt(data)
encrypted_string = base64.urlsafe_b64encode(encrypted_bytes)
encrytid = open("encryptid.txt",'w') #writting encrypted data for ref
encrytid.write(encrypted_string)
encrytid.close()
keys = b64decode('HTj9bAAAMg9XxK6uLs4JGg==')
key = (open('public.pem', 'rb').read()) #reading public.pem data
rsakey = RSA.importKey(key)
rsakey = PKCS1_OAEP.new(rsakey)
encrypted = rsakey.encrypt(keys)
#print ("enc: ", encrypted)
encrypt_aes = base64.b64encode(encrypted)
这是用于解密上述输出的java代码:
当我们尝试使用 java 解密数据时,出现以下错误:
错误
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2121)
有人可以提出解决问题的可能解决方案吗...
【问题讨论】:
-
随机想法:在你的 python 代码中你说 AES 但 java 异常是 RSA?
-
您的 java 代码不完整。如果没有完整的代码,我们无法理解您的错误。
-
对不起,我用 RSA 代码更新代码不好..
-
这还不完整。 python 代码导入 RSA,但从不使用它。 Java 代码没有 public static void main 函数。
-
您的代码不完整,但我认为(根据错误文本)您在解密中使用了错误的算法/模式/填充
标签: java python encryption cryptography padding