Home > other >  Java AES/CBC/PKCS7Padding encryption algorithm, written in python?
Java AES/CBC/PKCS7Padding encryption algorithm, written in python?

Time:09-18

The Java encryption, try writing python to write, I do not know such right, is there are familiar with the two bosses under the guidance of the

Java (the original is two files and all sorts of chaos, I rewrote, might not be the format) :
 import Java. Security. MessageDigest; 
Import the Java. Security. Spec. AlgorithmParameterSpec;
The import javax.mail. Crypto. Cipher;
The import javax.mail. Crypto. Spec. IvParameterSpec;
The import javax.mail. Crypto. Spec. SecretKeySpec;
The import android. Util. Base64;

Public final class AESCipher {
Public static void main (String [] args) {
String raw="texttext";
String cryptedStr=AESCipher. Encrypt (raw);
System. The out. Println (cryptedStr);
}

Public static Object key () {
The MessageDigest instance=MessageDigest. GetInstance (" SHA - 256 ");
The instance. The update (" AN2jH1M1FD9. UDN2 ". GetBytes (" utf-8 "));
The Object key=new byte [32];
System. Arraycopy (instance. Digest (), 0, key, 0, 32).
The return key.
}

Public static String encrypt (String raw) {
The Object key=key ();
Byte []=new iv byte [] {(byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0 (byte), (byte) 0, 0} (byte);
SecretKeySpec keyspec=new SecretKeySpec ((byte []) key, AES, "");
AlgorithmParameterSpec ivspec=new IvParameterSpec (iv);
Cipher Cipher=Cipher. GetInstance (" AES/CBC/PKCS7Padding ");
Cipher. The init (cipher DECRYPT_MODE, keyspec, ivspec);
Encrypted byte []=cipher. DoFinal (raw. GetBytes (" utf-8 "));
String cryptedStr=new String (Base64 encode (encrypted, 0), "utf-8");
Return cryptedStr;

}
}


Python, I do not know such right
 import hashlib 
The from Crypto. Cipher import AES
The import base64

The class AESCipher:
Def __init__ (self) :
Self. Str_key='AN2jH1M1FD9. UDN2'
Self. Iv="\ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" # 16 characters, used to fill the missing content, can be a fixed value can also be a random string, specific watching demand,

Def get_key (self) :
The instance=bytes (self str_key, encoding="utf-8")
Sha256=hashlib. Sha256 ()
Sha256. Update (instance)
Key=sha256. Digest () : [32]
The return key

Def __pad (self, text) :
"" "filling way, encrypted content must be in multiples of 16 bytes, if less than the use of the self. Iv to fill "" "
Text_length=len (text)
Amount_to_pad=AES. Block_size - (text_length % AES. The block_size)
If amount_to_pad==0:
Amount_to_pad=AES. Block_size
Pad=CRH (amount_to_pad)
Return the text + pad * amount_to_pad

Def encrypt (self, raw) :
Key=self. Get_key ()
Raw=self. __pad (raw) # note here will definitely turn through the utf-8 code first become a byte string to call padding algorithm

Cipher=AES. New (key, AES. MODE_CBC, self. Iv, segment_size=128) # note here segment_size=128
Encrypted=cipher. Encrypt (raw)
CryptedStr=base64. B64encode (encrypted)
Return cryptedStr

CodePudding user response:

  • Related