Tag: aes

Java:摆脱`Cipher.init()`开销

我需要提高以下方法的性能: private byte[] decrypt(final byte[] encrypted, final Key key) throws … { this.cipher.init(Cipher.DECRYPT_MODE, key); return this.cipher.doFinal(encrypted); } cipher对象(“AES / ECB / NoPadding”)在构造函数中初始化,因此可以重用它。 使用ECB,因为encrypted数组将始终只包含16个字节的数据(即1个数据块)。 使用128位密钥。 这种方法被称为数百万次,以解密16字节的块,每次使用不同的密钥。 例如,这个方法被调用如下: final List keys = List with millions of keys final byte[] data = new byte[] { … 16 bytes of data go here …} for (final Key key : […]

如何在Python中加密并在Java中解密?

我正在尝试加密Python程序中的一些数据并将其保存,然后在Java程序中解密该数据。 在Python中,我正在加密它: from Crypto.Cipher import AES KEY = ‘12345678901234567890123456789012’ def encrypt(data): cipher = AES.new(KEY, AES.MODE_CFB) return cipher.encrypt(data) 在Java中,我正在解密它: import java.security.*; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; public class Encryption { private static byte[] KEY = { ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘0’, ‘1’, […]

Java端的AES加密 – 在PHP端解密并选择单个密钥

我正在使用AES,我想找到一个可以在Java端加密字符串的密钥,我在php端硬编码相同的密钥,如果字符串匹配则解密字符串我已经过身份validation。 以下是我在Java中的代码: 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 String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, […]

AES文件解密“给定最终块未正确填充”

我想加密然后解密文件使用AES。 我已经阅读了很多关于错误的主题”Given final block not properly padded” 。 但我找不到解决方案。 抱歉指定我的代码的语言,我不知道写java语言 这是我的代码: 变量 // IV, secret, salt in the same time private byte[] salt = { ‘h’, ‘u’, ‘n’, ‘g’, ‘d’, ‘h’, ‘9’, ‘4’ }; public byte[] iv; public SecretKey secret; createSecretKey public void createSecretKey(String password){ SecretKeyFactory factory = SecretKeyFactory.getInstance(“PBKDF2WithHmacSHA1”); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, […]

Java(Android)解密带附加IV的消息

我生成一个随机IV,这个IV连接(以普通字节)到加密的msg的前面,如下所示; public String encrypt(String plainText, byte[] encryptionKey) throws Exception { SecretKeySpec key = new SecretKeySpec(encryptionKey, “AES”); cipher.init(Cipher.ENCRYPT_MODE, key, iV); byte[] data = new byte[iV.getIV().length + plainText.getBytes(“UTF-8”).length]; // Merge together plain IV and encrypted cipher text System.arraycopy(iV.getIV(), 0, data, 0, iV.getIV().length); System.arraycopy(cipher.doFinal(plainText.getBytes(“UTF-8”)), 0, data, iV.getIV().length, plainText.getBytes(“UTF-8”).length); return Base64.encodeToString(data, Base64.DEFAULT); } 使用WiFi Direct在设备之间发送消息。 这是在我的MainActivity中处理的; case MESSAGE_READ: byte[] […]

通过URLCassloader加载加密的JarFile

我一直在编写一个小系统来动态加载AES加密的jar文件。 我的代码: public static void main(String args[]) throws Exception { String jar = “http://site.com/api/rsc/test.jar”; List urls = new ArrayList(); urls.add(getURL(jar)); URL jarurl = urls.get(0); ObjectInputStream ois = new ObjectInputStream((new URL(“http://site.com/api/rsc/key_1.txt”).openStream())); Object o = ois.readObject(); DESKeySpec ks = new DESKeySpec((byte[])o); SecretKeyFactory skf = SecretKeyFactory.getInstance(“DES”); SecretKey key = skf.generateSecret(ks); Cipher c = Cipher.getInstance(“DES/CFB8/NoPadding”); c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) […]

如何解密javascript中的文件,该文件由JAVA用AES加密

我用AES256加密了Java中的JPG文件,但不知道用javascript解密JPG文件。 谁有更好的主意? 我正在苦苦挣扎4天。 byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; String key = “1234567890123456789012345678901d”; AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(key.getBytes(“UTF-8”), “AES”); Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS5Padding”); cipher.init(mode, newKey, ivSpec); InputStream input = null; OutputStream output = null; try […]

使用AES-128算法加密Java中的Excel文件和C#中的解密

我试图用Java加密excel文件,然后用C#解密,但解密文件显示垃圾字符。 我能够在Java中加密.txt和.docx文件,在C#中解密而没有任何问题。 请检查代码并告诉我,如果我做错了什么? 甚至让我知道是否需要添加任何东西。 Java代码加密xls文件 // file to be encrypted File file = new File(“D:\\SynchData\\output.xls”); FileInputStream inFile = new FileInputStream(file); FileOutputStream outFile = new FileOutputStream(“D:\\SynchData\\output.dec”); // password to encrypt the file String password = “MAKV2SPBNI99212”; // password, iv and salt should be transferred to the other end // in a secure manner // salt is […]

用Java中的AES算法加密

我创建了一个包p,包含一些整数和布尔值。 包如下: TCPPacket p=new TCPPacket(481,516,23,42,true,false,false,false,false,false,false,false,10,10); 如何用Java加密数据包?

使用IGE解密Java AES-256

我正试图用IGE解密AES-256。 但我不知道如何使用256位密钥。 在代码密钥中 – byte [] , 长度 == 32; 四 。 长度 == 32; BlockSize == 16 Cipher cipher = Cipher.getInstance(“AES/ECB/NoPadding”); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, “AES”)); Xprev = java.util.Arrays.copyOfRange(IV, 0, BlockSize); Yprev = java.util.Arrays.copyOfRange(IV, BlockSize, IV.length); Decripted = new byte[0]; for (int i = 0; i < Message.length; i += BlockSize) { Y = java.util.Arrays.copyOfRange(Message, […]