在Android中使用AESCrypt加密和解密

时间:2017-04-02 16:14:48

标签: android encryption aes

我正在开发一款应用,但我发现了一个问题。我可以加密,但我无法解密。你能帮我解决一下吗?

该应用程序涉及敏感数据,我想发送Firebase加密数据,但它们仍在列表中解密。

这是我的代码:

public class UserActivity extends AppCompatActivity {

private EditText input_tipo, input_id, input_password;
private ListView list_data;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
private FirebaseAnalytics firebaseAnalytics;
private List<User> list_users = new ArrayList<>();
private User selectedUser;
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private String encryptPass, decryptPass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    Toolbar tool = (Toolbar) findViewById(R.id.toolbar);
    tool.setTitle("MySecurity");
    setSupportActionBar(tool);

    input_tipo = (EditText) findViewById(R.id.tipo);
    input_id = (EditText) findViewById(R.id.id);
    input_password = (EditText) findViewById(R.id.password);
    list_data = (ListView) findViewById(R.id.list_data);

    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();

    list_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            User user = (User) adapterView.getItemAtPosition(i);
            selectedUser = user;

            input_tipo.setText(user.getTipo());
            input_id.setText(user.getId());
            input_password.setText(user.getPassword());

        }
    });


    initFirebase();
    addEventFirebaseListener();
    findNetwork();

}


private void findNetwork() {
    ConnectivityManager CM = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ninfo = CM.getActiveNetworkInfo();
    if (ninfo != null && ninfo.isConnectedOrConnecting()) {
        Toast.makeText(getBaseContext(), "Connesso in Firebase:" + ninfo.getTypeName(), Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getBaseContext(), "Connessione internet assente", Toast.LENGTH_LONG).show();
    }
}


private void addEventFirebaseListener() {
    list_data.setVisibility(View.VISIBLE);
    mDatabaseReference.child("users").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (list_users.size() > 0)
                list_users.clear();
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                User user = postSnapshot.getValue(User.class);
                list_users.add(user);
            }
            ListViewAdapter adapter = new ListViewAdapter(UserActivity.this, list_users);
            list_data.setAdapter(adapter);
            list_data.setVisibility(View.VISIBLE);
        }


            @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

private void initFirebase() {
    FirebaseApp.initializeApp(this);
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    firebaseAnalytics = FirebaseAnalytics.getInstance(this);
    firebaseAnalytics.setAnalyticsCollectionEnabled(true);
    firebaseAnalytics.setMinimumSessionDuration(5000);
    mDatabaseReference = mFirebaseDatabase.getReference().child("users").child(mUser.getUid());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.home, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.menu_add) {
        createUser();
    } else if (item.getItemId() == R.id.menu_remove) {
        deleteUser(selectedUser);
    } else if (item.getItemId() == R.id.menu_deselect) {
        DeselectedUser(selectedUser);
    }else if(item.getItemId()== R.id.menu_decrypt){
        DecryptUser(selectedUser);
    }
    return true;
}

private void DecryptUser(User selectedUser) {
    if (selectedUser != null) {
        mDatabaseReference.child("users").child(selectedUser.getUid());

        String password = selectedUser.getPassword().trim();
        String Message= selectedUser.getPassword().trim();

        try {
            decryptPass= AESCrypt.decrypt( password, Message);
            input_tipo.setText(input_tipo.getText().toString());
            input_id.setText(input_id.getText().toString());
            input_password.setText(decryptPass);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


private void DeselectedUser(User selectedUser) {
    if (selectedUser != null) {
        mDatabaseReference.child("users").child(selectedUser.getUid());
        clearEditText();
    }
}

private void deleteUser(User selectedUser) {
    if (selectedUser != null) {
        mDatabaseReference.child("users").child(selectedUser.getUid()).removeValue();
        clearEditText();
    }
}


private void createUser() {

    String password = input_password.getText().toString().trim();
    String Message= input_password.getText().toString().trim();

    try {
        encryptPass = AESCrypt.encrypt(password, Message);
        User user1= new User(UUID.randomUUID().toString(), input_tipo.getText().toString(),
                input_id.getText().toString(), encryptPass);
        mDatabaseReference.child("users").child(user1.getUid()).setValue(user1);
        clearEditText();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void clearEditText() {
    input_tipo.setText("");
    input_id.setText("");
    input_password.setText("");
}

@Override
protected void onStop() {
    super.onStop();
    finish();
}

}

1 个答案:

答案 0 :(得分:0)

<canvas id="canvas" width="600" height="400"></canvas>password变量分配的内容相同(Message)。您正在做的是尝试使用用户密码解密用户密码。您可能需要将要解密的密文分配给selectedUser.getPassword().trim()变量。