0

我是使用 AES 加密的新手。我需要在 Android 上加密一个字符串并将其发送到 OpenBSD 进行解密。我可以使用 OpenSSl 和 Android 使用此代码在 Openbsd 上加密/解密,但是来自 Android 的加密字符串不等于 OpenBSD 中的解密字符串 谁能帮帮我。

...
public class StringCryptor 
{
    private static final String CIPHER_ALGORITHM = "AES";
    private static final String RANDOM_GENERATOR_ALGORITHM = "SHA1PRNG";
    private static final int RANDOM_KEY_SIZE = 128;
    // Private key already generated with generatekey()
    static String PKEY= "15577737BBD910E794A6B3C250678DAF";
    // Convert PKEY to byte[]
    static byte[] secretKey = toByte(PKEY);

    // Encrypts string and encode in Base64
    public static String encrypt( String password, String data ) throws Exception 
    {
         byte[] clear = data.getBytes();
         SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, CIPHER_ALGORITHM );
         Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
         cipher.init( Cipher.ENCRYPT_MODE, secretKeySpec );
         byte[] encrypted = cipher.doFinal( clear );
         String encryptedString = Base64.encodeToString( encrypted, Base64.DEFAULT );
         return encryptedString;
    }

    // Decrypts string encoded in Base64
    public static String decrypt( String password, String encryptedData ) throws Exception 
    {
         SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );
         Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );
         cipher.init( Cipher.DECRYPT_MODE, secretKeySpec );
         byte[] encrypted = Base64.decode( encryptedData, Base64.DEFAULT );
         byte[] decrypted = cipher.doFinal( encrypted );
         return new String( decrypted );
    }


    // Convert String To Hexa
    public static String toHex(byte[] buf) { 
        if (buf == null)  
            return "";    
        StringBuffer result = new StringBuffer(2*buf.length);  
        for (int i = 0; i < buf.length; i++) {
           appendHex(result, buf[i]);       
        }        
       return result.toString();
    }
  // Convert hex To byte
  public static byte[] toByte(String hexString) { 
        int len = hexString.length()/2;      
        byte[] result = new byte[len];        
        for (int i = 0; i < len; i++)               
            result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();   
        return result;
  }

  private final static String HEX = "0123456789ABCDEF";
  private static void appendHex(StringBuffer sb, byte b) {
      sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  }

//public static byte[] generateKey( byte[] seed ) throws Exception
    //{
    //KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM );
        //SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM );
        //secureRandom.setSeed( seed );
        //keyGenerator.init( RANDOM_KEY_SIZE, secureRandom );
        //SecretKey secretKey = keyGenerator.generateKey();
        //return secretKey.getEncoded();
    //}
}
...

用android生成的加密字符串

    Bonjour >>>>>>> NkrWPLgiY0rt34iaNzhjOg==

在 OpenBSD 中,我使用 android 中生成的私钥加密字符串

#openssl version
openSSl 0.9.8k 25 Mar 2009
#echo "Bonjour">test.txt
#openssl enc -aes1278 -a -in text.txt -K 15577737BBD910E794A6B3C250678DAF -iv 0
4UwyKgMGJ41xPwTph2qHXQ==
4

1 回答 1

1

我花时间测试你的代码,问题很简单。如果您这样做echo "Bonjour" > test.txt,“换行符”会自动添加到Bonjour.

So in Java you encrypt the string "Bonjour" but the text.txt file read by openssl contains the string "Bonjour\n". You can change that by adding the -n flag to echo. Now openssl should print the same as Java:

$ echo -n "Bonjour" > test.txt
$ openssl enc -aes128 -a -in test.txt -K 15577737BBD910E794A6B3C250678DAF  -iv 0
NkrWPLgiY0rt34iaNzhjOg==

Obviously my statement that the IV is randomly generated by Java is wrong.

于 2013-03-09T06:29:54.187 回答