在iOS和Android PBEWithMD5AndDES [英] PBEWithMD5AndDES in iOS and Android
问题描述
我有很多的字符串连接使用的Java PBEWithMD5AndDES算法$ C $的CD。
现在我端起了code到Objective-C的。但我不能正确解密数据。
我使用所有的code写的 href=\"http://stackoverflow.com/questions/7152995/pbewithmd5anddes-encryption-in-ios\">。
我用这个功能在Java中的加密/解密数据:
I have a lot of strings encoded by Java using PBEWithMD5AndDES algorithm. Now I'm porting the code into Objective-C. But I can't correct decrypt the data. I'm using all the code written here. I've used this functions in Java for encrypt/decrypt the data:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.amain);
try {
viewKey();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("checkingthevalue",e.toString());
}
}
private void viewKey() {
// TODO Auto-generated method stub
String pass = "password";
String testStr = "TheSecretString";
final byte[] SALT = {
(byte) 0xaa, (byte) 0xaa, (byte) 0xce, (byte) 0xce,
(byte) 0xaa, (byte) 0xaa, (byte) 0xce, (byte) 0xce,
};
try{
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(pass.toCharArray()));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
String strEnc = base64Encode(pbeCipher.doFinal(testStr.getBytes("UTF-8")));
Log.d("ViewKey", "strEnc :: " + strEnc);
}catch (Exception e)
{
Log.d("ViewKey","ERROR");
}
}
private static String base64Encode(byte[] bytes) {
final int asd = Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP ;
return Base64.encodeToString(bytes,asd);
}
结果字符串(字符串strEnc
)是6_O6V3327e5vnNpMB7_wNg
我的理解,我可以使用这个类来加密/解密使用OpenSSL iOS上的数据:
As I understood, I can use this class to encrypt/decrypt the data in iOS using OpenSSL:
- (void)viewDidLoad
{
[super viewDidLoad];
[self testSSL3];
}
-(void) testSSL{
PS_PBEwithMD5andDes *PBE = [[PS_PBEwithMD5andDes alloc] init];
NSString *password = @"password";
NSString *message = @"TheSecretString";
int it = 20;
{
NSData *inData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSData *encData = [PBE encryptPBEWithMD5AndDESData:inData password:password iterations:it];
NSString *encString = [encData base64EncodedString];
NSLog(@":: %@",encString);
}
}
类PS_PBEwithMD5andDes:
Class PS_PBEwithMD5andDes:
PS_PBEwithMD5andDes.h
//
// PS_PBEwithMD5andDes.h
//
//
// Created by Admin on 11.06.14.
// Copyright (c) 2014 PS. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PS_PBEwithMD5andDes : NSObject
- (NSData *)encryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password iterations:(int)iterations;
- (NSData *)decryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password iterations:(int)iterations;
- (NSData *)encodePBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password direction:(int)direction iterations:(int)iterations;
@end
PS_PBEwithMD5andDes.m
//
// PS_PBEwithMD5andDes.m
//
//
// Created by Admin on 11.06.14.
// Copyright (c) 2014 PS. All rights reserved.
//
#import "PS_PBEwithMD5andDes.h"
#import "libs/include_openssl/openssl/md5.h"
#import "libs/include_openssl/openssl/sha.h"
#import "libs/include_openssl/openssl/x509.h"
#import "libs/include_openssl/openssl/err.h"
#import "libs/include_openssl/openssl/evp.h"
@implementation PS_PBEwithMD5andDes
- (NSData *)encryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password iterations:(int)iterations{
return [self encodePBEWithMD5AndDESData:inData password:password direction:1 iterations:iterations];
}
- (NSData *)decryptPBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password iterations:(int)iterations {
return [self encodePBEWithMD5AndDESData:inData password:password direction:0 iterations:iterations];
}
- (NSData *)encodePBEWithMD5AndDESData:(NSData *)inData password:(NSString *)password direction:(int)direction iterations:(int)iterations
{
// Change salt and number of iterations for your project !!!
static const char gSalt[] =
{
(unsigned char) 0xaa, (unsigned char) 0xaa, (unsigned char) 0xce, (unsigned char) 0xce,
(unsigned char) 0xaa, (unsigned char) 0xaa, (unsigned char) 0xce, (unsigned char) 0xce
};
unsigned char *salt = (unsigned char *)gSalt;
int saltLen = strlen(gSalt);
EVP_CIPHER_CTX cipherCtx;
unsigned char *mResults; // allocated storage of results
int mResultsLen = 0;
const char *cPassword = [password UTF8String];
unsigned char *mData = (unsigned char *)[inData bytes];
int mDataLen = [inData length];
SSLeay_add_all_algorithms();
X509_ALGOR *algorithm = PKCS5_pbe_set(NID_pbeWithMD5AndDES_CBC, iterations, salt, saltLen);
memset(&cipherCtx, 0, sizeof(cipherCtx));
if (algorithm != NULL)
{
EVP_CIPHER_CTX_init(&(cipherCtx));
if (EVP_PBE_CipherInit(algorithm->algorithm, cPassword, strlen(cPassword),
algorithm->parameter, &(cipherCtx), direction))
{
EVP_CIPHER_CTX_set_padding(&cipherCtx, 1);
int blockSize = EVP_CIPHER_CTX_block_size(&cipherCtx);
int allocLen = mDataLen + blockSize + 1; // plus 1 for null terminator on decrypt
mResults = (unsigned char *)OPENSSL_malloc(allocLen);
unsigned char *in_bytes = mData;
int inLen = mDataLen;
unsigned char *out_bytes = mResults;
int outLen = 0;
int outLenPart1 = 0;
if (EVP_CipherUpdate(&(cipherCtx), out_bytes, &outLenPart1, in_bytes, inLen))
{
out_bytes += outLenPart1;
int outLenPart2 = 0;
if (EVP_CipherFinal(&(cipherCtx), out_bytes, &outLenPart2))
{
outLen += outLenPart1 + outLenPart2;
mResults[outLen] = 0;
mResultsLen = outLen;
}
} else {
unsigned long err = ERR_get_error();
ERR_load_crypto_strings();
ERR_load_ERR_strings();
char errbuff[256];
errbuff[0] = 0;
ERR_error_string_n(err, errbuff, sizeof(errbuff));
NSLog(@"OpenSLL ERROR:\n\tlib:%d\n\tfunction:%d\n\treason:%d\n",
ERR_lib_error_string(err),
ERR_func_error_string(err),
ERR_reason_error_string(err));
ERR_free_strings();
}
NSData *encryptedData = [NSData dataWithBytes:mResults length:mResultsLen];
return encryptedData;
}
}
return nil;
}
@end
结果(的NSString * encString
)的iOS的情况下,为 @h1wZWAuxI71flGJKGcwCOg ==
The result (NSString *encString
) in iOS case is @"h1wZWAuxI71flGJKGcwCOg=="
总之,结果:
在Java的:6_O6V3327e5vnNpMB7_wNg
在Objective-C @h1wZWAuxI71flGJKGcwCOg ==
该问题:
1)。为什么他们给不同的结果?
2)。如何使用的问题修复?
3)。为什么在所有的情况下,在Objective-C的2最后一个字母是==
?
在Objective-C即时通讯使用OpenSSL库。
The questions:
1). Why they give the different results?
2). How to fix with problem?
3). Why in all cases in Objective-C the 2 last letters is "=="
?
In Objective-C Im using OpenSSL library.
推荐答案
我跟你同样的问题,但我想我解决它;
i have the same problem with you ;but i think i fix it;
<一个href=\"http://stackoverflow.com/questions/7152995/pbewithmd5anddes-encryption-in-ios/7153353\">PBEWithMD5AndDES加密iOS中我用这个来加密字符串的ios;我修改加密盐:
PBEWithMD5AndDES Encryption in iOS i use this to encrypt string in ios ; and I modify encrypt salt to :
static const char gSalt[] =
{
(unsigned char)0x18, (unsigned char)0x79, (unsigned char)0x6D, (unsigned char)0x6D,
(unsigned char)0x35, (unsigned char)0x3A, (unsigned char)0x6A, (unsigned char)0x60,
(unsigned char)0x00
};
然后我设置了Android的encry盐:
then I set the android 's encry salt to :
private static byte[] salt = new byte[] {24, 121, 109, 109, 53, 58, 106, 96};
在IOS的盐的值必须0-128之间(因为它的类型是unsigned char和Android的盐是字节型)
the ios's salt's value must between 0-128 (because it's type is unsigned char and android's salt is byte type )
final byte[] SALT = {
(byte) 0xaa, (byte) 0xaa, (byte) 0xce, (byte) 0xce,
(byte) 0xaa, (byte) 0xaa, (byte) 0xce, (byte) 0xce,
};
这是您的盐,和0xAA为170> 128,所以当你将其转换为字节,值被改变;让你在Android的盐在盐的ios不同;所以这就是为什么你在Android和iOS不同的base64字符串;
this is your salt,0xaa is 170 >128 so when you convert it to byte ,value is changed; so you salt in android is different to salt in ios; so this is why you get different base64 string in android and ios;
这篇关于在iOS和Android PBEWithMD5AndDES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!