There is disclosed an apparatus and method for generating a keystream with ideal two-level autocorrelation, which can be efficiently implemented in both software and hardware on mobile devices requiring high security protection and in resource limited smart devices such as RFID chips. In one embodiment, a 16-bit Welch-Gong (WG) transformation is used as the filtering function, which is applied to 16-bit input values generated from a linear feedback shift register. In another embodiment, an 8-bit WG transformation is used which is applied to 8-bit input values generated from a linear feedback shift register.