使用OpenSSL解密Android SSE
这是一个伟大的跨平台加密和解密的开源程序。它们为其他系统提供了一个客户端(基于它们对基于Java的bouncy castle库的使用),但我想知道是否可以使用openssl进行解密。这对我来说主要是一个练习,但是如果允许其他人通过发送openssl命令来对我的文件进行解密就更好了 SSE是开源的,但以下是生成加密密钥的核心代码:使用OpenSSL解密Android SSE,android,encryption,openssl,bouncycastle,Android,Encryption,Openssl,Bouncycastle,这是一个伟大的跨平台加密和解密的开源程序。它们为其他系统提供了一个客户端(基于它们对基于Java的bouncy castle库的使用),但我想知道是否可以使用openssl进行解密。这对我来说主要是一个练习,但是如果允许其他人通过发送openssl命令来对我的文件进行解密就更好了 SSE是开源的,但以下是生成加密密钥的核心代码: /** Generate password-base Keys (128, 256, 448 bits) */ private void generatePBKeys4
/** Generate password-base Keys (128, 256, 448 bits) */
private void generatePBKeys448Max(String pw, boolean unicodeAllowed) throws NoSuchAlgorithmException, InvalidKeySpecException
{
pw = pw.trim();
if(unicodeAllowed) pw = convertToCodePoints(pw);
byte[] shaL1 = getSHA512Hash(pw.getBytes());
byte[] shaSalt = getSHA256Hash(Helpers.getSubarray(shaL1, 0, 8));
byte[] shaIV = getSHA256Hash(Helpers.getSubarray(shaL1, 8, 8));
PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA1Digest());
char[] passwordChars = pw.toCharArray();
final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
pGen.init(pkcs12PasswordBytes, shaSalt, 600);
CBCBlockCipher aesCBC = new CBCBlockCipher(new AESFastEngine());
ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
aesCBC.init(false, aesCBCParams);
byte[] key = ((KeyParameter)aesCBCParams.getParameters()).getKey();
byte[] k01 = Helpers.getSubarray(key, 0, 16);
byte[] k02 = Helpers.getSubarray(key, 16, 16);
keysVault.put("KS256", key);
keysVault.put("KS128", Helpers.xorit(k01, k02));
byte[] k31 = Helpers.getSubarray(shaL1, 40, 24);
byte[] k32 = Helpers.concat(key, k31);
keysVault.put("KS448", k32);
String forIV = new String(getMD5Hash(shaIV));
PKCS12ParametersGenerator pGenIV = new PKCS12ParametersGenerator(new SHA1Digest());
char[] ivChars = forIV.toCharArray();
final byte[] pkcs12IVBytes = PBEParametersGenerator.PKCS12PasswordToBytes(ivChars);
pGenIV.init(pkcs12IVBytes, getMD5Hash(shaSalt).getBytes(), 100);
CBCBlockCipher aesCBC2 = new CBCBlockCipher(new AESFastEngine());
ParametersWithIV aesCBCParams2 = (ParametersWithIV) pGenIV.generateDerivedParameters(256, 128);
aesCBC2.init(false, aesCBCParams2);
byte[] keyIV = ((KeyParameter)aesCBCParams2.getParameters()).getKey();
byte[] k11 = Helpers.getSubarray(keyIV, 0, 16);
byte[] k12 = Helpers.getSubarray(keyIV, 16, 16);
byte[] ivTemp = Helpers.xorit(k11, k12);
byte[] k21 = Helpers.getSubarray(ivTemp, 0, 8);
byte[] k22 = Helpers.getSubarray(ivTemp, 8, 8);
keysVault.put("IS128", ivTemp);
keysVault.put("IS64", Helpers.xorit(k21, k22));
}
到目前为止,我已经这样做了(注意,SSE设置为使用AES 256加密)
我是这里的初学者,所以我可能错过了很多…:-/ 恢复一个死帖子,但我注意到的几件事是,您希望使用
-binary
选项生成openssl哈希。您还希望在echo上使用-n选项,以确保不会将换行符添加到明文密码短语中。此外,IV和Salt也是sha256散列的,所以您希望在十六进制转储之前这样做。
$ echo "testpass" | openssl sha512 -out shaL1
$ dd count=8 bs=1 if=shaL1 of=shaSalt
$ dd skip=8 count=8 bs=1 if=shaL1 of=shaIV
$ hexdump -ve '1/1 "%.2x"' shaSalt > shaSalt.hex
$ hexdump -ve '1/1 "%.2x"' shaIV > shaIV.hex
$ openssl aes-256-cbc -d -S shaSalt.hex -kfile shaIV.hex -p -in temp.enc -out mydir
bad magic number