PHP到Android加密移植:CFB模式下的AES

时间:2011-01-28 10:03:32

标签: php android aes

我创建了一个PHP文件,其中包含以下代码,用于对字符串进行编码/解码:

$key = "D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ"; 

$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB), MCRYPT_RAND);

function encrypt($key, $text, $iv) {
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CFB, $iv)));
}

function decrypt($key, $text, $iv) { 
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_CFB, $iv));
}

$text = $_GET['text'];

echo "Plain Number : " . $text . "<br><br>";

$encrypted = encrypt($key, $text, $iv);
echo "AES Number : " . $encrypted . "<br><br>";
echo "block size=";
echo $block=mcrypt_get_block_size ( MCRYPT_RIJNDAEL_128,"cfb" );
 echo "<br/>"."IV=";
   echo $ivValue =mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CFB);
echo "<br/>";
echo "Plain Number : ". decrypt($key,$encrypted, $iv) . "<br><br>";

我希望使用Android API的

做同样的事情
static String encryptString(final String RAWDATA, boolean ENCODE) { // This
                  // was a
                  // custom
   Log.d("TEST1", iv.toString()); 
  String encrypted = null;
  byte[] encryptedBytes = null;
  byte[] key;

  key = AES_V1_KEY.getBytes();
  //SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  // Instantiate the cipher
  Cipher cipher = null;
  try {
   String input = Integer.toString(RAWDATA.length()) + '|' +RAWDATA;
   KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
   SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
   sr.setSeed(key);
    kgen.init(128, sr); // 192 and 256 bits may not be available
   SecretKey skey = kgen.generateKey();
      byte[] raw1 = skey.getEncoded();

      SecretKeySpec skeySpec = new SecretKeySpec(raw1,KEY_ALGORITHM);
   cipher = Cipher.getInstance(CIPHER_ALGORITHM);
   cipher.init(Cipher.ENCRYPT_MODE, skeySpec,MyEnc1.ivParameterSpec);
   //cipher.init(Cipher.ENCRYPT_MODE, MyEnc1.secretKeySpec,MyEnc1.ivParameterSpec);
    // encryptedBytes = cipher.doFinal(nullPadString(input).getBytes());

   encryptedBytes = cipher.doFinal(nullPadString(input).getBytes());
  }catch(InvalidAlgorithmParameterException e){
   Log.d("error",e.toString());
  } 
  catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (BadPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  encrypted = new String(encryptedBytes);
  if (ENCODE)
   encrypted = new String(Base64.encodeBytes(encryptedBytes));
  else
   encrypted = new String(encryptedBytes);
  return encrypted;

  // /return encrypted;
 }// method end

变量在哪里:

 final static String AES_V1_KEY = "D0QgiY8JYvx8qzKx0iaN8kwEJgwpEqAJ";
 private static final String KEY_ALGORITHM = "AES";
 private static final String CIPHER_ALGORITHM = "AES/CFB/NoPadding";

在Android端加密和解密可能使用自己的API。

0 个答案:

没有答案