aes

AES 256 bit encryption - java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

喜欢而已 提交于 2019-12-03 04:03:35
Below is my encryption logic. Although my IV is 16bytes long, I still get an error with invalid IV length. Would appreciate any help @Override public String encrypt(String dataToEncrypt, String IV) throws Exception{ if(encryptionKey.length() < 10){ encryptionKey = generateEncryptionKey().toString(); } System.out.println("number of IV bytes is "+IV.length()+" "+IV); Cipher cipher = Cipher.getInstance(encrpytionAlgo); SecretKey key = new SecretKeySpec(encryptionKey.getBytes(Charset.forName("UTF-8")), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes(Charset.forName(

AES/CBC/PKCS5Padding issue

 ̄綄美尐妖づ 提交于 2019-12-03 04:00:28
I am trying to encrypt and decrypt some simple text. But for some reason I am getting a strange error: javax.crypto.BadPaddingException . Why would JCE generates bytes that are not properly padded? I have the following code: import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import java.security.SecureRandom; public class SimplestTest { public static void main(String[] args) throws Exception { SecureRandom rnd = new SecureRandom(); String text = "Hello, my dear! ... " + System.getProperty("user.home"); byte[]

Java AES cipher text size

倖福魔咒の 提交于 2019-12-03 04:00:23
I'm using a very standard way of Java AES encryption / decryption. byte[] key = hexStringToByteArray("C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"); byte[] message = hexStringToByteArray("01A0A1A2A3A4A5A6A703020100060001"); SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); // Instantiate the cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(message); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] original = cipher.doFinal(encrypted); As you can see I'm using 128 bit key, and 128 bit message. I always

AES encrypt/decrypt with Bouncy Castle provider [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-03 03:58:32
问题 This question already has answers here : Java Bouncy Castle Cryptography - Encrypt with AES (2 answers) Closed 6 years ago . Here is my implementation of a AES 256 encrypt and decrypt, developed with the native library of JDK 5: public static String encrypt(String key, String toEncrypt) throws Exception { Key skeySpec = generateKeySpec(key); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(toEncrypt.getBytes()); byte[]

BadPaddingException when decrypting AES with the same key

牧云@^-^@ 提交于 2019-12-03 03:50:45
This is the tester: public class CryptographySimpleTests extends ActivityTestCase { public void testsCryptographyClass_encryptAndDecrypt() { final String orgVal = "hi world! :D"; final String key = "key"; try { final byte[] encryptKey = Cryptography.deriveAES256Key(key); final byte[] decryptKey = Cryptography.deriveAES256Key(key); //Deviation method Assert.assertTrue(Arrays.equals(encryptKey, decryptKey)); byte[] encrypted = Cryptography.encryptAES(encryptKey, orgVal.getBytes()); Assert.assertFalse(Arrays.equals(encrypted, orgVal.getBytes())); byte[] decrypted = Cryptography.decryptAES

Node.js crypto key and iv to match java SecretKeySpec / IvParameterSpec

此生再无相见时 提交于 2019-12-03 03:46:17
I'm trying to to port a Java (simple) encryption algorythm to Node JS. I will need to be able to decrypt/encrypt stuff encrypted/decrypted from the Java side. I'm stuck at the very beginning, the initialization of the cipher. In Java, I get the key with SecretKeySpec , and the Initialization Vector with IvParameterSpec : public CryptStuff(String password) throws zillion_exceptions { if (password==null) throw new InvalidKeyException("No encryption password is set!"); key = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ivSpec=new

Python AES decryption

北慕城南 提交于 2019-12-03 03:45:48
I have the following piece of code in Java that I want to replicate in Python. public class AESDecryption { protected SecretKeySpec getPublicKey() { try { byte[] key = "MuidKeibimbtjph9".getBytes("UTF-8"); key = MessageDigest.getInstance("SHA-256").digest(key); key = Arrays.copyOf(key, 32); return new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public String decrypt(byte[] data) { Cipher cipher = null; try { cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(2,

Decrypting bytes encrypted by .NET's RijndaelManaged using Java

让人想犯罪 __ 提交于 2019-12-03 03:44:23
I am trying to decrypt something, which was encrypted using RijndaelManaged of .NET/C#, using Java to decrypt. The C# program is not mine; I cannot change it to be more interoperable. But I know how it is encrypting: byte[] bytes = new UnicodeEncoding().GetBytes(password); // edit: built-in is 8chars FileStream fileStream = new FileStream(outputFile, FileMode.Create); RijndaelManaged rijndaelManaged = new RijndaelManaged(); CryptoStream cryptoStream = new CryptoStream((Stream) fileStream, rijndaelManaged.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write); I do not know how to decrypt this

AES Encryption in Golang and Decryption in Java

ε祈祈猫儿з 提交于 2019-12-03 03:37:44
I have the following AES Encryption function written in Golang. func encrypt(key []byte, text string) string { plaintext := []byte(text) block, err := aes.NewCipher(key) if err != nil { panic(err) } ciphertext := make([]byte, aes.BlockSize+len(plaintext)) iv := ciphertext[:aes.BlockSize] if _, err := io.ReadFull(rand.Reader, iv); err != nil { panic(err) } stream := cipher.NewCFBEncrypter(block, iv) stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext) return base64.URLEncoding.EncodeToString(ciphertext) } I am struggling to understand the flow to decrypt the generated text using Java. Any

Encrypt and Decrypt by AES algorithm in both python and android

十年热恋 提交于 2019-12-03 03:19:55
I have python and android code for AES encryption. When I encrypt a text in android, it decrypt on python successfully but it can’t decrypt in android side. Do anyone have an idea? Python code : import base64 import hashlib from Crypto import Random from Crypto.Cipher import AES class AESCipher: def __init__(self, key): self.bs = 16 self.key = hashlib.sha256(key.encode()).digest() def encrypt(self, message): message = self._pad(message) iv = Random.new().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(message)).decode('utf-8') def