Tag: aes

将java中的加密代码转换为Ruby

我一直在尝试将java中的加密代码转换为ruby,但我无法完全完成。 我得到不同的价值观。 passphrase = passphrase + STATIC_KEY; byte[] key = passphrase.getBytes(“UTF-8”); MessageDigest sha = MessageDigest.getInstance(“SHA-1”); key = sha.digest(key); key = Arrays.copyOf(key, 16); SecretKey secretKey = new SecretKeySpec(key, “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); IvParameterSpec initialisationVector = new IvParameterSpec( new byte[16]); cipher.init(Cipher.ENCRYPT_MODE, secretKey, initialisationVector); byte[] encryptedData = cipher.doFinal(plainText.getBytes(“UTF-8”)); return SimpleCrypto.toHex(encryptedData); 任何人都可以让我知道,如何做到这一点ruby。 unencrypted = “passphrase” c = OpenSSL::Cipher.new(“aes-128-cbc”) […]

AES / CBC / PKCS5Padding加密在java解密ruby中

我试图加密java中的数据并解密ruby中的数据。 我发现几乎相同的问题,但我的情况有点不同。 用Ruby加密和用Java解密 – 为什么它不起作用? AES / CBC在Java中加密,在Ruby中解密 我的代码是…在java中加密 import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import net.sf.json.JSONObject; import org.apache.commons.codec.binary.Hex; class Crypt { public static void main(String[] args) throws Exception { Map node = new HashMap(); node.put(“timestamp”, “1377499097199”); JSONObject jsonObject = JSONObject.fromObject(node); String json = jsonObject.toString(); System.out.println(json); //key String skeyString […]

javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整 – 解密加密的AES字符串

我试图解密我从后端服务器收到的字符串”~9?8?m???=?T?G” ,后端服务器使用OpenSSL使用AES-256-CBC加密字符串。 有代码块: public static String decryptText(String textToDecrypt) { try { byte[] base64TextToDecrypt = Base64.encodeBase64(textToDecrypt.getBytes(“UTF-8”)); byte[] guid = “fjakdsjkld;asfj”.getBytes(“UTF-8”); byte[] iv = new byte[16]; System.arraycopy(guid, 0, iv, 0, guid.length); IvParameterSpec ips = new IvParameterSpec(iv); byte[] secret = DECRYPTION_SECRET_HASH.getBytes(“UTF-8”); SecretKeySpec secretKey = new SecretKeySpec(secret, “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); // decryption pass cipher.init(Cipher.DECRYPT_MODE, secretKey, ips); byte[] […]

如何使用AES解密Android中的文件?

我有一个文件(mp3),我加密,我的意图是将此文件下载到Android设备并解密它,但在解密过程中我得到一个IOException: java.io.IOException: last block incomplete in decryption 我知道下面的代码中存在明显的安全漏洞,我只是想让它先工作。 任何有关这方面的帮助都表示赞赏,我对编码一般都是新人,所以如果这是一个愚蠢的问题,请提前原谅我! 加密类(未在Android中完成,有效): public class EncryptFile { public static void main(String args[]) { if (args.length < 1) { System.out.println("Usage: java EncryptFile “); System.exit(-1); } try { File aesFile = new File(“encodedfile.enc”); FileInputStream fis; FileOutputStream fos; CipherInputStream cis; //Creation of Secret key String key = “mysecretkey”; int length=key.length(); if(length>16 […]

使用AES加密16个字节时,为什么密文长32个字节?

我使用加密AES算法,当我加密16字节(一个块)时,结果是32字节。 这个可以吗? 我使用的源代码是: package net.sf.andhsli.hotspotlogin; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * Usage: * * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext) * … * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto) * * @author ferenc.hechler */ public class SimpleCrypto { public static String encrypt(String seed, String cleartext) throws Exception { byte[] rawKey = […]

没有填充的Java AES

在没有自动填充的情况下,AES加密和解密16字节数组有哪些最简单的方法? 我找到了使用外部库的解决方案,但我希望尽可能避免这种情况。 我目前的代码是 SecretKeySpec skeySpec = new SecretKeySpec(getCryptoKeyByteArray(length=16)); // 128 bits Cipher encryptor = Cipher.getInstance(“AES”); encryptor.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = encryptor.doFinal(plain); 如何防止填充? plain数据总是固定长度并包含自己的填充。 如何在不使encrypted成为32字节的情况下允许plain为16字节?

需要解决AES中错误的IV长度问题

我正在尝试在Java中实现AES,这是我使用的代码: byte[] sessionKey = {00000000000000000000000000000000}; byte[] iv = {00000000000000000000000000000000}; byte[] plaintext = “6a84867cd77e12ad07ea1be895c53fa3”.getBytes(); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, “AES”), new IvParameterSpec(iv)); byte[] ciphertext = cipher.doFinal(plaintext); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, “AES”), new IvParameterSpec(iv)); byte[] deciphertext = cipher.doFinal(ciphertext); 我需要这个固定密钥和IV用于测试目的,但我得到以下exception: Exception in thread “main” java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long at com.sun.crypto.provider.SunJCE_h.a(DashoA12275) at com.sun.crypto.provider.AESCipher.engineInit(DashoA12275) […]

AES密钥长度错误无效

此代码提供无效的AES密钥长度错误。 我怎么能纠正它? (我想要128位密钥AES加密) package org.temp2.cod1; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; public class Code1 { public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { String s = “9882623867”; byte[] plaintext = s.getBytes(“UTF-16”); String s2 = “supernova”; byte[] key = s2.getBytes(“UTF-16”); Cipher c = Cipher.getInstance(“AES”); SecretKeySpec k = new SecretKeySpec(key, […]

如何解密从Mifare Desfire EV1发送的第一条消息

有没有人知道如何解密从卡发送的第一条消息? 我的意思是在身份validation成功后然后你发送一个命令(例如0x51(GetRealTagUID)。它返回00 + random32bits(总是不同)。我尝试解密它: private byte[] decrypt(byte[] raw, byte[] encrypted, byte[] iv) throws Exception { IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } 用decrypt调用它(sessionKey,response,iv) IV =全零(16字节) 响应= 0x51命令后的32randombits(刚刚删除了两个零) 有人告诉我,第一次发送命令(0x51)后IV发生了变化。 如何生成正确的IV来解密该响应? 我认为全零是错误的,因为解密的消息总是不同的,并且它应该始终与同一张卡相同。 -编辑- 在应用您的(Michael Roland)指令后,解密的响应仍然只是随机位。 这是我的代码(我想我做错了): byte[] x = […]

字符串加密工作,byte 数组类型的加密不起作用

我正在使用以下LINK进行加密,并使用Strings进行了尝试并且它有效。 但是,由于我正在处理图像,我需要使用字节数组进行加密/解密过程。 所以我将该链接中的代码修改为以下内容: public class AESencrp { private static final String ALGO = “AES”; private static final byte[] keyValue = new byte[] { ‘T’, ‘h’, ‘e’, ‘B’, ‘e’, ‘s’, ‘t’, ‘S’, ‘e’, ‘c’, ‘r’,’e’, ‘t’, ‘K’, ‘e’, ‘y’ }; public static byte[] encrypt(byte[] Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); […]