ध्यान दें कि पीएचपी mcrypt शून्य बाइट गद्दी तो new ZeroBytePadding()
new PKCS7Padding()
के बजाय इस्तेमाल किया जाना चाहिए उपयोग करता है।
CBC
और RIJNDAEL 256
का उपयोग करके पूर्ण कार्यान्वयन को पूरा करें।
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.ZeroBytePadding;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Base64;
public static String encryptWithAesCBC(String plaintext, String key, String iv)
{
try {
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new ZeroBytePadding());
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key.getBytes()), iv.getBytes());
cipher.init(true, ivAndKey);
return new String(Base64.encode(cipherData(cipher, plaintext.getBytes())));
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e);
}
}
public static String decryptWithAesCBC(String encrypted, String key, String iv)
{
try {
byte[] ciphertext = Base64.decode(encrypted);
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new ZeroBytePadding());
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key.getBytes()), iv.getBytes());
aes.init(false, ivAndKey);
return new String(cipherData(aes, ciphertext));
} catch (InvalidCipherTextException e) {
throw new RuntimeException(e);
}
}
private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws InvalidCipherTextException
{
int minSize = cipher.getOutputSize(data.length);
byte[] outBuf = new byte[minSize];
int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
int length2 = cipher.doFinal(outBuf, length1);
int actualLength = length1 + length2;
byte[] cipherArray = new byte[actualLength];
for (int x = 0; x < actualLength; x++) {
cipherArray[x] = outBuf[x];
}
return cipherArray;
}
private String md5(String string)
{
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(string.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
CFB उपयोग करते समय, PaddedBufferedBlockCipher
द्वारा की जगह होना चाहिए निम्नलिखित:
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CFBBlockCipher(new RijndaelEngine(256),8), new ZeroBytePadding());
// PHP mcrypt uses a blocksize of 8 bit for CFB
उपयोग:
String salt = "fbhweui3497";
String key = md5(salt);
String iv = md5(md5(salt));
String encrypted = encryptWithAesCBC("text to encript", key, iv);
String decrypted = decryptWithAesCBC(encrypted, key, iv);
स्रोत
2017-11-16 08:52:15
शायद आपने अपना जावा JCE असीमित एन्क्रिप्शन कक्षाएं स्थापित किया है सुनिश्चित करने के लिए होगा । वे विशिष्ट मानक जावा वितरण का हिस्सा नहीं हैं क्योंकि वे कुछ देशों में अवैध हैं। – Romain
पहले से ही उन्हें है। अभी भी पता नहीं लगा सकता कि आवश्यक अवरोध – Laures
के साथ एक सिफर ऑब्जेक्ट कैसे प्राप्त करें, फिर मुझे विश्वास है कि @ ग्रेग्स का उत्तर है। मेरा मानना था कि रिजेंडेल -256 जेसीई असीमित प्रदाता का हिस्सा था, लेकिन मुझे अपनी कंपनी के कुछ हद तक अनुकूलित जावा का उपयोग करने के लिए उपयोग किया जाता है - जिसमें इसके लिए एक जेसीई प्रदाता है, लेकिन यह निजी कार्यान्वयन भी हो सकता है। – Romain