我需要将Java中的简短代码转换为python 3。
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class DesEncrypterPdys {
private static DesEncrypterPdys desEncrypter;
private static Cipher ecipher;
private static Cipher dcipher;
private DesEncrypterPdys() throws DesEncrypterException{
try {
if(ecipher == null || dcipher == null){
String cryptoKey;
cryptoKey = "RAMPOLO S.O. Plokity Lopiokiujhygh ;)";
SecretKey k = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(cryptoKey.getBytes()));
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, k);
dcipher.init(Cipher.DECRYPT_MODE, k);
}
}catch (Exception e) {
throw new DesEncrypterException(e);
}
}
public static DesEncrypterPdys getInstance() throws DesEncrypterException{
if (desEncrypter == null || ecipher == null || dcipher == null) {
desEncrypter = new DesEncrypterPdys();
}
return desEncrypter;
}
public String encrypt(String str) throws DesEncrypterException{
try {
if(str == null) return null;
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
throw new DesEncrypterException(e);
} catch (IllegalBlockSizeException e) {
throw new DesEncrypterException(e);
}
catch (java.io.IOException e) {
throw new DesEncrypterException(e);
}
}
public String decrypt(String str) throws DesEncrypterException{
try {
if(str == null) return null;
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) {
throw new DesEncrypterException(e);
} catch (IllegalBlockSizeException e) {
throw new DesEncrypterException(e);
} catch (java.io.IOException e) {
throw new DesEncrypterException(e);
}
}
}
我试图使用类似这样的东西:
import pyDes as pds
import base64 as b64
cryptoKey = "RAMPOLO S.O. Plokity Lopiokiujhygh ;)";
data = b" " # here should be a data for decrypting
k = pds.des(secretKey, pds.ECB, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pds.PAD_PKCS5)
d = k.encrypt(data)
print("Encrypted: %r" % d)
print("Decrypted: %r" % k.decrypt(d))
print(b64.b64encode(d))
但不幸的是没有运气:/
我想收到的是一个简短的程序,用于加密和解码密码。不幸的是,我遇到了太长的cryptokey(?)问题。一旦我能够翻译一段类似的代码,但是幸运的是,旧案例与pydes文档中的案例相对应。现在不一样了...有人可以帮助我将此Java代码翻译为python吗?
运行代码时,返回的错误是:
>>> import pyDes as pds
>>> import base64 as b64
>>> cryptoKey = "RAMPOLO S.O. Plokity Lopiokiujhygh ;)"
>>> data = b"qwertyuiop" # Random gibberish
>>> k = pds.des(cryptoKey, pds.ECB, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pds.PAD_PKCS5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pyDes.py", line 400, in __init__
raise ValueError("Invalid DES key size. Key must be exactly 8 bytes long.")
ValueError: Invalid DES key size. Key must be exactly 8 bytes long.
如果您查看pyDES的Github文档,它指定对于DES加密,密钥应为8字节长,而您的则更长。您需要使用较短的密钥,然后它才能正常工作。
>>> k = pds.des('12345678', pds.ECB, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pds.PAD_PKCS5)
>>> k
<pyDes.des object at 0x7fc4ade1f9d0>
重要的旁注,从安全角度来看,您应该避免使用DES,因为它已完全破坏了当今的标准,即使是Wikipedia page(这不是加密知识的重要来源)也表明DES破解是可行的。由于每个块都以完全相同的方式加密,因此ECB模式也无法提供任何安全保证,因此它也被破坏了,请检查this。