Objective Cの暗号化クラスを自分のiPhoneアプリに使用していますが、AndroidのアプリからJAVAで同じ機能を使用するのに苦労しています。私の暗号化コードは以下の通りです:Java文字列を暗号化
NSString * _secret = @"password";
NSString * _key = @"1428324560542678";
StringEncryption *crypto = [[StringEncryption alloc] init];
NSData *_secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding];
CCOptions padding = kCCOptionPKCS7Padding;
NSData *encryptedData = [crypto encrypt:_secretData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];
私はJAVAでそれを複製しようとしましたが、私は同じデータをエンコードするときに別の文字列を取得します。だから私は何か間違っているが、私はそれを理解できない。私のJAVAコードは以下の通りです:
byte[] key = "1428324560542678".getBytes();
Cipher c = null;
try {
c = Cipher.getInstance("AES/ECB/PKCS7Padding");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SecretKeySpec k = new SecretKeySpec(key, "AES");
try {
c.init(Cipher.ENCRYPT_MODE, k);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
EditText tv1passwordText = (EditText) findViewById(R.id.password);
String password = URLEncoder.encode(tv1passwordText.getText().toString(), "UTF-8");
byte[] encryptedData = c.doFinal(password.getBytes());
どこが間違っているのでしょうか? I以下のコメントをもとに
はGetBytesメソッドを追加しましたが、生産の文字列はまだ異なっている:
byte[] key = null;
try {
key = "1428324560542678".getBytes("UTF-8");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Cipher c = null;
try {
c = Cipher.getInstance("AES/ECB/PKCS7Padding");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SecretKeySpec k = new SecretKeySpec(key, "AES");
try {
c.init(Cipher.ENCRYPT_MODE, k);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
EditText tv1passwordText = (EditText) findViewById(R.id.password);
byte[] password = tv1passwordText.getText().toString().getBytes("UTF-8");
byte[] encryptedData = c.doFinal(password);
復号化します文字列が一致するようにします。 –