1

我正在尝试使用 Jython 将 python 类转换为 Java 字节码(在 mac osx lion 上)

./jython -m compileall /Users/owengerig/Downloads/Code\Downloads/cryptPYTHON.py

但得到这个错误,它没有表明什么是错的

列出 /Users/owengerig/Downloads/Code Downloads/cryptPYTHON.py ... 无法列出 /Users/owengerig/Downloads/Code Downloads/cryptPYTHON.py

我的 python 类是如何设置的(以这篇文章为例):

from Crypto.Cipher import AES
import base64
import os

class Crypticle(CryptInterface):
    """Authenticated encryption class

     * @param string    $key      base64-encoded encryption key
     * @param integer   $key_len  length of raw key in bits

    Encryption algorithm: AES-CBC
    Signing algorithm: HMAC-SHA256
    """

    AES_BLOCK_SIZE = 16

    @JAVA
    def __init__(self, key_string, key_size=192):
        assert not key_size % 8
        self.key = self.extract_key(key_string, key_size)
        self.key_size = key_size

    @classmethod
    def generate_key_string(cls, key_size=192):
        key = os.urandom(key_size / 8)
        return base64.urlsafe_b64encode(str(key))

    @classmethod
    def extract_key(cls, key_string, key_size):
        key = base64.urlsafe_b64decode(str(key_string))
        assert len(key) == key_size / 8, "invalid key"
        return key

    @JAVA(String, String)    
    def encrypt(self, data):
        """encrypt data with AES-CBC"""
        aes_key = self.key
        pad = self.AES_BLOCK_SIZE - len(data) % self.AES_BLOCK_SIZE
        data = data + pad * chr(pad)
        iv_bytes = os.urandom(self.AES_BLOCK_SIZE)
        cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
        data = iv_bytes + cypher.encrypt(data)
        data_str = base64.urlsafe_b64encode(str(data))
        return data_str

    @JAVA(String, String)  
    def decrypt(self, data_str):
        """decrypt data with AES-CBC"""
        aes_key = self.key
        data = base64.urlsafe_b64decode(data_str)
        iv_bytes = data[:self.AES_BLOCK_SIZE]
        data = data[self.AES_BLOCK_SIZE:]
        cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
        data = cypher.decrypt(data)
        return data[:-ord(data[-1])]

还尝试了此代码(根据下面的评论),但出现相同的错误:

class Employee(Object):
   def __init__(self):
      self.first = "Josh"
      self.last  = "Juneau"
      self.id = "myempid"

   def getEmployeeFirst(self):
      return self.first

   def getEmployeeLast(self):
      return self.last

   def getEmployeeId(self):
      return self.id
4

1 回答 1

3

-m compileall 采用目录,而不是文件名。因此,您需要执行以下操作:

./jython -m compileall /Users/owengerig/Downloads/Code\ Downloads/

长解释

如果打开jython 目录/Lib/compileall.py

try:
    names = os.listdir(dir)
except os.error:
    print "Can't list", dir
    names = []

如果没有将目录作为参数传递,os.listdir() 会引发错误。由于这是用于编译命令行参数的函数,并且 main() 函数不检查参数是否为目录,所以这将失败。

        for dir in args:
            if not compile_dir(dir, maxlevels, ddir,
                               force, rx, quiet):
                success = 0

/长解释

于 2012-06-18T21:54:59.613 回答