As a boy of fourteen, Duke of York, future James II, had a bitter experience of having his letter in cipher discovered by the enemy.
In June 1645, three years after open war broke out between the King and the Parliament, the royalist army had a crushing defeat at Naseby. In April 1646, the royalist stronghold Oxford was threatened by the parliamentarian army and Charles I barely escaped and threw himself to the protection of the Scottish army. Soon Oxford surrendered and the Duke was held prisoner by the parliamentarians at St. James' Palace. (His elder brother, the Prince of Wales and future Charles II, had gone over to France after the Battle of Naseby.) The first attempt to deliver the Duke, made within a few weeks, was discovered but the Duke denied involvement in the plan.
Meanwhile, Charles I could not reach an agreement with the Scots. In January 1647, the Scots handed over the person of Charles I to the English parliamentarians. The parliamentarians still wanted to have the good will of the King and in July, when he was staying in Caversham, he was permitted to write to his children. The King was even allowed to meet his children from time to time.
It appears that some time after being at Caversham, the King sent the Duke a letter with an enclosed cipher (Lords Journal, 24 February 1648). In response to one of the King's letters, the Duke wrote partly in cipher as follows.
The letter mentioned in cipher a plan of "conveying away" the Duke by the assistance of Mrs. Kilvert, an attendant of Princess Elizabeth (James' younger sister) and a barber called Hill. However, the letter was intercepted by the parliamentarians. Examined by a committee of two lords and four commoners sent by the Parliament, the fourteen-year-old Duke could not deny his authorship.
As to the cipher, the Duke had given the key to Mrs. Kilvert to hide it in some prearranged part of the house (Clarke, The Life of James II, p.32). The Duke said to the committee he had burnt the key (Clarke p.32; Lords Journal, op. cit.). When threatened with imprisonment in the Tower, he produced the key (Jock Haswell, James II, p.32) but he did not reveal his assistants (Clarke p.32). Anyway, the Duke's letter was deciphered and read in the Parliament.
The Duke had to write his engagement to the Parliament.
Hill was imprisoned for some time and discharged from the Duke's service but Mrs. Kilvert's involvement was never discovered (Clarke p.31)
Even while the examination was ongoing, another escape was planned by Colonel Joseph Bampfield (also spelled Bamfield, Bamfylde, etc.). This time, the Duke took care to commit nothing to paper. George Howard (younger brother of the Earl of Suffolk), placed in his service by the Parliament, had been won over and every day carried verbal messages between the Duke and the Colonel (Clarke p.34, Apology p.126).
Due to Bampfield's later reputation, historians were reluctant to turn to Bampfield's own account, published many years later, but John Loftis and Paul H. Hardacre found it was factually accurate except in rare instances (John Loftis and Paul H. Hardacre ed., Colonel Joseph Bampfield's Apology "Written by Himself and Printed at His Desire" 1685).
According to Bampfield's Apology, the King's letter of 24 January 1648 told Bampfield to consider an escape plan and wanted to have correspondence with the Duke (Apology, Section 92).
Apparently, there was a plan to depose the King, disinherit the Prince of Wales, and crown the Duke of York (Gardiner p.99, Apology p.125). Bampfield wrote to the Earl of Lanark (later second Duke of Hamilton), "Shortly the design of the Duke of York's crowning in case there be a necessity that monarchical government must continue, is freshly thought upon." Much of the sentence was in code, "622" representing "the Duke of York" (Apology, p.241, 280). This may have led the King to think the Duke had to be rescued as soon as possible (Gardiner p.100, n.3).
According to Bampfield's Apology, he had "the enclosed" to be delivered to the Duke by "a sure hand" (i.e., Howard (Apology, p.126, Section 94)) and the Duke let him know that he had promised the King to make his escape (Apology, Section 93). Although the interception of the Duke's letter put the parliamentarians on alert, the King's letter of 22 February approved what Bampfield had already done and what he proposed for the saving of the Duke of York (Apology, Section 94).
As for the Duke's engagement not try to escape, Bampfield overcame his scruples by telling him he could not make "any engagement in a business of so public concernment ... without his father's consent" (Apology, p.126).
On 20 April 1648 after supper, the Duke played hide and seek with his younger brother and sister. For the past fortnight, the Duke used to hide himself so well that he was not found for half an hour. This assured that, when his escape plan would be carried out, he would not be missed for at least half an hour. When the game started, he got out of the house and found Bampfield, who took the Duke across the Channel to Holland. The adventures of the rescue can be read in, e.g., Clarke, The Life of James the Second, p.34 ff. (Google Booksearch).