Reversible shuffle algorithm using a key

后端 未结 4 1994
夕颜
夕颜 2020-11-29 09:21

How would I code a reversible shuffle algorithm in C# which uses a key to shuffle and can be reversed to the original state?

For instance, I have a string: \"Hello

4条回答
  •  清歌不尽
    2020-11-29 10:06

    A java question also redirects here, so here is the full cryptographic-strength java implementation:

    import java.security.*;
    import java.util.*;
    
    /** Cryptographic strength reversible random shuffle. To be truly secure, the passKey arguments should be 20 chars or more and (obviously) not guessable. */
    public class SecureShuffle
    {
        public static int[] getShuffleExchanges(int size, String passKey)
        {
            int[] exchanges = new int[size - 1];
            SecureRandom rand = new SecureRandom(passKey.getBytes());
            for (int i = size - 1; i > 0; i--)
            {
                int n = rand.nextInt(i + 1);
                exchanges[size - 1 - i] = n;
            }
            return exchanges;
        }
    
        public static void shuffle(byte[] toShuffle, String passKey)
        {
            int size = toShuffle.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = size - 1; i > 0; i--)
            {
                int n = exchanges[size - 1 - i];
                byte tmp = toShuffle[i];
                toShuffle[i] = toShuffle[n];
                toShuffle[n] = tmp;
            }
        }
    
        public static void deshuffle(byte[] shuffled, String passKey)
        {
            int size = shuffled.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = 1; i < size; i++)
            {
                int n = exchanges[size - i - 1];
                byte tmp = shuffled[i];
                shuffled[i] = shuffled[n];
                shuffled[n] = tmp;
            }
        }
    
        public static void shuffle(char[] toShuffle, String passKey)
        {
            int size = toShuffle.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = size - 1; i > 0; i--)
            {
                int n = exchanges[size - 1 - i];
                char tmp = toShuffle[i];
                toShuffle[i] = toShuffle[n];
                toShuffle[n] = tmp;
            }
        }
    
        public static void deshuffle(char[] shuffled, String passKey)
        {
            int size = shuffled.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = 1; i < size; i++)
            {
                int n = exchanges[size - i - 1];
                char tmp = shuffled[i];
                shuffled[i] = shuffled[n];
                shuffled[n] = tmp;
            }
        }
    
        public static void shuffle(int[] toShuffle, String passKey)
        {
            int size = toShuffle.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = size - 1; i > 0; i--)
            {
                int n = exchanges[size - 1 - i];
                int tmp = toShuffle[i];
                toShuffle[i] = toShuffle[n];
                toShuffle[n] = tmp;
            }
        }
    
        public static void deshuffle(int[] shuffled, String passKey)
        {
            int size = shuffled.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = 1; i < size; i++)
            {
                int n = exchanges[size - i - 1];
                int tmp = shuffled[i];
                shuffled[i] = shuffled[n];
                shuffled[n] = tmp;
            }
        }
    
        public static void shuffle(long[] toShuffle, String passKey)
        {
            int size = toShuffle.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = size - 1; i > 0; i--)
            {
                int n = exchanges[size - 1 - i];
                long tmp = toShuffle[i];
                toShuffle[i] = toShuffle[n];
                toShuffle[n] = tmp;
            }
        }
    
        public static void deshuffle(long[] shuffled, String passKey)
        {
            int size = shuffled.length;
            int[] exchanges = getShuffleExchanges(size, passKey);
            for (int i = 1; i < size; i++)
            {
                int n = exchanges[size - i - 1];
                long tmp = shuffled[i];
                shuffled[i] = shuffled[n];
                shuffled[n] = tmp;
            }
        }
    
        public static void main(String[] args)
        {
            String passphrase = "passphrase";
            String text = "The rain in Spain stays mainly on the plain";
    
            char[] chars = text.toCharArray();
            shuffle(chars, passphrase);
            System.out.println(new String(chars));
    
            deshuffle(chars, passphrase);
            System.out.println(new String(chars));
    
            byte[] bytes = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
            shuffle(bytes, passphrase);
            System.out.println(Arrays.toString(bytes));
    
            deshuffle(bytes, passphrase);
            System.out.println(Arrays.toString(bytes));
        }
    
    }
    

提交回复
热议问题