billp / vpccmcrypt Goto Github PK
View Code? Open in Web Editor NEWAES/CCM Implementation in Objective-C with Streaming Support
License: GNU Lesser General Public License v3.0
AES/CCM Implementation in Objective-C with Streaming Support
License: GNU Lesser General Public License v3.0
A lot of variables don't understand, for example:iv、adata
I'm having a problem that a Java Cipher can't decrypt the encrypted text because the Tag is wrong. It fails with following error: mac check in CCM failed
.
Her's a Java code I'm using:
import java.math.BigInteger;
import java.security.*;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CCMBlockCipher;
import org.bouncycastle.crypto.params.CCMParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class ADYDecrypt
{
private static String AES = "AES";
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static void main(String[] args)
{
try {
byte[] encrypted = hexStringToByteArray(args[0]);
byte[] keybytes = hexStringToByteArray("366D2C7804AEC975A7AB66BCF7B4A3DFA86476CCC5EFC7062CD2E80471FD663E");
SecretKey key = new SecretKeySpec(keybytes, AES);
byte[] iv = hexStringToByteArray("547971D2272DFA601147C8C1");
byte[] plain;
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
plain = cipher.doFinal(encrypted);
System.out.println(args[0] + " -> " + new String(plain));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
The plain text I'm encryption is abc123456
, the key
is 366D2C7804AEC975A7AB66BCF7B4A3DFA86476CCC5EFC7062CD2E80471FD663E
, the iv
is 547971D2272DFA601147C8C1
and the tag length is 8.
The encryption with your library gives 8aadc9c57ce679dc99 c37bc73cf9bb5fa8
(secod one is the tag).
While the encryption with OpenSSL (EVP_CIPHER_CTX
) is 8AADC9C57CE679DC99 B4FB1B4CD037A4BE
So it seems the Tag calculation is wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.